fix(lint): resolve ruff errors in tests and run_tests.py

- Fix import sorting (I001)
- Remove unused imports and variables (F401, F841)
- Add noqa for test code (S106 hardcoded passwords, S314 XML parsing)
- Auto-format with ruff format
This commit is contained in:
2026-02-02 12:44:37 +01:00
parent 97a7764155
commit 3f222a9141
12 changed files with 87 additions and 70 deletions

View File

@@ -3,10 +3,7 @@
import random
from datetime import timedelta
from django.utils import timezone
import factory
from apps.parsers.models import (
IndustrialCertificateRecord,
InspectionRecord,
@@ -14,6 +11,7 @@ from apps.parsers.models import (
ParserLoadLog,
Proxy,
)
from django.utils import timezone
# === Хелперы для генерации реалистичных данных ===
@@ -320,7 +318,9 @@ class InspectionRecordFactory(factory.django.DjangoModelFactory):
lambda _: random.choice(["плановая", "внеплановая"])
)
inspection_form = factory.LazyAttribute(
lambda _: random.choice(["документарная", "выездная", "документарная и выездная"])
lambda _: random.choice(
["документарная", "выездная", "документарная и выездная"]
)
)
start_date = factory.LazyAttribute(
lambda _: (timezone.now() - timedelta(days=random.randint(1, 180))).strftime(
@@ -339,9 +339,7 @@ class InspectionRecordFactory(factory.django.DjangoModelFactory):
lambda _: random.choice(["294-ФЗ", "248-ФЗ", "184-ФЗ"])
)
result = factory.LazyAttribute(
lambda _: random.choice(
["нарушения не выявлены", "выявлены нарушения", ""]
)
lambda _: random.choice(["нарушения не выявлены", "выявлены нарушения", ""])
if random.random() > 0.3
else ""
)

View File

@@ -3,17 +3,15 @@
from io import BytesIO
from unittest.mock import patch
from django.test import TestCase, tag
from faker import Faker
from openpyxl import Workbook
from apps.parsers.clients.base import BaseHTTPClient, HTTPClientError
from apps.parsers.clients.minpromtorg.industrial import IndustrialProductionClient
from apps.parsers.clients.minpromtorg.manufactures import ManufacturesClient
from apps.parsers.clients.minpromtorg.schemas import IndustrialCertificate, Manufacturer
from apps.parsers.clients.proverki import ProverkiClient
from apps.parsers.clients.proverki.schemas import Inspection
from django.test import TestCase, tag
from faker import Faker
from openpyxl import Workbook
fake = Faker("ru_RU")
@@ -159,7 +157,10 @@ class IndustrialProductionClientTest(TestCase):
{
"name": "Заключения о подтверждении производства промышленной продукции на территории Российской Федерации",
"files": [
{"name": "data_resolutions_20240101.xlsx", "url": "/files/test.xlsx"},
{
"name": "data_resolutions_20240101.xlsx",
"url": "/files/test.xlsx",
},
],
}
]
@@ -193,9 +194,18 @@ class IndustrialProductionClientTest(TestCase):
{
"name": "Заключения о подтверждении производства промышленной продукции на территории Российской Федерации",
"files": [
{"name": "data_resolutions_20240101.xlsx", "url": "/files/old.xlsx"},
{"name": "data_resolutions_20240315.xlsx", "url": "/files/new.xlsx"},
{"name": "data_resolutions_20240201.xlsx", "url": "/files/mid.xlsx"},
{
"name": "data_resolutions_20240101.xlsx",
"url": "/files/old.xlsx",
},
{
"name": "data_resolutions_20240315.xlsx",
"url": "/files/new.xlsx",
},
{
"name": "data_resolutions_20240201.xlsx",
"url": "/files/mid.xlsx",
},
],
}
]
@@ -539,7 +549,7 @@ class ProverkiClientTest(TestCase):
client = ProverkiClient()
xml_str = '<inspection inn="1234567890" registration_number="TEST123" organisation_name="Test Co"/>'
element = ET.fromstring(xml_str)
element = ET.fromstring(xml_str) # noqa: S314
result = client._parse_xml_record(element)
@@ -553,7 +563,7 @@ class ProverkiClientTest(TestCase):
client = ProverkiClient()
xml_str = "<empty_record></empty_record>"
element = ET.fromstring(xml_str)
element = ET.fromstring(xml_str) # noqa: S314
result = client._parse_xml_record(element)
@@ -569,9 +579,7 @@ class ProverkiClientTest(TestCase):
<registration_number>TEST001</registration_number>
<organisation_name>Компания</organisation_name>
</inspection>
</inspections>""".encode(
"windows-1251"
)
</inspections>""".encode("windows-1251")
inspections = client._parse_xml_content(xml_content, None)

View File

@@ -1,13 +1,12 @@
"""Tests for parsers models."""
from django.test import TestCase
from apps.parsers.models import (
IndustrialCertificateRecord,
ManufacturerRecord,
ParserLoadLog,
Proxy,
)
from django.test import TestCase
from .factories import (
IndustrialCertificateRecordFactory,

View File

@@ -1,9 +1,5 @@
"""Tests for parsers services."""
from django.test import TestCase
from faker import Faker
from apps.parsers.clients.minpromtorg.schemas import IndustrialCertificate, Manufacturer
from apps.parsers.clients.proverki.schemas import Inspection
from apps.parsers.models import (
@@ -20,6 +16,8 @@ from apps.parsers.services import (
ParserLoadLogService,
ProxyService,
)
from django.test import TestCase
from faker import Faker
from .factories import (
IndustrialCertificateRecordFactory,
@@ -147,7 +145,9 @@ class ParserLoadLogServiceTest(TestCase):
def test_get_next_batch_id_first(self):
"""Test getting first batch_id for new source."""
batch_id = ParserLoadLogService.get_next_batch_id(ParserLoadLog.Source.INDUSTRIAL)
batch_id = ParserLoadLogService.get_next_batch_id(
ParserLoadLog.Source.INDUSTRIAL
)
self.assertEqual(batch_id, 1)
def test_get_next_batch_id_increment(self):
@@ -155,7 +155,9 @@ class ParserLoadLogServiceTest(TestCase):
ParserLoadLogFactory(batch_id=5, source=ParserLoadLog.Source.INDUSTRIAL)
ParserLoadLogFactory(batch_id=3, source=ParserLoadLog.Source.INDUSTRIAL)
batch_id = ParserLoadLogService.get_next_batch_id(ParserLoadLog.Source.INDUSTRIAL)
batch_id = ParserLoadLogService.get_next_batch_id(
ParserLoadLog.Source.INDUSTRIAL
)
self.assertEqual(batch_id, 6)
def test_get_next_batch_id_per_source(self):
@@ -272,7 +274,9 @@ class IndustrialCertificateServiceTest(TestCase):
results = IndustrialCertificateService.find_by_inn("1111111111")
self.assertEqual(results.count(), 2)
results_batch1 = IndustrialCertificateService.find_by_inn("1111111111", batch_id=1)
results_batch1 = IndustrialCertificateService.find_by_inn(
"1111111111", batch_id=1
)
self.assertEqual(results_batch1.count(), 1)
def test_find_by_certificate_number(self):
@@ -313,7 +317,7 @@ class IndustrialCertificateServiceTest(TestCase):
ogrn="9999999999999",
)
]
count2 = IndustrialCertificateService.save_certificates(duplicate, batch_id=2)
IndustrialCertificateService.save_certificates(duplicate, batch_id=2)
# Should still be 1 record (duplicate skipped)
self.assertEqual(IndustrialCertificateRecord.objects.count(), 1)
@@ -420,7 +424,7 @@ class ManufacturerServiceTest(TestCase):
address="New Address",
)
]
count2 = ManufacturerService.save_manufacturers(duplicate, batch_id=2)
ManufacturerService.save_manufacturers(duplicate, batch_id=2)
# Should still be 1 record (duplicate skipped)
self.assertEqual(ManufacturerRecord.objects.count(), 1)
@@ -567,7 +571,7 @@ class InspectionServiceTest(TestCase):
result="выявлены нарушения",
)
]
count2 = InspectionService.save_inspections(duplicate, batch_id=2)
InspectionService.save_inspections(duplicate, batch_id=2)
# Should still be 1 record (duplicate skipped)
self.assertEqual(InspectionRecord.objects.count(), 1)
@@ -581,10 +585,9 @@ class InspectionServiceTest(TestCase):
self.assertEqual(record.load_batch, 1) # Original batch
from django.test import tag
from apps.parsers.clients.base import HTTPClientError
from apps.parsers.clients.minpromtorg.industrial import IndustrialProductionClient
from django.test import tag
@tag("integration", "slow", "network", "e2e")
@@ -674,4 +677,3 @@ class EndToEndIntegrationTest(TestCase):
except HTTPClientError as e:
self.skipTest(f"External API unavailable: {e}")