from uuid import UUID
import pytest
from django.core.management import call_command
from django.core.management.base import CommandError
from address.models import AddressObject
@pytest.mark.django_db
def test_imports_and_updates_fias_xml(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml, batch_size=1)
city = AddressObject.objects.get(fias_id=UUID("11111111-1111-1111-1111-111111111111"))
assert city.object_id == 100
assert city.name == "Владивосток"
assert city.region_code == 25
assert city.is_active is True
assert AddressObject.objects.count() == 1
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
city.refresh_from_db()
assert city.name == "Владивосток обновлённый"
assert AddressObject.objects.count() == 1
@pytest.mark.django_db
def test_keeps_last_record_when_fias_id_repeats_in_one_batch(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml, batch_size=10)
city = AddressObject.objects.get(fias_id=UUID("11111111-1111-1111-1111-111111111111"))
assert AddressObject.objects.count() == 1
assert city.name == "Владивосток"
assert city.is_active is True
@pytest.mark.django_db
def test_region_code_is_required_when_it_cannot_be_inferred(tmp_path):
xml = tmp_path / "addresses.xml"
xml.write_text("", encoding="utf-8")
with pytest.raises(CommandError, match="--region-code"):
call_command("import_addresses", xml)
@pytest.mark.django_db
def test_skips_inactive_objects(tmp_path):
region_dir = tmp_path / "27"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
city = AddressObject.objects.get()
assert city.name == "Хабаровск"
assert city.is_active is True
@pytest.mark.django_db
def test_skips_garage_cooperative_types(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
assert AddressObject.objects.count() == 1
assert AddressObject.objects.get().name == "Владивосток"
@pytest.mark.django_db
def test_skips_kilometer_markers(tmp_path):
region_dir = tmp_path / "27"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
assert AddressObject.objects.count() == 1
assert AddressObject.objects.get().name == "Хабаровск"
@pytest.mark.django_db
def test_skips_urban_and_municipal_okrugs(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
assert AddressObject.objects.count() == 1
assert AddressObject.objects.get().name == "Владивосток"
@pytest.mark.django_db
def test_imports_chuvashia_as_region(tmp_path):
region_dir = tmp_path / "21"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
subject = AddressObject.objects.get()
assert subject.level == AddressObject.Level.REGION
assert subject.type_name == "Чувашия"
assert subject.region_code == 21
@pytest.mark.django_db
def test_imports_snt_as_planning_structure(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
snt = AddressObject.objects.get(name="Абрикос")
territory = AddressObject.objects.get(name="Рассвет")
assert snt.level == AddressObject.Level.PLANNING
assert snt.type_name == "снт"
assert territory.level == AddressObject.Level.PLANNING
assert territory.type_name == "тер. СНТ"
@pytest.mark.django_db
def test_skips_non_residential_and_railway_types(tmp_path):
region_dir = tmp_path / "25"
region_dir.mkdir()
xml = region_dir / "AS_ADDR_OBJ.xml"
xml.write_text(
"""
""",
encoding="utf-8",
)
call_command("import_addresses", xml)
names = set(AddressObject.objects.values_list("name", flat=True))
assert names == {"Владивосток", "Вокзальная", "Южный"}