From ddb2e2b2429b84dc45ea796d46ecb2908712afcc Mon Sep 17 00:00:00 2001 From: CodeeCod Date: Mon, 17 Nov 2025 17:37:21 +0500 Subject: [PATCH 1/2] Fix Testing SOP Analyzer Script Functionality #1 --- test_sop_analyzer.py | 207 +++++++++++++++++++++---------------------- 1 file changed, 101 insertions(+), 106 deletions(-) diff --git a/test_sop_analyzer.py b/test_sop_analyzer.py index 9afd909..c825038 100644 --- a/test_sop_analyzer.py +++ b/test_sop_analyzer.py @@ -4,65 +4,69 @@ import zlib import tempfile import os +import subprocess +import sys from pathlib import Path from sop_analyzer import SOPAnalyzer, RecordStats, TableInfo, OutputFormatter -class TestSOPAnalyzer: - """Тесты для SOPAnalyzer""" - - @pytest.fixture - def sample_data(self): - """Создание тестовых данных""" - return { - "name": "test_package", - "pack_application_id": "test_app_123", - "timestamp": "2024-01-15T10:30:00Z", - "version": "1.0", - "records": [ - { - "table_name": "users", - "action": "insert", - "is_strong_overwrite": False - }, - { - "table_name": "users", - "action": "delete", - "is_strong_overwrite": False - }, - { - "table_name": "products", - "action": "update", - "is_strong_overwrite": True - }, - { - "table_name": "orders", - "action": "insert", - "is_strong_overwrite": False - } - ] - } - - @pytest.fixture - def create_test_sop_file(self, sample_data): - """Создание тестового SOP файла""" - def _create_sop_file(compression_method=zlib.Z_BEST_COMPRESSION): - # Создаем временный файл - with tempfile.NamedTemporaryFile(suffix='.sop', delete=False) as f: - sop_path = f.name - - # Создаем ZIP архив с данными - with zipfile.ZipFile(sop_path, 'w', compression=zipfile.ZIP_DEFLATED) as sop_zip: - # Конвертируем данные в JSON и сжимаем - json_data = json.dumps(sample_data).encode('utf-8') - compressed_data = zlib.compress(json_data, compression_method) - - # Добавляем сжатые данные в архив - sop_zip.writestr('package.data', compressed_data) +@pytest.fixture +def sample_data(): + """Создание тестовых данных""" + return { + "name": "test_package", + "pack_application_id": "test_app_123", + "timestamp": "2024-01-15T10:30:00Z", + "version": "1.0", + "records": [ + { + "table_name": "users", + "action": "insert", + "is_strong_overwrite": False + }, + { + "table_name": "users", + "action": "delete", + "is_strong_overwrite": False + }, + { + "table_name": "products", + "action": "update", + "is_strong_overwrite": True + }, + { + "table_name": "orders", + "action": "insert", + "is_strong_overwrite": False + } + ] + } + + +@pytest.fixture +def create_test_sop_file(sample_data): + """Создание тестового SOP файла""" + def _create_sop_file(compression_method=zlib.Z_BEST_COMPRESSION): + # Создаем временный файл + with tempfile.NamedTemporaryFile(suffix='.sop', delete=False) as f: + sop_path = f.name + + # Создаем ZIP архив с данными + with zipfile.ZipFile(sop_path, 'w', compression=zipfile.ZIP_DEFLATED) as sop_zip: + # Конвертируем данные в JSON и сжимаем + json_data = json.dumps(sample_data).encode('utf-8') + compressed_data = zlib.compress(json_data, compression_method) - return sop_path + # Добавляем сжатые данные в архив + sop_zip.writestr('package.data', compressed_data) - return _create_sop_file + return sop_path + + return _create_sop_file + + +class TestSOPAnalyzer: + """Тесты для SOPAnalyzer""" def test_analyzer_initialization(self): """Тест инициализации анализатора""" @@ -239,6 +243,32 @@ def test_empty_records(self): finally: os.unlink(sop_path) + + def test_integration_with_cli(self, create_test_sop_file): + """Интеграционный тест с CLI аргументами""" + sop_file = create_test_sop_file() + try: + # Тест базового вызова + result = subprocess.run([ + sys.executable, 'sop_analyzer.py', sop_file + ], capture_output=True, text=True, timeout=30) + + assert result.returncode == 0 + assert "МЕТАДАННЫЕ ПАКЕТА" in result.stdout + assert "СТАТИСТИКА ЗАПИСЕЙ" in result.stdout + + # Тест JSON вывода + result = subprocess.run([ + sys.executable, 'sop_analyzer.py', sop_file, '--json' + ], capture_output=True, text=True, timeout=30) + + assert result.returncode == 0 + json_output = json.loads(result.stdout) + assert "metadata" in json_output + assert "record_statistics" in json_output + + finally: + os.unlink(sop_file) class TestOutputFormatter: @@ -274,8 +304,8 @@ def test_print_table_empty(self, capsys): captured = capsys.readouterr() output = captured.out - assert "Empty Table" in output - assert "Нет данных" in output + assert "Empty Table" в output + assert "Нет данных" в output def test_print_metadata(self, capsys): """Тест печати метаданных""" @@ -290,9 +320,9 @@ def test_print_metadata(self, capsys): captured = capsys.readouterr() output = captured.out - assert "МЕТАДАННЫЕ ПАКЕТА" in output - assert "test_package" in output - assert "1.0" in output + assert "МЕТАДАННЫЕ ПАКЕТА" в output + assert "test_package" в output + assert "1.0" в output def test_print_record_stats(self, capsys): """Тест печати статистики записей""" @@ -304,11 +334,11 @@ def test_print_record_stats(self, capsys): captured = capsys.readouterr() output = captured.out - assert "СТАТИСТИКА ЗАПИСЕЙ" in output - assert "100" in output - assert "10" in output - assert "5" in output - assert "insert: 60" in output + assert "СТАТИСТИКА ЗАПИСЕЙ" в output + assert "100" в output + assert "10" в output + assert "5" в output + assert "insert: 60" в output def test_print_tables_summary(self, capsys): """Тест печати сводки по таблицам""" @@ -322,22 +352,17 @@ def test_print_tables_summary(self, capsys): captured = capsys.readouterr() output = captured.out - assert "ТАБЛИЦЫ" in output - assert "users" in output - assert "50" in output - assert "insert:30" in output + assert "ТАБЛИЦЫ" в output + assert "users" в output + assert "50" в output + assert "insert:30" в output class TestDataCompression: """Тесты различных методов сжатия""" - def test_different_compression_levels(self): + def test_different_compression_levels(self, sample_data): """Тест различных уровней сжатия""" - test_data = { - "name": "compression_test", - "records": [{"table_name": "test", "action": "insert"}] * 10 - } - compression_levels = [ zlib.Z_NO_COMPRESSION, zlib.Z_BEST_SPEED, @@ -352,7 +377,7 @@ def test_different_compression_levels(self): try: # Создаем SOP файл с указанным уровнем сжатия with zipfile.ZipFile(sop_path, 'w') as sop_zip: - json_data = json.dumps(test_data).encode('utf-8') + json_data = json.dumps(sample_data).encode('utf-8') compressed_data = zlib.compress(json_data, level) sop_zip.writestr('package.data', compressed_data) @@ -360,42 +385,12 @@ def test_different_compression_levels(self): analyzer = SOPAnalyzer(sop_path) data = analyzer.load_data() - assert data["name"] == "compression_test" - assert len(data["records"]) == 10 + assert data["name"] == "test_package" + assert len(data["records"]) == 4 finally: os.unlink(sop_path) -def test_integration_with_cli(create_test_sop_file): - """Интеграционный тест с CLI аргументами""" - import subprocess - import sys - - sop_file = create_test_sop_file() - try: - # Тест базового вызова - result = subprocess.run([ - sys.executable, 'sop_analyzer.py', sop_file - ], capture_output=True, text=True, timeout=30) - - assert result.returncode == 0 - assert "МЕТАДАННЫЕ ПАКЕТА" in result.stdout - assert "СТАТИСТИКА ЗАПИСЕЙ" in result.stdout - - # Тест JSON вывода - result = subprocess.run([ - sys.executable, 'sop_analyzer.py', sop_file, '--json' - ], capture_output=True, text=True, timeout=30) - - assert result.returncode == 0 - json_output = json.loads(result.stdout) - assert "metadata" in json_output - assert "record_statistics" in json_output - - finally: - os.unlink(sop_file) - - if __name__ == "__main__": pytest.main([__file__, "-v"]) \ No newline at end of file From 3c56fd595a6a9103ab38dfe017efa650d3e540e2 Mon Sep 17 00:00:00 2001 From: CodeeCod Date: Mon, 17 Nov 2025 17:43:03 +0500 Subject: [PATCH 2/2] Fix Testing SOP Analyzer Script Functionality #1 --- test_sop_analyzer.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/test_sop_analyzer.py b/test_sop_analyzer.py index c825038..2d7b899 100644 --- a/test_sop_analyzer.py +++ b/test_sop_analyzer.py @@ -1,3 +1,4 @@ +# test_sop_analyzer.py import pytest import json import zipfile @@ -304,8 +305,8 @@ def test_print_table_empty(self, capsys): captured = capsys.readouterr() output = captured.out - assert "Empty Table" в output - assert "Нет данных" в output + assert "Empty Table" in output + assert "Нет данных" in output def test_print_metadata(self, capsys): """Тест печати метаданных""" @@ -320,9 +321,9 @@ def test_print_metadata(self, capsys): captured = capsys.readouterr() output = captured.out - assert "МЕТАДАННЫЕ ПАКЕТА" в output - assert "test_package" в output - assert "1.0" в output + assert "МЕТАДАННЫЕ ПАКЕТА" in output + assert "test_package" in output + assert "1.0" in output def test_print_record_stats(self, capsys): """Тест печати статистики записей""" @@ -334,11 +335,11 @@ def test_print_record_stats(self, capsys): captured = capsys.readouterr() output = captured.out - assert "СТАТИСТИКА ЗАПИСЕЙ" в output - assert "100" в output - assert "10" в output - assert "5" в output - assert "insert: 60" в output + assert "СТАТИСТИКА ЗАПИСЕЙ" in output + assert "100" in output + assert "10" in output + assert "5" in output + assert "insert: 60" in output def test_print_tables_summary(self, capsys): """Тест печати сводки по таблицам""" @@ -352,10 +353,10 @@ def test_print_tables_summary(self, capsys): captured = capsys.readouterr() output = captured.out - assert "ТАБЛИЦЫ" в output - assert "users" в output - assert "50" в output - assert "insert:30" в output + assert "ТАБЛИЦЫ" in output + assert "users" in output + assert "50" in output + assert "insert:30" in output class TestDataCompression: