DIP/python/dip-clustering-service/tests/test_cluster_api.py

86 lines
3.0 KiB
Python

from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "UP"
assert "DBSCAN" in data["algorithms"]
def test_kmeans_cluster():
body = {
"algorithm": "KMEANS",
"parameters": {"k": 2},
"reductionMethod": "NONE",
"items": [
{
"embeddingId": "11111111-1111-1111-1111-111111111111",
"documentId": "22222222-2222-2222-2222-222222222221",
"representationId": "33333333-3333-3333-3333-333333333331",
"vector": [1.0, 1.0]
},
{
"embeddingId": "11111111-1111-1111-1111-111111111112",
"documentId": "22222222-2222-2222-2222-222222222222",
"representationId": "33333333-3333-3333-3333-333333333332",
"vector": [1.1, 1.0]
},
{
"embeddingId": "11111111-1111-1111-1111-111111111113",
"documentId": "22222222-2222-2222-2222-222222222223",
"representationId": "33333333-3333-3333-3333-333333333333",
"vector": [-1.0, -1.0]
},
{
"embeddingId": "11111111-1111-1111-1111-111111111114",
"documentId": "22222222-2222-2222-2222-222222222224",
"representationId": "33333333-3333-3333-3333-333333333334",
"vector": [-1.1, -1.0]
}
]
}
response = client.post("/cluster", json=body)
assert response.status_code == 200
data = response.json()
assert len(data["clusters"]) == 2
assert data["noiseCount"] == 0
def test_dbscan_cluster_with_noise():
body = {
"algorithm": "DBSCAN",
"parameters": {"eps": 0.25, "minSamples": 2, "normalizeVectors": False},
"reductionMethod": "NONE",
"items": [
{
"embeddingId": "11111111-1111-1111-1111-111111111211",
"documentId": "22222222-2222-2222-2222-222222222211",
"representationId": "33333333-3333-3333-3333-333333333211",
"vector": [0.0, 0.0]
},
{
"embeddingId": "11111111-1111-1111-1111-111111111212",
"documentId": "22222222-2222-2222-2222-222222222212",
"representationId": "33333333-3333-3333-3333-333333333212",
"vector": [0.05, 0.0]
},
{
"embeddingId": "11111111-1111-1111-1111-111111111213",
"documentId": "22222222-2222-2222-2222-222222222213",
"representationId": "33333333-3333-3333-3333-333333333213",
"vector": [10.0, 10.0]
}
]
}
response = client.post("/cluster", json=body)
assert response.status_code == 200
data = response.json()
assert data["noiseCount"] == 1