/
alsoalgo
/
CTF
Обзор
Документация
Войти
/
alsoalgo
/
CTF
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/test_source_tracker.py
200 строк
7 KB
alsoalgo
some improvements
14 дек 2025, 22:23
14 дек 2025, 22:23
37d2ec0
Код
Авторство
О чём код?
""" Тесты для SourceTracker """ import pytest from src.components.source_tracker import SourceTracker, SourceLocation class TestSourceLocation: """Тесты для SourceLocation""" def test_source_location_creation(self): """Тест создания SourceLocation""" location = SourceLocation( file_path="/path/to/file.pdf", file_name="file.pdf", page_number=1, coordinates={"x": 10, "y": 20, "width": 100, "height": 30}, context="Some context", extraction_method="ocr", confidence=0.95 ) assert location.file_path == "/path/to/file.pdf" assert location.file_name == "file.pdf" assert location.page_number == 1 assert location.coordinates == {"x": 10, "y": 20, "width": 100, "height": 30} assert location.context == "Some context" assert location.extraction_method == "ocr" assert location.confidence == 0.95 def test_source_location_defaults(self): """Тест SourceLocation с значениями по умолчанию""" location = SourceLocation( file_path="/path/to/file.pdf", file_name="file.pdf" ) assert location.page_number is None assert location.coordinates is None assert location.context is None assert location.extraction_method == "standard" assert location.confidence == 1.0 def test_source_location_to_dict(self): """Тест преобразования SourceLocation в словарь""" location = SourceLocation( file_path="/path/to/file.pdf", file_name="file.pdf", page_number=1 ) data = location.to_dict() assert isinstance(data, dict) assert data['file_path'] == "/path/to/file.pdf" assert data['file_name'] == "file.pdf" assert data['page_number'] == 1 def test_source_location_from_dict(self): """Тест создания SourceLocation из словаря""" data = { 'file_path': "/path/to/file.pdf", 'file_name': "file.pdf", 'page_number': 1, 'coordinates': None, 'context': None, 'extraction_method': "ocr", 'confidence': 0.9 } location = SourceLocation.from_dict(data) assert location.file_path == "/path/to/file.pdf" assert location.file_name == "file.pdf" assert location.page_number == 1 assert location.extraction_method == "ocr" assert location.confidence == 0.9 class TestSourceTracker: """Тесты для SourceTracker""" def setup_method(self): """Настройка перед каждым тестом""" self.tracker = SourceTracker() def test_tracker_initialization(self): """Тест инициализации SourceTracker""" tracker = SourceTracker() assert tracker.sources == {} def test_add_source(self): """Тест добавления источника""" self.tracker.add_source( key="supplier", file_path="/path/to/invoice.pdf", page_number=1, coordinates={"x": 10, "y": 20}, context="Supplier name", extraction_method="ocr", confidence=0.95 ) assert "supplier" in self.tracker.sources source = self.tracker.sources["supplier"] assert source.file_path == "/path/to/invoice.pdf" assert source.page_number == 1 assert source.extraction_method == "ocr" def test_add_source_minimal(self): """Тест добавления источника с минимальными данными""" self.tracker.add_source( key="field1", file_path="/path/to/file.pdf" ) assert "field1" in self.tracker.sources source = self.tracker.sources["field1"] assert source.file_name == "file.pdf" assert source.extraction_method == "standard" assert source.confidence == 1.0 def test_get_source_existing(self): """Тест получения существующего источника""" self.tracker.add_source( key="test_key", file_path="/path/to/file.pdf" ) source = self.tracker.get_source("test_key") assert source is not None assert source.file_path == "/path/to/file.pdf" def test_get_source_nonexistent(self): """Тест получения несуществующего источника""" source = self.tracker.get_source("nonexistent") assert source is None def test_get_all_sources(self): """Тест получения всех источников""" self.tracker.add_source("key1", "/path/to/file1.pdf") self.tracker.add_source("key2", "/path/to/file2.pdf") all_sources = self.tracker.get_all_sources() assert len(all_sources) == 2 assert "key1" in all_sources assert "key2" in all_sources # Проверяем, что это копия, а не ссылка all_sources["key3"] = "test" assert "key3" not in self.tracker.sources def test_to_dict(self): """Тест преобразования SourceTracker в словарь""" self.tracker.add_source( key="test_key", file_path="/path/to/file.pdf", page_number=1 ) data = self.tracker.to_dict() assert isinstance(data, dict) assert "test_key" in data assert data["test_key"]["file_path"] == "/path/to/file.pdf" assert data["test_key"]["page_number"] == 1 def test_from_dict(self): """Тест создания SourceTracker из словаря""" data = { "key1": { 'file_path': "/path/to/file1.pdf", 'file_name': "file1.pdf", 'page_number': 1, 'coordinates': None, 'context': None, 'extraction_method': "ocr", 'confidence': 0.9 }, "key2": { 'file_path': "/path/to/file2.pdf", 'file_name': "file2.pdf", 'page_number': None, 'coordinates': None, 'context': None, 'extraction_method': "standard", 'confidence': 1.0 } } tracker = SourceTracker.from_dict(data) assert len(tracker.sources) == 2 assert tracker.get_source("key1").file_path == "/path/to/file1.pdf" assert tracker.get_source("key2").file_path == "/path/to/file2.pdf" def test_multiple_sources(self): """Тест работы с несколькими источниками""" self.tracker.add_source("supplier", "/path/to/invoice.pdf", page_number=1) self.tracker.add_source("amount", "/path/to/invoice.pdf", page_number=1) self.tracker.add_source("date", "/path/to/contract.pdf", page_number=2) assert len(self.tracker.sources) == 3 assert self.tracker.get_source("supplier").file_path == "/path/to/invoice.pdf" assert self.tracker.get_source("date").file_path == "/path/to/contract.pdf"