from __future__ import annotations
from apps.parsers.clients.base import HTTPClientError
from apps.parsers.clients.common.structured import StructuredDataClient
from django.test import SimpleTestCase
class _FakeHTTPClient:
def __init__(self, responses: dict[str, bytes | Exception]) -> None:
self.responses = responses
self.downloaded_urls: list[str] = []
def download_file(self, endpoint: str, **_kwargs) -> bytes:
self.downloaded_urls.append(endpoint)
response = self.responses[endpoint]
if isinstance(response, Exception):
raise response
return response
class StructuredDataClientEisCardEnrichmentTest(SimpleTestCase):
def test_procurement_card_enriches_customer_identity_from_detail_page(self):
detail_url = (
"https://zakupki.gov.ru/epz/order/notice/ea20/view/"
"common-info.html?regNumber=0338100002026000022"
)
client = StructuredDataClient(source="procurements_44fz")
client._http_client = _FakeHTTPClient(
{
detail_url: """
Сведения о заказчике
Полное наименование
ФЕДЕРАЛЬНОЕ ГБУ НАУКИ
ИНН / КПП
4101020011 / 410101001
ОГРН
1024101023456
""".encode()
}
)
records = client.fetch_records(
content=f"""
№ 0338100002026000022
Работа комиссии
Объект закупки
Поставка оборудования
Заказчик
ФЕДЕРАЛЬНОЕ ГБУ НАУКИ
Начальная цена
1 000,00 ₽
Размещено
19.05.2026
""".encode(),
file_name="results.html",
)
self.assertEqual(len(records), 1)
record = records[0]
self.assertEqual(record.inn, "4101020011")
self.assertEqual(record.ogrn, "1024101023456")
self.assertEqual(record.payload["kpp"], "410101001")
self.assertEqual(record.payload["detail_url"], detail_url)
self.assertEqual(client._http_client.downloaded_urls, [detail_url])
def test_procurement_card_keeps_record_when_detail_page_is_unavailable(self):
detail_url = (
"https://zakupki.gov.ru/epz/order/notice/ea20/view/"
"common-info.html?regNumber=0338100002026000022"
)
client = StructuredDataClient(source="procurements_44fz")
client._http_client = _FakeHTTPClient(
{detail_url: HTTPClientError("detail unavailable", url=detail_url)}
)
records = client.fetch_records(
content=f"""
№ 0338100002026000022
Объект закупки
Поставка оборудования
Заказчик
ФЕДЕРАЛЬНОЕ ГБУ НАУКИ
""".encode(),
file_name="results.html",
)
self.assertEqual(len(records), 1)
self.assertEqual(records[0].inn, "")
self.assertEqual(records[0].ogrn, "")
self.assertEqual(records[0].organisation_name, "ФЕДЕРАЛЬНОЕ ГБУ НАУКИ")