Spaces:
Sleeping
Sleeping
File size: 2,684 Bytes
a686b1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | """
Testes para configuracao de logging.
"""
import pytest
import logging
import json
import tempfile
from pathlib import Path
from src.logging_config import setup_logging, get_logger, log_rag_query
class TestLoggingConfig:
"""Testes para configuracao de logging."""
def test_setup_logging_default(self):
"""Testa setup de logging com configuracao padrao."""
logger = setup_logging()
assert logger is not None
assert logger.level == logging.INFO
def test_setup_logging_debug(self):
"""Testa setup com nivel DEBUG."""
logger = setup_logging(level="DEBUG")
assert logger.level == logging.DEBUG
def test_get_logger(self):
"""Testa obtencao de logger."""
logger = get_logger("test_module")
assert logger is not None
assert logger.name == "test_module"
def test_log_rag_query(self):
"""Testa logging de query RAG."""
# Criar arquivo de log temporario
with tempfile.NamedTemporaryFile(mode='w', suffix='.log', delete=False) as f:
log_file = f.name
try:
# Setup logging para arquivo
logger = setup_logging(log_file=log_file)
# Log query
log_rag_query(
query="teste query",
num_results=5,
response_time=0.123,
model="test-model"
)
# Verificar que foi escrito
log_content = Path(log_file).read_text()
assert "teste query" in log_content or len(log_content) > 0
finally:
Path(log_file).unlink(missing_ok=True)
def test_log_levels(self):
"""Testa diferentes niveis de log."""
logger = get_logger("test_levels")
# Nao deve lancar excecoes
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
def test_structured_logging(self):
"""Testa logging estruturado em JSON."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.log', delete=False) as f:
log_file = f.name
try:
logger = setup_logging(log_file=log_file, format="json")
# Log mensagem estruturada
logger.info("Test message", extra={
"query": "test",
"results": 5,
"time": 0.5
})
# Ler log
log_content = Path(log_file).read_text()
assert len(log_content) > 0
finally:
Path(log_file).unlink(missing_ok=True)
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|