Implement story 2.1: aircraft data model and fetcher

Add HttpFetcher and FileFixtureFetcher with shared _parse_aircraft helper,
DUMP1090_URL constant, realistic fixture data, and full test coverage for
all acceptance criteria (AC1–AC5).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Matt Edholm
2026-04-22 23:03:20 -04:00
parent 7d89166880
commit 6208134a1c
5 changed files with 236 additions and 35 deletions
@@ -1,6 +1,6 @@
# Story 2.1: Aircraft Data Model & Fetcher
Status: ready-for-dev
Status: review
## Story
@@ -22,18 +22,18 @@ AC5: **Given** a `FileFixtureFetcher` pointed at `tests/fixtures/aircraft_sample
## Tasks / Subtasks
- [ ] Task 1: Implement `Aircraft` dataclass in `src/planemapper/models.py` (AC: #1, #2, #4)
- [ ] 1.1 Replace the existing stub with the full dataclass as specified in architecture: `icao: str`, `lat: float`, `lon: float`, `heading: float = 0.0`, `altitude_ft: int = 0`, `callsign: str = ""`, `category: str = ""`, `is_mlat: bool = False`, `is_stale: bool = False`
- [ ] 1.2 Add `from __future__ import annotations` and `from dataclasses import dataclass` imports
- [ ] 1.3 Confirm `ruff check .` passes with zero violations after change
- [x] Task 1: Implement `Aircraft` dataclass in `src/planemapper/models.py` (AC: #1, #2, #4)
- [x] 1.1 Replace the existing stub with the full dataclass as specified in architecture: `icao: str`, `lat: float`, `lon: float`, `heading: float = 0.0`, `altitude_ft: int = 0`, `callsign: str = ""`, `category: str = ""`, `is_mlat: bool = False`, `is_stale: bool = False`
- [x] 1.2 Add `from __future__ import annotations` and `from dataclasses import dataclass` imports
- [x] 1.3 Confirm `ruff check .` passes with zero violations after change
- [ ] Task 2: Add `DUMP1090_URL` constant to `src/planemapper/constants.py` (AC: #1, #3)
- [ ] 2.1 Add `DUMP1090_URL = "http://localhost:8080/data/aircraft.json"` to `constants.py`
- [ ] 2.2 Confirm existing constants are unaffected and `ruff check .` passes
- [x] Task 2: Add `DUMP1090_URL` constant to `src/planemapper/constants.py` (AC: #1, #3)
- [x] 2.1 Add `DUMP1090_URL = "http://localhost:8080/data/aircraft.json"` to `constants.py`
- [x] 2.2 Confirm existing constants are unaffected and `ruff check .` passes
- [ ] Task 3: Implement `HttpFetcher` in `src/planemapper/fetcher.py` (AC: #1, #2, #3, #4)
- [ ] 3.1 Add imports: `import requests`, `from pathlib import Path`, `from planemapper.constants import DUMP1090_URL, FETCH_TIMEOUT_S`
- [ ] 3.2 Implement `_parse_aircraft(entry: dict) -> Aircraft` private module-level helper:
- [x] Task 3: Implement `HttpFetcher` in `src/planemapper/fetcher.py` (AC: #1, #2, #3, #4)
- [x] 3.1 Add imports: `import requests`, `from pathlib import Path`, `from planemapper.constants import DUMP1090_URL, FETCH_TIMEOUT_S`
- [x] 3.2 Implement `_parse_aircraft(entry: dict) -> Aircraft` private module-level helper:
- Map `hex``icao`
- Map `lat``lat`, `lon``lon`
- Map `flight``callsign` with `.strip()` and default `""`
@@ -41,44 +41,44 @@ AC5: **Given** a `FileFixtureFetcher` pointed at `tests/fixtures/aircraft_sample
- Map `category``category` with default `""`
- Map `mlat``is_mlat`: `bool(entry.get("mlat"))` (empty list → `False`, non-empty → `True`)
- `is_stale` always defaults to `False`
- [ ] 3.3 Implement `HttpFetcher` class:
- [x] 3.3 Implement `HttpFetcher` class:
- `fetch(self) -> list[Aircraft]` calls `requests.get(DUMP1090_URL, timeout=FETCH_TIMEOUT_S)`
- Parses top-level JSON key `"aircraft"` as a list
- Skips entries missing `lat` or `lon` (cannot be plotted)
- Returns `[_parse_aircraft(e) for e in entries]`
- Does NOT catch `requests.Timeout` — let it propagate
- [ ] 3.4 Confirm `HttpFetcher` satisfies `FetcherInterface` structurally (no explicit inheritance needed)
- [x] 3.4 Confirm `HttpFetcher` satisfies `FetcherInterface` structurally (no explicit inheritance needed)
- [ ] Task 4: Implement `FileFixtureFetcher` in `src/planemapper/fetcher.py` (AC: #5)
- [ ] 4.1 Add `FileFixtureFetcher` class after `HttpFetcher`:
- [x] Task 4: Implement `FileFixtureFetcher` in `src/planemapper/fetcher.py` (AC: #5)
- [x] 4.1 Add `FileFixtureFetcher` class after `HttpFetcher`:
- Constructor: `__init__(self, path: Path)` stores `self._path = path`
- `fetch(self) -> list[Aircraft]` reads JSON from `self._path`, parses with same `_parse_aircraft` helper
- Same skip logic for missing `lat`/`lon`
- [ ] 4.2 Confirm `FileFixtureFetcher` satisfies `FetcherInterface` structurally
- [x] 4.2 Confirm `FileFixtureFetcher` satisfies `FetcherInterface` structurally
- [ ] Task 5: Update `tests/fixtures/aircraft_sample.json` with realistic dump1090 data (AC: #1, #2, #4, #5)
- [ ] 5.1 Replace the empty `{"aircraft": []}` stub with a JSON object containing four aircraft entries:
- [x] Task 5: Update `tests/fixtures/aircraft_sample.json` with realistic dump1090 data (AC: #1, #2, #4, #5)
- [x] 5.1 Replace the empty `{"aircraft": []}` stub with a JSON object containing four aircraft entries:
- Entry 1: complete aircraft — all fields present (`hex`, `lat`, `lon`, `flight`, `altitude`, `category`, `mlat: []`)
- Entry 2: missing `flight` (callsign) — should default to `""`
- Entry 3: missing `altitude` — should default to `altitude_ft=0`
- Entry 4: MLAT aircraft — `mlat` is a non-empty list (e.g. `["lat", "lon"]`)
- [ ] 5.2 Ensure all four entries have `lat` and `lon` so none are skipped
- [x] 5.2 Ensure all four entries have `lat` and `lon` so none are skipped
- [ ] Task 6: Write tests in `tests/test_fetcher.py` covering all 5 ACs (AC: #1#5)
- [ ] 6.1 Test AC1: use `responses` library or `unittest.mock.patch` to mock `requests.get`; assert all fields on a fully-populated aircraft are correct
- [ ] 6.2 Test AC2: mock a response with missing `callsign`, `altitude`, `category`; assert defaults are applied and no exception raised
- [ ] 6.3 Test AC3: mock `requests.get` to raise `requests.Timeout`; assert `HttpFetcher.fetch()` propagates it (does not catch it)
- [ ] 6.4 Test AC4: mock a response where the MLAT aircraft has `"mlat": ["lat", "lon"]`; assert `is_mlat=True`
- [ ] 6.5 Test AC5: point `FileFixtureFetcher` at `tests/fixtures/aircraft_sample.json`; assert the expected `list[Aircraft]` is returned with no `requests.get` call made
- [x] Task 6: Write tests in `tests/test_fetcher.py` covering all 5 ACs (AC: #1#5)
- [x] 6.1 Test AC1: use `responses` library or `unittest.mock.patch` to mock `requests.get`; assert all fields on a fully-populated aircraft are correct
- [x] 6.2 Test AC2: mock a response with missing `callsign`, `altitude`, `category`; assert defaults are applied and no exception raised
- [x] 6.3 Test AC3: mock `requests.get` to raise `requests.Timeout`; assert `HttpFetcher.fetch()` propagates it (does not catch it)
- [x] 6.4 Test AC4: mock a response where the MLAT aircraft has `"mlat": ["lat", "lon"]`; assert `is_mlat=True`
- [x] 6.5 Test AC5: point `FileFixtureFetcher` at `tests/fixtures/aircraft_sample.json`; assert the expected `list[Aircraft]` is returned with no `requests.get` call made
- [ ] Task 7: Update `tests/test_models.py` — verify dataclass (AC: #1, #2)
- [ ] 7.1 Confirm `test_aircraft_defaults` and `test_aircraft_full` (already present from story 1.1 QA) still pass after the full dataclass is in place — no stub test needed, these are real assertions
- [ ] 7.2 Add a test for the `altitude` edge-case if not already covered: create `Aircraft(icao="X", lat=0.0, lon=0.0, altitude_ft=0)` and assert `altitude_ft == 0`
- [x] Task 7: Update `tests/test_models.py` — verify dataclass (AC: #1, #2)
- [x] 7.1 Confirm `test_aircraft_defaults` and `test_aircraft_full` (already present from story 1.1 QA) still pass after the full dataclass is in place — no stub test needed, these are real assertions
- [x] 7.2 Add a test for the `altitude` edge-case if not already covered: create `Aircraft(icao="X", lat=0.0, lon=0.0, altitude_ft=0)` and assert `altitude_ft == 0`
- [ ] Task 8: Run quality gates
- [ ] 8.1 `pytest tests/` — all tests pass, 0 failures
- [ ] 8.2 `ruff check .` — zero violations
- [ ] 8.3 `ruff format --check .` — no formatting issues
- [x] Task 8: Run quality gates
- [x] 8.1 `pytest tests/` — all tests pass, 0 failures
- [x] 8.2 `ruff check .` — zero violations
- [x] 8.3 `ruff format --check .` — no formatting issues
## Dev Notes