Some checks failed
CI/CD Pipeline / Code Quality Checks (pull_request) Failing after 10m20s
CI/CD Pipeline / Run Tests (pull_request) Failing after 11m5s
CI/CD Pipeline / Build Docker Images (pull_request) Has been skipped
CI/CD Pipeline / Push to Gitea Registry (pull_request) Has been skipped
166 lines
5.5 KiB
Python
166 lines
5.5 KiB
Python
"""Tests for parsers models."""
|
||
|
||
from apps.parsers.models import (
|
||
GenericParserRecord,
|
||
IndustrialCertificateRecord,
|
||
ManufacturerRecord,
|
||
ParserLoadLog,
|
||
Proxy,
|
||
)
|
||
from django.test import TestCase
|
||
|
||
from .factories import (
|
||
GenericParserRecordFactory,
|
||
IndustrialCertificateRecordFactory,
|
||
ManufacturerRecordFactory,
|
||
ParserLoadLogFactory,
|
||
ProxyFactory,
|
||
)
|
||
|
||
|
||
class ProxyModelTest(TestCase):
|
||
"""Tests for Proxy model."""
|
||
|
||
def test_create_proxy(self):
|
||
"""Test creating proxy record."""
|
||
proxy = ProxyFactory()
|
||
|
||
self.assertIsInstance(proxy, Proxy)
|
||
self.assertIn("://", proxy.address) # http://, https://, socks5://
|
||
self.assertTrue(proxy.is_active)
|
||
self.assertEqual(proxy.fail_count, 0)
|
||
|
||
def test_proxy_str(self):
|
||
"""Test proxy string representation."""
|
||
proxy = ProxyFactory(address="http://proxy:8080", is_active=True)
|
||
self.assertEqual(str(proxy), "http://proxy:8080 (active)")
|
||
|
||
proxy.is_active = False
|
||
self.assertEqual(str(proxy), "http://proxy:8080 (inactive)")
|
||
|
||
def test_proxy_ordering(self):
|
||
"""Test proxy ordering by fail_count."""
|
||
proxy1 = ProxyFactory(fail_count=5)
|
||
proxy2 = ProxyFactory(fail_count=0)
|
||
proxy3 = ProxyFactory(fail_count=10)
|
||
|
||
proxies = list(Proxy.objects.all())
|
||
self.assertEqual(proxies[0], proxy2) # fail_count=0
|
||
self.assertEqual(proxies[1], proxy1) # fail_count=5
|
||
self.assertEqual(proxies[2], proxy3) # fail_count=10
|
||
|
||
def test_proxy_unique_address(self):
|
||
"""Test proxy address uniqueness."""
|
||
ProxyFactory(address="http://unique:8080")
|
||
|
||
from django.db import IntegrityError
|
||
|
||
with self.assertRaises(IntegrityError):
|
||
ProxyFactory(address="http://unique:8080")
|
||
|
||
|
||
class ParserLoadLogModelTest(TestCase):
|
||
"""Tests for ParserLoadLog model."""
|
||
|
||
def test_create_load_log(self):
|
||
"""Test creating load log record."""
|
||
log = ParserLoadLogFactory()
|
||
|
||
self.assertIsInstance(log, ParserLoadLog)
|
||
self.assertIsNotNone(log.batch_id)
|
||
self.assertIn(log.source, [c[0] for c in ParserLoadLog.Source.choices])
|
||
|
||
def test_load_log_str(self):
|
||
"""Test load log string representation."""
|
||
log = ParserLoadLogFactory(
|
||
batch_id=42, source=ParserLoadLog.Source.INDUSTRIAL, records_count=100
|
||
)
|
||
self.assertIn("42", str(log))
|
||
self.assertIn("100", str(log))
|
||
|
||
def test_load_log_timestamps(self):
|
||
"""Test load log has timestamps from mixin."""
|
||
log = ParserLoadLogFactory()
|
||
|
||
self.assertIsNotNone(log.created_at)
|
||
self.assertIsNotNone(log.updated_at)
|
||
|
||
|
||
class IndustrialCertificateRecordModelTest(TestCase):
|
||
"""Tests for IndustrialCertificateRecord model."""
|
||
|
||
def test_create_certificate(self):
|
||
"""Test creating certificate record."""
|
||
cert = IndustrialCertificateRecordFactory()
|
||
|
||
self.assertIsInstance(cert, IndustrialCertificateRecord)
|
||
self.assertIsNotNone(cert.certificate_number)
|
||
self.assertIsNotNone(cert.inn)
|
||
self.assertIsNotNone(cert.ogrn)
|
||
|
||
def test_certificate_str(self):
|
||
"""Test certificate string representation."""
|
||
cert = IndustrialCertificateRecordFactory(
|
||
certificate_number="CERT-123", organisation_name="Test Company LLC"
|
||
)
|
||
self.assertIn("CERT-123", str(cert))
|
||
self.assertIn("Test Company", str(cert))
|
||
|
||
def test_certificate_timestamps(self):
|
||
"""Test certificate has timestamps from mixin."""
|
||
cert = IndustrialCertificateRecordFactory()
|
||
|
||
self.assertIsNotNone(cert.created_at)
|
||
self.assertIsNotNone(cert.updated_at)
|
||
|
||
|
||
class ManufacturerRecordModelTest(TestCase):
|
||
"""Tests for ManufacturerRecord model."""
|
||
|
||
def test_create_manufacturer(self):
|
||
"""Test creating manufacturer record."""
|
||
manufacturer = ManufacturerRecordFactory()
|
||
|
||
self.assertIsInstance(manufacturer, ManufacturerRecord)
|
||
self.assertIsNotNone(manufacturer.full_legal_name)
|
||
self.assertIsNotNone(manufacturer.inn)
|
||
self.assertIsNotNone(manufacturer.ogrn)
|
||
|
||
def test_manufacturer_str(self):
|
||
"""Test manufacturer string representation."""
|
||
manufacturer = ManufacturerRecordFactory(
|
||
inn="1234567890", full_legal_name="Test Manufacturing Company"
|
||
)
|
||
self.assertIn("1234567890", str(manufacturer))
|
||
self.assertIn("Test Manufacturing", str(manufacturer))
|
||
|
||
def test_manufacturer_timestamps(self):
|
||
"""Test manufacturer has timestamps from mixin."""
|
||
manufacturer = ManufacturerRecordFactory()
|
||
|
||
self.assertIsNotNone(manufacturer.created_at)
|
||
self.assertIsNotNone(manufacturer.updated_at)
|
||
|
||
|
||
class GenericParserRecordModelTest(TestCase):
|
||
"""Tests for GenericParserRecord model."""
|
||
|
||
def test_create_generic_record(self):
|
||
"""Test creating generic parser record."""
|
||
record = GenericParserRecordFactory()
|
||
|
||
self.assertIsInstance(record, GenericParserRecord)
|
||
self.assertEqual(record.source, ParserLoadLog.Source.FNS_FINANCIAL)
|
||
self.assertIsNotNone(record.external_id)
|
||
self.assertIsInstance(record.payload, dict)
|
||
|
||
def test_generic_record_str(self):
|
||
"""Test generic record string representation."""
|
||
record = GenericParserRecordFactory(
|
||
source=ParserLoadLog.Source.TRUDVSEM,
|
||
organisation_name='ООО "Тест"',
|
||
)
|
||
|
||
self.assertIn(ParserLoadLog.Source.TRUDVSEM, str(record))
|
||
self.assertIn("Тест", str(record))
|