|
1 | 1 | # tests/conftest.py |
2 | | -from unittest.mock import MagicMock, patch |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from fastapi.testclient import TestClient |
6 | 6 |
|
| 7 | +# Mock databases before any imports - patch where they're USED not where they're defined |
| 8 | +_mock_engine_patch = patch("app.services.auth_service.get_odmantic_engine") |
| 9 | +_mock_driver_patch = patch("app.services.auth_service.get_graph_db_driver") |
| 10 | + |
| 11 | +_mock_engine = _mock_engine_patch.start() |
| 12 | +_mock_driver = _mock_driver_patch.start() |
| 13 | + |
| 14 | +_mock_engine.return_value = AsyncMock() |
| 15 | +_mock_driver.return_value = MagicMock() |
| 16 | + |
| 17 | + |
| 18 | +def pytest_sessionfinish(session, exitstatus): |
| 19 | + """Clean up mocks after test session""" |
| 20 | + _mock_engine_patch.stop() |
| 21 | + _mock_driver_patch.stop() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def reset_auth_service_singleton(): |
| 26 | + """Reset AuthService instances before each test""" |
| 27 | + yield |
| 28 | + |
7 | 29 |
|
8 | 30 | @pytest.fixture(scope="module") |
9 | 31 | def client(): |
10 | | - with patch("app.services.dbs.databases.get_odmantic_engine") as mock_get_engine: |
11 | | - mock_get_engine.return_value = MagicMock() |
12 | | - from app.main import app # Importa después de aplicar el patch |
13 | | - with TestClient(app) as c: |
14 | | - yield c |
| 32 | + from app.main import app |
| 33 | + with TestClient(app) as c: |
| 34 | + yield c |
0 commit comments