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_imports_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() == 3
assert AddressObject.objects.get(name="Мотор").level == 8
assert AddressObject.objects.get(name="1").level == 7
@pytest.mark.django_db
def test_imports_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)
marker = AddressObject.objects.get(name="104-й")
assert marker.level == 8
assert marker.type_name == "км"
@pytest.mark.django_db
def test_imports_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() == 6
assert AddressObject.objects.get(name="Арсеньевский").level == 3
assert AddressObject.objects.get(name="Сальское").level == 4
@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_imports_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)
assert AddressObject.objects.count() == 10
assert AddressObject.objects.get(name="Раздольное").level == 6
assert AddressObject.objects.get(name="Приморский").level == 8
assert AddressObject.objects.get(name="Аяно-Майская").level == 4
@pytest.mark.django_db
def test_imports_street_level_poselok_with_fias_level(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.get(name="Западный").level == 6
ajax = AddressObject.objects.get(name="Аякс")
assert ajax.level == 8
assert ajax.type_name == "п."