import zipfile from uuid import UUID import pytest from django.core.management import call_command from address.models import AddressObject, House ADDR_XML = """ """ HOUSE_XML = """ """ HIERARCHY_XML = """ """ def _gar_zip(tmp_path, *, extra: dict[str, str] | None = None): archive = tmp_path / "gar_xml.zip" files = { "25/AS_ADDR_OBJ_20260827_aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.XML": ADDR_XML, "25/AS_HOUSES_20260827_bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb.XML": HOUSE_XML, "25/AS_ADM_HIERARCHY_20260827_cccccccc-cccc-cccc-cccc-cccccccccccc.XML": HIERARCHY_XML, "25/AS_ADDR_OBJ_PARAMS_20260827_dddddddd-dddd-dddd-dddd-dddddddddddd.XML": "", "77/AS_ADDR_OBJ_20260827_eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee.XML": """ """, "77/AS_HOUSES_20260827_ffffffffffff-ffff-ffff-ffff-ffffffffffff.XML": "", "77/AS_ADM_HIERARCHY_20260827_00000000-0000-0000-0000-000000000000.XML": "", } if extra: files.update(extra) with zipfile.ZipFile(archive, "w") as zf: for name, content in files.items(): zf.writestr(name, content) return archive @pytest.mark.django_db def test_import_commands_read_zip_without_extracting(tmp_path): archive = _gar_zip(tmp_path) call_command("import_addresses", archive, region_code="25") call_command("import_houses", archive, region_code="25") call_command("import_hierarchy", archive, region_code="25") assert AddressObject.objects.count() == 3 city = AddressObject.objects.get(fias_id=UUID("11111111-1111-1111-1111-111111111111")) street = AddressObject.objects.get(name="Светланская") house = House.objects.get() assert city.region_code == 25 assert street.parent_id == city.pk assert house.house_number == "10" assert house.parent_id == street.pk extracted = [path for path in tmp_path.rglob("*") if path != archive and path.is_file()] assert extracted == [] @pytest.mark.django_db def test_import_gar_imports_all_regions_from_zip(tmp_path): archive = _gar_zip(tmp_path) call_command("import_gar", archive) assert AddressObject.objects.filter(region_code=25).count() == 3 assert AddressObject.objects.filter(name="Москва").exists() house = House.objects.get() street = AddressObject.objects.get(name="Светланская") assert house.parent_id == street.pk @pytest.mark.django_db def test_import_gar_can_limit_to_one_region(tmp_path): archive = _gar_zip(tmp_path) call_command("import_gar", archive, region_codes=["25"]) assert AddressObject.objects.filter(name="Москва").exists() is False assert AddressObject.objects.count() == 3 assert House.objects.count() == 1 @pytest.mark.django_db def test_import_gar_accepts_several_region_codes(tmp_path): archive = _gar_zip(tmp_path) call_command("import_gar", archive, region_codes=["25", "77"]) assert AddressObject.objects.filter(region_code=25).count() == 3 assert AddressObject.objects.filter(name="Москва").exists() assert House.objects.count() == 1 @pytest.mark.django_db def test_import_gar_can_import_houses_only_for_selected_regions(tmp_path): archive = _gar_zip(tmp_path) call_command("import_gar", archive, houses_region_codes=["25"]) assert AddressObject.objects.filter(name="Москва").exists() assert AddressObject.objects.filter(region_code=25).count() == 3 house = House.objects.get() assert house.region_code == 25 assert house.parent_id == AddressObject.objects.get(name="Светланская").pk