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 AddressObject
|
||
|
||
|
||
def _object(*, object_id: int, name: str = "Владивосток") -> AddressObject:
|
||
return AddressObject.objects.create(
|
||
fias_id=uuid4(),
|
||
object_id=object_id,
|
||
name=name,
|
||
type_name="г",
|
||
level=5,
|
||
region_code="25",
|
||
)
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_fills_kladr_id(tmp_path):
|
||
region_dir = tmp_path / "25"
|
||
region_dir.mkdir()
|
||
city = _object(object_id=100)
|
||
other = _object(object_id=101, name="Артем")
|
||
xml = region_dir / "AS_ADDR_OBJ_PARAMS.xml"
|
||
xml.write_text(
|
||
"""<PARAMS>
|
||
<PARAM OBJECTID="100" TYPEID="10" VALUE="2500000100000" CHANGEIDEND="0" />
|
||
<PARAM OBJECTID="100" TYPEID="10" VALUE="250000010000099" CHANGEIDEND="123" />
|
||
<PARAM OBJECTID="100" TYPEID="11" VALUE="2500000100000" CHANGEIDEND="0" />
|
||
<PARAM OBJECTID="101" TYPEID="10" VALUE="abc" CHANGEIDEND="0" />
|
||
<PARAM OBJECTID="999" TYPEID="10" VALUE="2500000200000" CHANGEIDEND="0" />
|
||
</PARAMS>""",
|
||
encoding="utf-8",
|
||
)
|
||
|
||
call_command("import_kladr", xml)
|
||
|
||
city.refresh_from_db()
|
||
other.refresh_from_db()
|
||
assert city.kladr_id == "2500000100000"
|
||
assert other.kladr_id == ""
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_keeps_last_current_kladr_id(tmp_path):
|
||
region_dir = tmp_path / "25"
|
||
region_dir.mkdir()
|
||
city = _object(object_id=100)
|
||
xml = region_dir / "AS_ADDR_OBJ_PARAMS.xml"
|
||
xml.write_text(
|
||
"""<PARAMS>
|
||
<PARAM OBJECTID="100" TYPEID="10" VALUE="2500000100000" CHANGEIDEND="0" />
|
||
<PARAM OBJECTID="100" TYPEID="10" VALUE="25000001000001700" CHANGEIDEND="0" />
|
||
</PARAMS>""",
|
||
encoding="utf-8",
|
||
)
|
||
|
||
call_command("import_kladr", xml)
|
||
|
||
city.refresh_from_db()
|
||
assert city.kladr_id == "25000001000001700"
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_kladr_file_must_exist(tmp_path):
|
||
with pytest.raises(CommandError, match="Файл не найден"):
|
||
call_command("import_kladr", tmp_path / "missing.xml")
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_fills_kladr_id_from_zip(tmp_path):
|
||
city = _object(object_id=100)
|
||
archive = tmp_path / "gar_xml.zip"
|
||
with zipfile.ZipFile(archive, "w") as zf:
|
||
zf.writestr(
|
||
"25/AS_ADDR_OBJ_PARAMS_20260827_bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb.XML",
|
||
"""<PARAMS>
|
||
<PARAM OBJECTID="100" TYPEID="10" VALUE="2500000100000" CHANGEIDEND="0" />
|
||
</PARAMS>""",
|
||
)
|
||
|
||
call_command("import_kladr", archive, region_code="25")
|
||
|
||
city.refresh_from_db()
|
||
assert city.kladr_id == "2500000100000"
|