200 lines
6.3 KiB
Python
200 lines
6.3 KiB
Python
"""
|
|
Integration Tests for RAGDocument API
|
|
End-to-end tests for API endpoints
|
|
"""
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from main import app
|
|
from src.models._model import RAGDocument
|
|
from src.config.database import SessionLocal
|
|
|
|
client = TestClient(app)
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup_db():
|
|
"""Setup and teardown database for each test"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.query(RAGDocument).delete()
|
|
db.commit()
|
|
db.close()
|
|
|
|
class TestRAGDocumentAPI:
|
|
def test_create__success(self, setup_db):
|
|
"""Test successful creation of """
|
|
data = {
|
|
"id": True,
|
|
"document_type": "testdocument_type",
|
|
"title": "testtitle",
|
|
"payer_id": True,
|
|
"payer_name": "testpayer_name",
|
|
"specialty": "testspecialty",
|
|
"content": True,
|
|
"content_hash": "testcontent_hash",
|
|
"embedding_vector": True,
|
|
"chunk_index": True,
|
|
"parent_document_id": True,
|
|
"source_url": "testsource_url",
|
|
"source_file_path": "testsource_file_path",
|
|
"effective_date": True,
|
|
"expiration_date": True,
|
|
"version": "testversion",
|
|
"is_active": True,
|
|
"is_stale": True,
|
|
"relevance_score": True,
|
|
"usage_count": True,
|
|
"last_used_at": True,
|
|
"metadata": True,
|
|
"tags": True,
|
|
"uploaded_by_id": True,
|
|
"": True,
|
|
"": True,
|
|
}
|
|
|
|
response = client.post("/api/v1/s", json=data)
|
|
|
|
assert response.status_code == 201
|
|
assert response.json()["id"] is not None
|
|
for key, value in data.items():
|
|
assert response.json()[key] == value
|
|
|
|
def test_create__validation_error(self):
|
|
"""Test validation error on invalid data"""
|
|
invalid_data = {}
|
|
|
|
response = client.post("/api/v1/s", json=invalid_data)
|
|
|
|
assert response.status_code == 400
|
|
assert "errors" in response.json()
|
|
|
|
def test_get__by_id_success(self, setup_db):
|
|
"""Test successful retrieval by id"""
|
|
# Create a first
|
|
create_data = {
|
|
"id": True,
|
|
"document_type": "testdocument_type",
|
|
"title": "testtitle",
|
|
"payer_id": True,
|
|
"payer_name": "testpayer_name",
|
|
"specialty": "testspecialty",
|
|
"content": True,
|
|
"content_hash": "testcontent_hash",
|
|
"embedding_vector": True,
|
|
"chunk_index": True,
|
|
"parent_document_id": True,
|
|
"source_url": "testsource_url",
|
|
"source_file_path": "testsource_file_path",
|
|
"effective_date": True,
|
|
"expiration_date": True,
|
|
"version": "testversion",
|
|
"is_active": True,
|
|
"is_stale": True,
|
|
"relevance_score": True,
|
|
"usage_count": True,
|
|
"last_used_at": True,
|
|
"metadata": True,
|
|
"tags": True,
|
|
"uploaded_by_id": True,
|
|
"": True,
|
|
"": True,
|
|
}
|
|
create_response = client.post("/api/v1/s", json=create_data)
|
|
_id = create_response.json()["id"]
|
|
|
|
response = client.get("/api/v1/s/" + str(_id))
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["id"] == _id
|
|
|
|
def test_get__not_found(self):
|
|
"""Test 404 when not found"""
|
|
response = client.get("/api/v1/s/999")
|
|
|
|
assert response.status_code == 404
|
|
assert "message" in response.json()
|
|
|
|
def test_update__success(self, setup_db):
|
|
"""Test successful update"""
|
|
# Create a first
|
|
create_data = {
|
|
"id": True,
|
|
"document_type": "testdocument_type",
|
|
"title": "testtitle",
|
|
"payer_id": True,
|
|
"payer_name": "testpayer_name",
|
|
"specialty": "testspecialty",
|
|
"content": True,
|
|
"content_hash": "testcontent_hash",
|
|
"embedding_vector": True,
|
|
"chunk_index": True,
|
|
"parent_document_id": True,
|
|
"source_url": "testsource_url",
|
|
"source_file_path": "testsource_file_path",
|
|
"effective_date": True,
|
|
"expiration_date": True,
|
|
"version": "testversion",
|
|
"is_active": True,
|
|
"is_stale": True,
|
|
"relevance_score": True,
|
|
"usage_count": True,
|
|
"last_used_at": True,
|
|
"metadata": True,
|
|
"tags": True,
|
|
"uploaded_by_id": True,
|
|
"": True,
|
|
"": True,
|
|
}
|
|
create_response = client.post("/api/v1/s", json=create_data)
|
|
_id = create_response.json()["id"]
|
|
|
|
update_data = {"name": "Updated"}
|
|
response = client.put("/api/v1/s/" + str(_id), json=update_data)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["name"] == update_data["name"]
|
|
|
|
def test_delete__success(self, setup_db):
|
|
"""Test successful deletion"""
|
|
# Create a first
|
|
create_data = {
|
|
"id": True,
|
|
"document_type": "testdocument_type",
|
|
"title": "testtitle",
|
|
"payer_id": True,
|
|
"payer_name": "testpayer_name",
|
|
"specialty": "testspecialty",
|
|
"content": True,
|
|
"content_hash": "testcontent_hash",
|
|
"embedding_vector": True,
|
|
"chunk_index": True,
|
|
"parent_document_id": True,
|
|
"source_url": "testsource_url",
|
|
"source_file_path": "testsource_file_path",
|
|
"effective_date": True,
|
|
"expiration_date": True,
|
|
"version": "testversion",
|
|
"is_active": True,
|
|
"is_stale": True,
|
|
"relevance_score": True,
|
|
"usage_count": True,
|
|
"last_used_at": True,
|
|
"metadata": True,
|
|
"tags": True,
|
|
"uploaded_by_id": True,
|
|
"": True,
|
|
"": True,
|
|
}
|
|
create_response = client.post("/api/v1/s", json=create_data)
|
|
_id = create_response.json()["id"]
|
|
|
|
response = client.delete("/api/v1/s/" + str(_id))
|
|
|
|
assert response.status_code == 204
|
|
|
|
# Verify deletion
|
|
get_response = client.get("/api/v1/s/" + str(_id))
|
|
assert get_response.status_code == 404
|
|
|