11import pytest
22from fastapi .testclient import TestClient
33from unittest .mock import MagicMock , AsyncMock
4+ import uuid
5+
6+ # Mock dependencies before importing app
7+ import src .infra .queue
8+ import src .db .session
9+
10+ # We mock the connection and session at the module level or within fixtures
411from src .api .app import app
5- import src .api .app as api_app
612
713client = TestClient (app )
814
15+ @pytest .fixture
16+ def mock_db (mocker ):
17+ mock_session = MagicMock ()
18+ mocker .patch ("src.db.session.SessionLocal" , return_value = mock_session )
19+ # Mock Depends(get_db)
20+ app .dependency_overrides [src .db .session .get_db ] = lambda : mock_session
21+ yield mock_session
22+ app .dependency_overrides .clear ()
23+
24+ @pytest .fixture
25+ def mock_queue (mocker ):
26+ mock_q = MagicMock ()
27+ mocker .patch ("src.api.app.get_queue" , return_value = mock_q )
28+ return mock_q
29+
930@pytest .fixture
1031def mock_retrieval_wf (mocker ):
1132 mock_wf = MagicMock ()
@@ -23,7 +44,10 @@ def test_health_check():
2344 assert response .json ()["status" ] == "ok"
2445
2546def test_query_endpoint_uninitialized ():
26- # Before initialization, it should return 503
47+ # Set global retrieval_wf to None for this test
48+ import src .api .app as api_app
49+ api_app .retrieval_wf = None
50+
2751 response = client .post ("/query" , json = {"query" : "test" })
2852 assert response .status_code == 503
2953 assert "not initialized" in response .json ()["detail" ]
@@ -51,3 +75,28 @@ def test_query_endpoint_error(mock_retrieval_wf, mocker):
5175
5276 assert response .status_code == 500
5377 assert "Retrieval failed" in response .json ()["detail" ]
78+
79+ def test_ingest_endpoint (mock_db , mock_queue ):
80+ # Mock job creation
81+ job_id = uuid .uuid4 ()
82+ mock_db .add .side_effect = lambda job : setattr (job , 'id' , job_id )
83+
84+ response = client .post ("/ingest" )
85+
86+ assert response .status_code == 202
87+ assert response .json ()["job_id" ] == str (job_id )
88+ mock_queue .enqueue .assert_called_once ()
89+
90+ def test_get_job_status (mock_db ):
91+ job_id = str (uuid .uuid4 ())
92+ mock_job = MagicMock ()
93+ mock_job .id = job_id
94+ mock_job .status = "COMPLETED"
95+
96+ mock_db .query .return_value .filter .return_value .first .return_value = mock_job
97+
98+ response = client .get (f"/jobs/{ job_id } " )
99+
100+ assert response .status_code == 200
101+ assert response .json ()["status" ] == "COMPLETED"
102+ assert response .json ()["id" ] == job_id
0 commit comments