44"""
55
66import json
7+ import time
78from pathlib import Path
89from typing import Dict , Any
910import unittest
1011from unittest .mock import patch , mock_open
1112from ai_core .knowledge_base import load_error_knowledge
1213
14+
1315class TestKnowledgeBase (unittest .TestCase ):
1416 """Caso de prueba para el módulo de base de conocimiento"""
1517
1618 @classmethod
1719 def setUpClass (cls ) -> None :
1820 """Configuración inicial de datos de prueba"""
19- cls .test_data_dir = Path (__file__ ).parent / ' test_data'
21+ cls .test_data_dir = Path (__file__ ).parent / " test_data"
2022 cls .test_data_dir .mkdir (exist_ok = True )
2123
2224 # Archivo válido
@@ -27,26 +29,26 @@ def setUpClass(cls) -> None:
2729 "description" : "Test error" ,
2830 "patterns" : ["test pattern" ],
2931 "solutions" : ["test solution" ],
30- "severity" : "low"
32+ "severity" : "low" ,
3133 }
3234 }
3335 }
34- with open (cls .valid_kb_path , 'w' , encoding = ' utf-8' ) as f :
36+ with open (cls .valid_kb_path , "w" , encoding = " utf-8" ) as f :
3537 json .dump (cls .valid_data , f , indent = 2 )
3638
3739 # Archivo inválido
3840 cls .invalid_kb_path = cls .test_data_dir / "invalid_knowledge.json"
39- with open (cls .invalid_kb_path , 'w' , encoding = ' utf-8' ) as f :
41+ with open (cls .invalid_kb_path , "w" , encoding = " utf-8" ) as f :
4042 f .write ('{"key": "value"' ) # JSON incompleto
4143
4244 def test_large_file_performance (self ):
4345 """Test de carga con archivo grande"""
4446 large_data = {"common_errors" : {f"ERR{ i } " : {} for i in range (1000 )}}
45- with patch (' builtins.open' , mock_open (read_data = json .dumps (large_data ))):
46- start = time .time ()
47- result = load_error_knowledge ()
48- elapsed = time .time () - start
49- self .assertLess (elapsed , 0.5 ) # Menos de 500ms
47+ with patch (" builtins.open" , mock_open (read_data = json .dumps (large_data ))):
48+ start = time .time ()
49+ result = load_error_knowledge ()
50+ elapsed = time .time () - start
51+ self .assertLess (elapsed , 0.5 ) # Menos de 500ms
5052
5153 def test_load_valid_knowledge (self ) -> None :
5254 """Test Case KB-101: Carga exitosa de conocimiento válido"""
@@ -64,5 +66,6 @@ def test_file_not_found(self) -> None:
6466 with self .assertRaises (ImportError ):
6567 load_error_knowledge ("non_existent_file.json" )
6668
67- if __name__ == '__main__' :
69+
70+ if __name__ == "__main__" :
6871 unittest .main (verbosity = 2 )
0 commit comments