feat(registry): add new endpoints for registers, exchange, and backups; update routing and configurations
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 3m10s
CI/CD Pipeline / Run Tests (push) Successful in 3m35s
CI/CD Pipeline / Telegram Notify Success (push) Has been skipped
CI/CD Pipeline / Code Quality Checks (pull_request) Failing after 2m26s
CI/CD Pipeline / Run Tests (pull_request) Successful in 2m46s
CI/CD Pipeline / Telegram Notify Success (pull_request) Has been skipped
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 3m10s
CI/CD Pipeline / Run Tests (push) Successful in 3m35s
CI/CD Pipeline / Telegram Notify Success (push) Has been skipped
CI/CD Pipeline / Code Quality Checks (pull_request) Failing after 2m26s
CI/CD Pipeline / Run Tests (pull_request) Successful in 2m46s
CI/CD Pipeline / Telegram Notify Success (pull_request) Has been skipped
This commit is contained in:
0
tests/apps/exchange/__init__.py
Normal file
0
tests/apps/exchange/__init__.py
Normal file
23
tests/apps/exchange/factories.py
Normal file
23
tests/apps/exchange/factories.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Фабрики для тестов exchange."""
|
||||
|
||||
import factory
|
||||
from apps.exchange.models import ExchangeConnection
|
||||
from faker import Faker
|
||||
|
||||
fake = Faker("ru_RU")
|
||||
|
||||
|
||||
class ExchangeConnectionFactory(factory.django.DjangoModelFactory):
|
||||
"""Фабрика для модели ExchangeConnection."""
|
||||
|
||||
class Meta:
|
||||
model = ExchangeConnection
|
||||
|
||||
server = factory.LazyAttribute(lambda _: fake.ipv4())
|
||||
port = 5432
|
||||
username = factory.LazyAttribute(lambda _: fake.user_name())
|
||||
password = factory.LazyAttribute(lambda _: fake.password())
|
||||
database_name = factory.Sequence(lambda n: f"target_db_{n + 1}")
|
||||
schema_name = "public"
|
||||
is_active = False
|
||||
last_error = ""
|
||||
25
tests/apps/exchange/test_services.py
Normal file
25
tests/apps/exchange/test_services.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""Tests for exchange services."""
|
||||
|
||||
from apps.exchange.services import ExchangeConnectionService
|
||||
from apps.parsers.models import IndustrialCertificateRecord, ParserLoadLog
|
||||
from apps.registers.models import Organization
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class ExchangeConnectionServiceDependenciesTest(TestCase):
|
||||
"""Tests for dependency expansion in copy operation."""
|
||||
|
||||
def test_extend_models_without_registry_links(self):
|
||||
models_to_copy = ExchangeConnectionService._extend_models_with_dependencies(
|
||||
[ParserLoadLog]
|
||||
)
|
||||
|
||||
self.assertEqual(models_to_copy, [ParserLoadLog])
|
||||
|
||||
def test_extend_models_adds_registers_organization_first(self):
|
||||
models_to_copy = ExchangeConnectionService._extend_models_with_dependencies(
|
||||
[IndustrialCertificateRecord]
|
||||
)
|
||||
|
||||
self.assertEqual(models_to_copy[0], Organization)
|
||||
self.assertEqual(models_to_copy[1], IndustrialCertificateRecord)
|
||||
122
tests/apps/exchange/test_views.py
Normal file
122
tests/apps/exchange/test_views.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""Tests for exchange API views."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from apps.exchange.models import ExchangeConnection
|
||||
from apps.exchange.services import ExchangeServiceError
|
||||
from django.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from tests.apps.exchange.factories import ExchangeConnectionFactory
|
||||
from tests.apps.user.factories import UserFactory
|
||||
|
||||
|
||||
class ExchangeViewsTest(APITestCase):
|
||||
def setUp(self):
|
||||
self.user = UserFactory.create_user()
|
||||
self.admin = UserFactory.create_superuser()
|
||||
self.connections_url = reverse("api_v1:exchange:connections")
|
||||
self.copy_url = reverse("api_v1:exchange:copy")
|
||||
|
||||
def test_connections_endpoint_admin_only(self):
|
||||
response = self.client.get(self.connections_url)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
self.client.force_authenticate(self.user)
|
||||
response = self.client.get(self.connections_url)
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.get(self.connections_url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(response.data["success"])
|
||||
self.assertIsInstance(response.data["data"], list)
|
||||
|
||||
@patch("apps.exchange.services.ExchangeConnectionService.validate_target_structure")
|
||||
@patch("apps.exchange.services.ExchangeConnectionService.test_connection")
|
||||
def test_create_connection_success(self, connection_mock, validate_mock):
|
||||
old_active = ExchangeConnectionFactory(is_active=True)
|
||||
|
||||
payload = {
|
||||
"server": "127.0.0.1",
|
||||
"port": 5432,
|
||||
"username": "postgres",
|
||||
"password": "secret",
|
||||
"database_name": "target_db",
|
||||
"schema_name": "public",
|
||||
}
|
||||
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.post(self.connections_url, payload, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(ExchangeConnection.objects.filter(is_active=True).count(), 1)
|
||||
|
||||
new_connection = ExchangeConnection.objects.get(id=response.data["data"]["id"])
|
||||
self.assertTrue(new_connection.is_active)
|
||||
|
||||
old_active.refresh_from_db()
|
||||
self.assertFalse(old_active.is_active)
|
||||
|
||||
connection_mock.assert_called_once()
|
||||
validate_mock.assert_called_once()
|
||||
|
||||
@patch("apps.exchange.services.ExchangeConnectionService.test_connection")
|
||||
def test_create_connection_fail_rolls_back_active(self, connection_mock):
|
||||
connection_mock.side_effect = ExchangeServiceError("Connection refused")
|
||||
|
||||
old_active = ExchangeConnectionFactory(is_active=True)
|
||||
|
||||
payload = {
|
||||
"server": "127.0.0.1",
|
||||
"port": 5432,
|
||||
"username": "postgres",
|
||||
"password": "secret",
|
||||
"database_name": "target_db",
|
||||
"schema_name": "public",
|
||||
}
|
||||
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.post(self.connections_url, payload, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertEqual(ExchangeConnection.objects.count(), 1)
|
||||
|
||||
old_active.refresh_from_db()
|
||||
self.assertTrue(old_active.is_active)
|
||||
|
||||
def test_copy_requires_active_connection(self):
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.post(self.copy_url, {"mode": "all"}, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@patch("apps.exchange.views.copy_parsers_data_async.delay")
|
||||
@patch("apps.exchange.services.ExchangeConnectionService.get_active_connection")
|
||||
def test_copy_all_success(self, get_active_mock, delay_mock):
|
||||
active_connection = ExchangeConnectionFactory(is_active=True)
|
||||
get_active_mock.return_value = active_connection
|
||||
delay_mock.return_value = SimpleNamespace(id="task-123")
|
||||
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.post(self.copy_url, {"mode": "all"}, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
self.assertEqual(response.data["data"]["status"], "started")
|
||||
self.assertEqual(response.data["data"]["task_id"], "task-123")
|
||||
self.assertEqual(response.data["data"]["connection_id"], active_connection.id)
|
||||
get_active_mock.assert_called_once()
|
||||
delay_mock.assert_called_once_with(
|
||||
connection_id=active_connection.id,
|
||||
payload={"mode": "all", "truncate_before_copy": True},
|
||||
requested_by_id=self.admin.id,
|
||||
)
|
||||
|
||||
def test_copy_single_requires_table(self):
|
||||
self.client.force_authenticate(self.admin)
|
||||
response = self.client.post(self.copy_url, {"mode": "single"}, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn("table", str(response.data))
|
||||
Reference in New Issue
Block a user