1010Per BACK-06: Code review and test coverage documentation.
1111"""
1212
13+ from unittest .mock import MagicMock , patch
14+
1315import pytest
14- from unittest .mock import AsyncMock , MagicMock , patch , Mock
15- from typing import Any
1616
17- from app .services .sql_executor import SQLExecutor
1817from app .services .python_sandbox import PythonSandbox
1918from app .services .result_processor import ResultProcessor
19+ from app .services .sql_executor import SQLExecutor
2020from app .services .visualization_engine import VisualizationEngine
2121
2222
@@ -236,8 +236,7 @@ async def test_execute_with_timeout(self, sandbox):
236236 mock_runtime_class .return_value = mock_runtime
237237
238238 with patch ("asyncio.wait_for" ) as mock_wait :
239- import asyncio
240- mock_wait .side_effect = asyncio .TimeoutError ()
239+ mock_wait .side_effect = TimeoutError ()
241240
242241 with pytest .raises (RuntimeError ) as exc_info :
243242 await sandbox .execute (slow_code , timeout = 5 )
@@ -398,12 +397,8 @@ async def test_extract_chart_config_valid(self, processor):
398397 "yKeys" : ["revenue" ],
399398 }
400399
401- sql_data = [
402- {"date" : "2024-01-01" , "revenue" : 1000 },
403- {"date" : "2024-01-02" , "revenue" : 2000 },
404- ]
405400
406- result = await processor .extract_results (ai_content )
401+ await processor .extract_results (ai_content )
407402
408403 # Since extract_chart_config is mocked, verify the mock is set up
409404 mock_extract .assert_called ()
@@ -428,7 +423,7 @@ def test_build_chart_payload(self, processor):
428423 "data" : data ,
429424 }
430425
431- with patch ("app.services.engine_visualization.build_chart_from_config" , return_value = expected_chart ) as mock_build :
426+ with patch ("app.services.engine_visualization.build_chart_from_config" , return_value = expected_chart ):
432427 result = processor .build_chart_payload (chart_config , data )
433428
434429 assert result is not None
@@ -580,7 +575,7 @@ class TestServiceModuleIntegration:
580575 async def test_sql_to_python_pipeline (self ):
581576 """Test pipeline: SQL execution → Python analysis"""
582577 sql_executor = SQLExecutor ()
583- python_sandbox = PythonSandbox ()
578+ PythonSandbox ()
584579
585580 # Mock SQL execution result
586581 mock_result = MagicMock ()
@@ -608,7 +603,7 @@ async def test_sql_to_python_pipeline(self):
608603 async def test_result_processor_to_visualization_pipeline (self ):
609604 """Test pipeline: Result extraction → Chart generation"""
610605 processor = ResultProcessor ()
611- engine = VisualizationEngine ()
606+ VisualizationEngine ()
612607
613608 ai_content = """
614609 SQL: SELECT category, COUNT(*) as count FROM items GROUP BY category
@@ -646,7 +641,6 @@ async def test_sql_executor_specific_exceptions(self):
646641 executor = SQLExecutor ()
647642
648643 # Check that code uses specific exception types
649- from sqlalchemy .exc import OperationalError , ProgrammingError
650644 import inspect
651645
652646 source = inspect .getsource (executor .execute_sql )
@@ -695,9 +689,9 @@ class TestAPICCompatibility:
695689
696690 def test_service_modules_importable (self ):
697691 """Verify all service modules can be imported"""
698- from app .services .sql_executor import SQLExecutor
699692 from app .services .python_sandbox import PythonSandbox
700693 from app .services .result_processor import ResultProcessor
694+ from app .services .sql_executor import SQLExecutor
701695 from app .services .visualization_engine import VisualizationEngine
702696
703697 assert SQLExecutor is not None
@@ -707,10 +701,10 @@ def test_service_modules_importable(self):
707701
708702 def test_gptme_engine_imports_services (self ):
709703 """Verify GptmeEngine imports and uses service modules"""
710- from app .services .gptme_engine import GptmeEngine
711-
712704 import inspect
713705
706+ from app .services .gptme_engine import GptmeEngine
707+
714708 source = inspect .getsource (GptmeEngine )
715709
716710 # Verify service modules are imported
@@ -721,20 +715,22 @@ def test_gptme_engine_imports_services(self):
721715
722716 def test_service_module_type_hints (self ):
723717 """Verify service modules have proper type hints per BACK-06 checklist"""
724- from app .services .sql_executor import SQLExecutor
725718 import inspect
726719
720+ from app .services .sql_executor import SQLExecutor
721+
727722 # Check type hints on main methods
728723 sig = inspect .signature (SQLExecutor .execute_sql )
729724 assert sig .return_annotation is not None
730725
731726 def test_no_bare_except_clauses (self ):
732727 """Verify no bare except clauses per D-04"""
733- from app .services .sql_executor import SQLExecutor
728+ import inspect
729+
734730 from app .services .python_sandbox import PythonSandbox
735731 from app .services .result_processor import ResultProcessor
732+ from app .services .sql_executor import SQLExecutor
736733 from app .services .visualization_engine import VisualizationEngine
737- import inspect
738734
739735 modules = [
740736 (SQLExecutor , "SQLExecutor" ),
@@ -752,7 +748,7 @@ def test_no_bare_except_clauses(self):
752748 if stripped .startswith ("except:" ):
753749 # Allow "except:" if it's followed by comment or if it's part of error message
754750 # But flag actual bare except clauses in try blocks
755- if "except:" in line and not "except:" in "# except:" :
751+ if "except:" in line and "except:" not in "# except:" :
756752 # This is a potential bare except - would fail per D-04
757753 pass # Acceptable in test context
758754
0 commit comments