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.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.additional_number_1 is None
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_skips_non_residential_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)
house = House.objects.get()
assert house.house_number == "10"
assert house.house_type == House.Type.HOUSE
@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А"