"""Tests for agenda.build_place_yaml.""" from pathlib import Path import yaml from agenda import build_place_yaml def test_upsert_station_adds_new_station(tmp_path: Path) -> None: """Station upsert should add a new station to stations.yaml.""" path = tmp_path / "stations.yaml" path.write_text("""- name: London St Pancras latitude: 51.531921 longitude: -0.126361 country: gb wikidata: Q720102 routes: {} """) replaced = build_place_yaml.upsert_station( tmp_path, { "name": "Paris Gare du Nord", "latitude": 48.8809, "longitude": 2.3553, "country": "fr", "wikidata": "Q624511", "routes": {}, }, ) stations = yaml.safe_load(path.read_text()) assert replaced is False assert [station["name"] for station in stations] == [ "London St Pancras", "Paris Gare du Nord", ] assert "\n\n- name: Paris Gare du Nord\n" in path.read_text() def test_upsert_station_replaces_existing_station(tmp_path: Path) -> None: """Station upsert should replace an existing station with the same name.""" path = tmp_path / "stations.yaml" path.write_text("""- name: Paris Gare du Nord latitude: 0 longitude: 0 country: fr wikidata: Q624511 routes: {} """) replaced = build_place_yaml.upsert_station( tmp_path, { "name": "Paris Gare du Nord", "latitude": 48.8809, "longitude": 2.3553, "country": "fr", "wikidata": "Q624511", "routes": {}, }, ) stations = yaml.safe_load(path.read_text()) assert replaced is True assert len(stations) == 1 assert stations[0]["latitude"] == 48.8809 assert "\n\n- name:" not in path.read_text() def test_upsert_airport_adds_mapping_entry(tmp_path: Path) -> None: """Airport upsert should add a new IATA-keyed airport entry.""" path = tmp_path / "airports.yaml" path.write_text("""LHR: iata: LHR name: Heathrow Airport city: London country: gb latitude: 51.47 longitude: -0.4543 qid: Q8691 """) replaced = build_place_yaml.upsert_airport( tmp_path, { "iata": "ORY", "name": "Paris Orly Airport", "city": "Paris Orly Airport", "country": "fr", "latitude": 48.723333, "longitude": 2.379444, "qid": "Q193353", }, ) airports = yaml.safe_load(path.read_text()) assert replaced is False assert list(airports) == ["LHR", "ORY"] assert airports["ORY"]["country"] == "fr"