from unittest.mock import patch from apps.parsers.fedresurs_company_profile import FedresursUpstreamError from apps.parsers.fedresurs_snapshot import refresh_fedresurs_company_profiles from django.test import TestCase, override_settings from django.utils import timezone from organizations.models import Organization, OrganizationSourceRecord def profile(guid, inn, ogrn): return { "fedresurs_guid": guid, "source_url": f"https://fedresurs.ru/companies/{guid}", "profile": { "full_name": "АО Тест", "registration_date": "2020-01-01", "status": {"is_active": True}, "inn": inn, "ogrn": ogrn, }, "sections": {"bankruptcy": "empty"}, } @override_settings( FEDRESURS_COMPANY_PROFILE_ENABLED=True, FEDRESURS_MIN_INTERVAL_SECONDS=0, FEDRESURS_MAX_RETRIES=0, ) class FedresursSnapshotTest(TestCase): def setUp(self): self.first = Organization.objects.create( name="АО Первый", inn="6150061451", ogrn="1096100003625", okpo="00084008", directory_imported_at=timezone.now(), opk_registry_membership=True, ) self.second = Organization.objects.create( name="АО Второй", inn="1234567890", ogrn="1027700132195", okpo="00084009", directory_imported_at=timezone.now(), opk_registry_membership=True, ) @patch("apps.parsers.fedresurs_snapshot.public_browser") @patch("apps.parsers.fedresurs_snapshot.lookup_company") def test_complete_batch_publishes_and_targeted_refresh_preserves_other_records(self, lookup, browser): browser.return_value.__enter__.return_value = object() lookup.side_effect = lambda driver, *, inn, ogrn: ( "match", profile( "e1661e0e-d217-4598-9e70-a94f4d4bbe52" if inn == self.first.inn else "a1661e0e-d217-4598-9e70-a94f4d4bbe52", inn, ogrn, ) ) _, result = refresh_fedresurs_company_profiles(load_batch=1) self.assertEqual(result.published, 2) self.assertEqual(OrganizationSourceRecord.objects.filter(source="fedresurs_company_profile").count(), 2) lookup.return_value = ("no_match", None) lookup.side_effect = None _, targeted = refresh_fedresurs_company_profiles( load_batch=2, mode="targeted", organization_uids=[str(self.first.uid)] ) self.assertEqual(targeted.quarantined, 1) self.assertEqual(OrganizationSourceRecord.objects.filter(source="fedresurs_company_profile").count(), 1) @patch("apps.parsers.fedresurs_snapshot.public_browser") @patch("apps.parsers.fedresurs_snapshot.lookup_company") def test_technical_failure_keeps_last_published_snapshot(self, lookup, browser): browser.return_value.__enter__.return_value = object() lookup.side_effect = lambda driver, *, inn, ogrn: ( "match", profile( "e1661e0e-d217-4598-9e70-a94f4d4bbe52" if inn == self.first.inn else "a1661e0e-d217-4598-9e70-a94f4d4bbe52", inn, ogrn, ) ) refresh_fedresurs_company_profiles(load_batch=1) self.assertEqual(OrganizationSourceRecord.objects.filter(source="fedresurs_company_profile").count(), 2) lookup.side_effect = FedresursUpstreamError("upstream_challenge") with self.assertRaises(FedresursUpstreamError): refresh_fedresurs_company_profiles(load_batch=2) self.assertEqual(OrganizationSourceRecord.objects.filter(source="fedresurs_company_profile").count(), 2)