Misc improvements
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
"""Tests for QuickReplyContentService."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from capa_de_integracion.config import Settings
|
||||
from capa_de_integracion.models.quick_replies import QuickReplyScreen
|
||||
from capa_de_integracion.services.quick_reply_content import QuickReplyContentService
|
||||
from capa_de_integracion.services import QuickReplyContentService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_settings():
|
||||
def mock_settings(tmp_path):
|
||||
"""Create mock settings for testing."""
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = Path("/tmp/test_resources")
|
||||
# Create the quick_replies directory
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
settings.base_path = tmp_path
|
||||
return settings
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service(mock_settings):
|
||||
"""Create QuickReplyContentService instance."""
|
||||
"""Create QuickReplyContentService instance with empty cache."""
|
||||
return QuickReplyContentService(mock_settings)
|
||||
|
||||
|
||||
@@ -59,22 +61,19 @@ async def test_get_quick_replies_whitespace_screen_id(service):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_quick_replies_file_not_found(service, tmp_path):
|
||||
"""Test get_quick_replies raises error when file not found."""
|
||||
# Set service to use a temp directory where the file won't exist
|
||||
service.quick_replies_path = tmp_path / "nonexistent_dir"
|
||||
|
||||
with pytest.raises(ValueError, match="Error loading quick replies"):
|
||||
async def test_get_quick_replies_file_not_found(service):
|
||||
"""Test get_quick_replies raises error when screen not in cache."""
|
||||
# Cache is empty (no files loaded), so any screen_id should raise ValueError
|
||||
with pytest.raises(ValueError, match="Quick reply not found for screen_id"):
|
||||
await service.get_quick_replies("nonexistent")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_quick_replies_success(service, tmp_path):
|
||||
"""Test get_quick_replies successfully loads file."""
|
||||
# Create test JSON file
|
||||
async def test_get_quick_replies_success(tmp_path):
|
||||
"""Test get_quick_replies successfully retrieves from cache."""
|
||||
# Create test JSON file BEFORE initializing service
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
service.quick_replies_path = quick_replies_dir
|
||||
|
||||
test_data = {
|
||||
"header": "Test Header",
|
||||
@@ -97,6 +96,11 @@ async def test_get_quick_replies_success(service, tmp_path):
|
||||
test_file = quick_replies_dir / "test_screen.json"
|
||||
test_file.write_text(json.dumps(test_data), encoding="utf-8")
|
||||
|
||||
# Initialize service - it will preload the file into cache
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
service = QuickReplyContentService(settings)
|
||||
|
||||
result = await service.get_quick_replies("test_screen")
|
||||
|
||||
assert isinstance(result, QuickReplyScreen)
|
||||
@@ -114,25 +118,30 @@ async def test_get_quick_replies_success(service, tmp_path):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_quick_replies_invalid_json(service, tmp_path):
|
||||
"""Test get_quick_replies raises error for invalid JSON."""
|
||||
async def test_get_quick_replies_invalid_json(tmp_path):
|
||||
"""Test that invalid JSON files are skipped during cache preload."""
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
service.quick_replies_path = quick_replies_dir
|
||||
|
||||
# Create invalid JSON file
|
||||
test_file = quick_replies_dir / "invalid.json"
|
||||
test_file.write_text("{ invalid json }", encoding="utf-8")
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid JSON format"):
|
||||
# Initialize service - invalid file should be logged but not crash
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
service = QuickReplyContentService(settings)
|
||||
|
||||
# Requesting the invalid screen should raise ValueError (not in cache)
|
||||
with pytest.raises(ValueError, match="Quick reply not found for screen_id"):
|
||||
await service.get_quick_replies("invalid")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_quick_replies_minimal_data(service, tmp_path):
|
||||
"""Test get_quick_replies with minimal data."""
|
||||
async def test_get_quick_replies_minimal_data(tmp_path):
|
||||
"""Test get_quick_replies with minimal data from cache."""
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
service.quick_replies_path = quick_replies_dir
|
||||
|
||||
test_data = {
|
||||
"preguntas": [],
|
||||
@@ -141,6 +150,11 @@ async def test_get_quick_replies_minimal_data(service, tmp_path):
|
||||
test_file = quick_replies_dir / "minimal.json"
|
||||
test_file.write_text(json.dumps(test_data), encoding="utf-8")
|
||||
|
||||
# Initialize service - it will preload the file
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
service = QuickReplyContentService(settings)
|
||||
|
||||
result = await service.get_quick_replies("minimal")
|
||||
|
||||
assert isinstance(result, QuickReplyScreen)
|
||||
@@ -168,3 +182,103 @@ async def test_validate_file_not_exists(service, tmp_path):
|
||||
|
||||
with pytest.raises(ValueError, match="Quick reply file not found"):
|
||||
service._validate_file(test_file, "test")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cache_preload_multiple_files(tmp_path):
|
||||
"""Test that cache preloads multiple files correctly."""
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
|
||||
# Create multiple test files
|
||||
for screen_id in ["home", "pagos", "transferencia"]:
|
||||
test_data = {
|
||||
"header": f"{screen_id} header",
|
||||
"preguntas": [
|
||||
{"titulo": f"Q1 for {screen_id}", "respuesta": "Answer 1"},
|
||||
],
|
||||
}
|
||||
test_file = quick_replies_dir / f"{screen_id}.json"
|
||||
test_file.write_text(json.dumps(test_data), encoding="utf-8")
|
||||
|
||||
# Initialize service
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
service = QuickReplyContentService(settings)
|
||||
|
||||
# Verify all screens are in cache
|
||||
home = await service.get_quick_replies("home")
|
||||
assert home.header == "home header"
|
||||
|
||||
pagos = await service.get_quick_replies("pagos")
|
||||
assert pagos.header == "pagos header"
|
||||
|
||||
transferencia = await service.get_quick_replies("transferencia")
|
||||
assert transferencia.header == "transferencia header"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cache_preload_with_mixed_valid_invalid(tmp_path):
|
||||
"""Test cache preload handles mix of valid and invalid files."""
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
|
||||
# Create valid file
|
||||
valid_data = {"header": "valid", "preguntas": []}
|
||||
(quick_replies_dir / "valid.json").write_text(
|
||||
json.dumps(valid_data), encoding="utf-8",
|
||||
)
|
||||
|
||||
# Create invalid file
|
||||
(quick_replies_dir / "invalid.json").write_text(
|
||||
"{ invalid }", encoding="utf-8",
|
||||
)
|
||||
|
||||
# Initialize service - should not crash
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
service = QuickReplyContentService(settings)
|
||||
|
||||
# Valid file should be in cache
|
||||
valid = await service.get_quick_replies("valid")
|
||||
assert valid.header == "valid"
|
||||
|
||||
# Invalid file should not be in cache
|
||||
with pytest.raises(ValueError, match="Quick reply not found"):
|
||||
await service.get_quick_replies("invalid")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cache_preload_handles_generic_exception(tmp_path, monkeypatch):
|
||||
"""Test cache preload handles generic exceptions during file processing."""
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
quick_replies_dir = tmp_path / "quick_replies"
|
||||
quick_replies_dir.mkdir()
|
||||
|
||||
# Create valid JSON file
|
||||
valid_data = {"header": "test", "preguntas": []}
|
||||
(quick_replies_dir / "test.json").write_text(
|
||||
json.dumps(valid_data), encoding="utf-8",
|
||||
)
|
||||
|
||||
# Mock _parse_quick_reply_data to raise generic exception
|
||||
def mock_parse_error(*args, **kwargs):
|
||||
raise ValueError("Simulated parsing error")
|
||||
|
||||
settings = Mock(spec=Settings)
|
||||
settings.base_path = tmp_path
|
||||
|
||||
# Initialize service and patch the parsing method
|
||||
with monkeypatch.context() as m:
|
||||
service = QuickReplyContentService(settings)
|
||||
m.setattr(service, "_parse_quick_reply_data", mock_parse_error)
|
||||
|
||||
# Manually call _preload_cache to trigger the exception
|
||||
service._cache.clear()
|
||||
service._preload_cache()
|
||||
|
||||
# The file should not be in cache due to the exception
|
||||
with pytest.raises(ValueError, match="Quick reply not found"):
|
||||
await service.get_quick_replies("test")
|
||||
|
||||
Reference in New Issue
Block a user