from uuid import UUID import pytest from django.core.management import call_command from django.core.management.base import CommandError from address.models import House @pytest.mark.django_db def test_imports_and_updates_houses_xml(tmp_path): region_dir = tmp_path / "25" region_dir.mkdir() xml = region_dir / "AS_HOUSES.xml" xml.write_text( """ """, encoding="utf-8", ) call_command("import_houses", xml, batch_size=1) house = House.objects.get(fias_id=UUID("33333333-3333-3333-3333-333333333333")) assert house.object_id == 500 assert house.region_code == 25 assert house.house_type == House.Type.HOUSE assert house.house_number == "д. 10 к. 3" assert house.display_number == "д. 10 к. 3" assert House.objects.count() == 1 xml.write_text( """ """, encoding="utf-8", ) call_command("import_houses", xml) house.refresh_from_db() assert house.house_number == "д. 10А" assert house.display_number == "д. 10А" assert House.objects.count() == 1 @pytest.mark.django_db def test_rejects_unknown_house_type(tmp_path): xml = tmp_path / "houses.xml" xml.write_text( """ """, encoding="utf-8", ) with pytest.raises(CommandError, match="HOUSETYPE=99"): call_command("import_houses", xml, region_code="25") @pytest.mark.django_db def test_imports_all_house_types(tmp_path): region_dir = tmp_path / "25" region_dir.mkdir() xml = region_dir / "AS_HOUSES.xml" xml.write_text( """ """, encoding="utf-8", ) call_command("import_houses", xml) assert House.objects.count() == 10 assert set(House.objects.values_list("house_type", flat=True)) == { House.Type.HOUSE, House.Type.GARAGE, House.Type.MINE, House.Type.BASEMENT, House.Type.BOILER_HOUSE, House.Type.CELLAR, House.Type.FACILITY, House.Type.UNFINISHED, House.Type.BUILDING, House.Type.STRUCTURE, } @pytest.mark.django_db def test_keeps_last_house_when_fias_id_repeats_in_one_batch(tmp_path): region_dir = tmp_path / "25" region_dir.mkdir() xml = region_dir / "AS_HOUSES.xml" xml.write_text( """ """, encoding="utf-8", ) call_command("import_houses", xml, batch_size=10) house = House.objects.get(fias_id=UUID("33333333-3333-3333-3333-333333333333")) assert House.objects.count() == 1 assert house.house_number == "д. 10А"