feat(v2): add BCL snapshot extractor and command
This commit is contained in:
32
tests/fixtures/bcl/bcl_players_stats.json
vendored
Normal file
32
tests/fixtures/bcl/bcl_players_stats.json
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"player": {
|
||||
"id": "bcl-player-42",
|
||||
"name": "John Carter",
|
||||
"first_name": "John",
|
||||
"last_name": "Carter",
|
||||
"birth_date": "1999-07-14",
|
||||
"nationality": "US",
|
||||
"height_cm": 198,
|
||||
"weight_kg": 95,
|
||||
"position": "SF"
|
||||
},
|
||||
"team": {
|
||||
"id": "bcl-team-murcia",
|
||||
"name": "UCAM Murcia"
|
||||
},
|
||||
"gp": 12,
|
||||
"mpg": 29.1,
|
||||
"ppg": 16.4,
|
||||
"rpg": 5.8,
|
||||
"apg": 2.7,
|
||||
"spg": 1.5,
|
||||
"bpg": 0.6,
|
||||
"tov": 2.3,
|
||||
"fg_pct": 48.1,
|
||||
"three_pct": 37.2,
|
||||
"ft_pct": 81.4
|
||||
}
|
||||
]
|
||||
}
|
||||
92
tests/test_bcl_extractor.py
Normal file
92
tests/test_bcl_extractor.py
Normal file
@ -0,0 +1,92 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from django.core.management import call_command
|
||||
|
||||
from apps.ingestion.extractors.bcl import BCLSnapshotExtractor
|
||||
from apps.ingestion.extractors.registry import create_extractor
|
||||
|
||||
|
||||
def _load_fixture(path: str) -> dict:
|
||||
fixture_path = Path(__file__).parent / "fixtures" / path
|
||||
return json.loads(fixture_path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bcl_extractor_normalizes_fixture_payload(tmp_path, settings):
|
||||
settings.EXTRACTOR_BCL_STATS_URL = "https://www.championsleague.basketball/public/stats.json"
|
||||
settings.EXTRACTOR_BCL_SEASON_LABEL = "2025-2026"
|
||||
settings.EXTRACTOR_BCL_COMPETITION_EXTERNAL_ID = "bcl"
|
||||
settings.EXTRACTOR_BCL_COMPETITION_NAME = "Basketball Champions League"
|
||||
|
||||
fixture_payload = _load_fixture("bcl/bcl_players_stats.json")
|
||||
|
||||
class FakeClient:
|
||||
def get_json(self, *_args, **_kwargs):
|
||||
return fixture_payload
|
||||
|
||||
extractor = BCLSnapshotExtractor(http_client=FakeClient())
|
||||
output_path = tmp_path / "bcl.json"
|
||||
result = extractor.run(output_path=output_path, snapshot_date=date(2026, 3, 13))
|
||||
|
||||
assert result.extractor_name == "bcl"
|
||||
assert result.source_name == "bcl"
|
||||
assert result.records_count == 1
|
||||
|
||||
payload = json.loads(output_path.read_text(encoding="utf-8"))
|
||||
assert payload["source_name"] == "bcl"
|
||||
assert payload["snapshot_date"] == "2026-03-13"
|
||||
row = payload["records"][0]
|
||||
assert row["competition_external_id"] == "bcl"
|
||||
assert row["competition_name"] == "Basketball Champions League"
|
||||
assert row["team_external_id"] == "bcl-team-murcia"
|
||||
assert row["team_name"] == "UCAM Murcia"
|
||||
assert row["player_external_id"] == "bcl-player-42"
|
||||
assert row["full_name"] == "John Carter"
|
||||
assert row["minutes_per_game"] == 29.1
|
||||
assert row["three_pt_pct"] == 37.2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bcl_extractor_registry_selection(settings):
|
||||
settings.EXTRACTOR_BCL_STATS_URL = "https://www.championsleague.basketball/public/stats.json"
|
||||
settings.EXTRACTOR_BCL_SEASON_LABEL = "2025-2026"
|
||||
extractor = create_extractor("bcl")
|
||||
assert isinstance(extractor, BCLSnapshotExtractor)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_run_bcl_extractor_command_writes_snapshot(tmp_path, settings, monkeypatch):
|
||||
settings.EXTRACTOR_BCL_STATS_URL = "https://www.championsleague.basketball/public/stats.json"
|
||||
settings.EXTRACTOR_BCL_SEASON_LABEL = "2025-2026"
|
||||
settings.EXTRACTOR_BCL_COMPETITION_EXTERNAL_ID = "bcl"
|
||||
settings.EXTRACTOR_BCL_COMPETITION_NAME = "Basketball Champions League"
|
||||
|
||||
fixture_payload = _load_fixture("bcl/bcl_players_stats.json")
|
||||
|
||||
class FakeClient:
|
||||
def get_json(self, *_args, **_kwargs):
|
||||
return fixture_payload
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.ingestion.extractors.bcl.ResponsibleHttpClient",
|
||||
lambda **_kwargs: FakeClient(),
|
||||
)
|
||||
|
||||
call_command(
|
||||
"run_bcl_extractor",
|
||||
"--output-path",
|
||||
str(tmp_path),
|
||||
"--snapshot-date",
|
||||
"2026-03-13",
|
||||
)
|
||||
|
||||
files = list(tmp_path.glob("bcl-2026-03-13.json"))
|
||||
assert len(files) == 1
|
||||
payload = json.loads(files[0].read_text(encoding="utf-8"))
|
||||
assert payload["source_name"] == "bcl"
|
||||
assert len(payload["records"]) == 1
|
||||
Reference in New Issue
Block a user