Paijo commited on
update tests/integration/test_hunter_service.py
Browse files
tests/integration/test_hunter_service.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from unittest.mock import MagicMock, AsyncMock, patch
|
| 3 |
+
from app.hunter.service import HunterService
|
| 4 |
+
from app.db_models import CandidateSource
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.mark.asyncio
|
| 8 |
+
async def test_hunter_service_run():
|
| 9 |
+
# Mock strategies
|
| 10 |
+
mock_strat_1 = MagicMock()
|
| 11 |
+
mock_strat_1.name = "mock1"
|
| 12 |
+
mock_strat_1.discover = AsyncMock(return_value=["http://new.com/list.txt"])
|
| 13 |
+
|
| 14 |
+
mock_strat_2 = MagicMock()
|
| 15 |
+
mock_strat_2.name = "mock2"
|
| 16 |
+
mock_strat_2.discover = AsyncMock(return_value=[])
|
| 17 |
+
|
| 18 |
+
service = HunterService()
|
| 19 |
+
service.strategies = [mock_strat_1, mock_strat_2]
|
| 20 |
+
|
| 21 |
+
# Mock DB session and get_db
|
| 22 |
+
mock_session = AsyncMock()
|
| 23 |
+
mock_result = MagicMock()
|
| 24 |
+
mock_result.scalar_one_or_none.return_value = None
|
| 25 |
+
mock_session.execute.return_value = mock_result
|
| 26 |
+
|
| 27 |
+
# Mock extractors/fetchers to avoid network
|
| 28 |
+
service._fetch_content = AsyncMock(return_value="1.1.1.1:80")
|
| 29 |
+
service._calculate_confidence = MagicMock(return_value=50)
|
| 30 |
+
|
| 31 |
+
# Mock UniversalExtractor to ensure isolation
|
| 32 |
+
with patch("app.hunter.service.UniversalExtractor") as mock_extractor:
|
| 33 |
+
mock_extractor.extract_proxies.return_value = [
|
| 34 |
+
MagicMock(protocol="http", ip="1.1.1.1", port=80)
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
with patch("app.hunter.service.get_db") as mock_get_db:
|
| 38 |
+
|
| 39 |
+
async def async_gen():
|
| 40 |
+
yield mock_session
|
| 41 |
+
|
| 42 |
+
mock_get_db.return_value = async_gen()
|
| 43 |
+
|
| 44 |
+
await service.run_hunt()
|
| 45 |
+
|
| 46 |
+
# Verify strategies called
|
| 47 |
+
mock_strat_1.discover.assert_called_once()
|
| 48 |
+
mock_strat_2.discover.assert_called_once()
|
| 49 |
+
|
| 50 |
+
# Verify DB add called
|
| 51 |
+
if not mock_session.add.called:
|
| 52 |
+
# Debugging: check if execute was called
|
| 53 |
+
print(f"Execute called count: {mock_session.execute.call_count}")
|
| 54 |
+
|
| 55 |
+
assert mock_session.add.called
|
| 56 |
+
args = mock_session.add.call_args[0]
|
| 57 |
+
candidate = args[0]
|
| 58 |
+
assert isinstance(candidate, CandidateSource)
|
| 59 |
+
assert candidate.url == "http://new.com/list.txt"
|
| 60 |
+
assert candidate.confidence_score == 50
|
| 61 |
+
assert candidate.status == "pending"
|