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)