90 lines
2.6 KiB
Python
90 lines
2.6 KiB
Python
import zipfile
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from django.core.management import call_command
|
|
from django.core.management.base import CommandError
|
|
|
|
from address.models import House
|
|
|
|
|
|
def _house(*, object_id: int, number: str = "10") -> House:
|
|
return House.objects.create(
|
|
object_id=object_id,
|
|
fias_id=uuid4(),
|
|
region_code="25",
|
|
house_number=number,
|
|
house_type=House.Type.HOUSE,
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_fills_house_postal_codes(tmp_path):
|
|
region_dir = tmp_path / "25"
|
|
region_dir.mkdir()
|
|
house = _house(object_id=500)
|
|
other = _house(object_id=501, number="11")
|
|
xml = region_dir / "AS_HOUSES_PARAMS.xml"
|
|
xml.write_text(
|
|
"""<PARAMS>
|
|
<PARAM OBJECTID="500" TYPEID="5" VALUE="690000" CHANGEIDEND="0" />
|
|
<PARAM OBJECTID="500" TYPEID="5" VALUE="690001" CHANGEIDEND="123" />
|
|
<PARAM OBJECTID="500" TYPEID="8" VALUE="25:01:000001:1" CHANGEIDEND="0" />
|
|
<PARAM OBJECTID="501" TYPEID="5" VALUE="abc" CHANGEIDEND="0" />
|
|
<PARAM OBJECTID="100" TYPEID="5" VALUE="690002" CHANGEIDEND="0" />
|
|
<PARAM OBJECTID="999" TYPEID="5" VALUE="690003" CHANGEIDEND="0" />
|
|
</PARAMS>""",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
call_command("import_postal_codes", xml)
|
|
|
|
house.refresh_from_db()
|
|
other.refresh_from_db()
|
|
assert house.postal_code == 690000
|
|
assert other.postal_code is None
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_keeps_last_current_postal_code(tmp_path):
|
|
region_dir = tmp_path / "25"
|
|
region_dir.mkdir()
|
|
house = _house(object_id=500)
|
|
xml = region_dir / "AS_HOUSES_PARAMS.xml"
|
|
xml.write_text(
|
|
"""<PARAMS>
|
|
<PARAM OBJECTID="500" TYPEID="5" VALUE="690000" CHANGEIDEND="0" />
|
|
<PARAM OBJECTID="500" TYPEID="5" VALUE="690091" CHANGEIDEND="0" />
|
|
</PARAMS>""",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
call_command("import_postal_codes", xml)
|
|
|
|
house.refresh_from_db()
|
|
assert house.postal_code == 690091
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_postal_codes_file_must_exist(tmp_path):
|
|
with pytest.raises(CommandError, match="Файл не найден"):
|
|
call_command("import_postal_codes", tmp_path / "missing.xml")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_fills_house_postal_codes_from_zip(tmp_path):
|
|
house = _house(object_id=500)
|
|
archive = tmp_path / "gar_xml.zip"
|
|
with zipfile.ZipFile(archive, "w") as zf:
|
|
zf.writestr(
|
|
"25/AS_HOUSES_PARAMS_20260827_dddddddd-dddd-dddd-dddd-dddddddddddd.XML",
|
|
"""<PARAMS>
|
|
<PARAM OBJECTID="500" TYPEID="5" VALUE="690000" CHANGEIDEND="0" />
|
|
</PARAMS>""",
|
|
)
|
|
|
|
call_command("import_postal_codes", archive, region_code="25")
|
|
|
|
house.refresh_from_db()
|
|
assert house.postal_code == 690000
|