Skip to content

Commit d03d050

Browse files
tests: add unit tests for query type functions in schemas.py (#281)
- test is_valid_query_type() for valid, invalid, empty, and case-sensitive inputs - test str_to_query_type() for correct conversion and ValueError on invalid input - test try_str_to_query_type() for fallback to MULTI and warning log on invalid input Closes #277 Co-authored-by: Bervianto Leo Pratama <[email protected]>
1 parent 249de21 commit d03d050

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

chatbot-core/tests/unit/models/__init__.py

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Unit tests for query type utility functions in api/models/schemas.py."""
2+
import pytest
3+
from api.models.schemas import (
4+
QueryType,
5+
is_valid_query_type,
6+
str_to_query_type,
7+
try_str_to_query_type,
8+
)
9+
10+
11+
def test_is_valid_query_type_returns_true_for_simple():
12+
"""Test that is_valid_query_type returns True for SIMPLE."""
13+
assert is_valid_query_type("SIMPLE") is True
14+
15+
16+
def test_is_valid_query_type_returns_true_for_multi():
17+
"""Test that is_valid_query_type returns True for MULTI."""
18+
assert is_valid_query_type("MULTI") is True
19+
20+
21+
def test_is_valid_query_type_returns_false_for_invalid():
22+
"""Test that is_valid_query_type returns False for invalid strings."""
23+
assert is_valid_query_type("INVALID") is False
24+
25+
26+
def test_is_valid_query_type_returns_false_for_empty_string():
27+
"""Test that is_valid_query_type returns False for empty string."""
28+
assert is_valid_query_type("") is False
29+
30+
31+
def test_is_valid_query_type_case_sensitive():
32+
"""Test that is_valid_query_type is case sensitive."""
33+
assert is_valid_query_type("simple") is False
34+
assert is_valid_query_type("multi") is False
35+
36+
37+
def test_str_to_query_type_converts_simple():
38+
"""Test that str_to_query_type converts SIMPLE to QueryType.SIMPLE."""
39+
assert str_to_query_type("SIMPLE") == QueryType.SIMPLE
40+
41+
42+
def test_str_to_query_type_converts_multi():
43+
"""Test that str_to_query_type converts MULTI to QueryType.MULTI."""
44+
assert str_to_query_type("MULTI") == QueryType.MULTI
45+
46+
47+
def test_str_to_query_type_raises_value_error_for_invalid():
48+
"""Test that str_to_query_type raises ValueError for invalid input."""
49+
with pytest.raises(ValueError, match="Invalid query type: UNKNOWN"):
50+
str_to_query_type("UNKNOWN")
51+
52+
53+
def test_str_to_query_type_raises_value_error_for_empty():
54+
"""Test that str_to_query_type raises ValueError for empty string."""
55+
with pytest.raises(ValueError):
56+
str_to_query_type("")
57+
58+
59+
def test_try_str_to_query_type_returns_simple(mocker):
60+
"""Test that try_str_to_query_type returns SIMPLE for valid input."""
61+
mock_logger = mocker.MagicMock()
62+
result = try_str_to_query_type("SIMPLE", mock_logger)
63+
assert result == QueryType.SIMPLE
64+
mock_logger.info.assert_not_called()
65+
66+
67+
def test_try_str_to_query_type_returns_multi(mocker):
68+
"""Test that try_str_to_query_type returns MULTI for valid input."""
69+
mock_logger = mocker.MagicMock()
70+
result = try_str_to_query_type("MULTI", mock_logger)
71+
assert result == QueryType.MULTI
72+
mock_logger.info.assert_not_called()
73+
74+
75+
def test_try_str_to_query_type_falls_back_to_multi_on_invalid(mocker):
76+
"""Test that try_str_to_query_type falls back to MULTI for invalid input."""
77+
mock_logger = mocker.MagicMock()
78+
result = try_str_to_query_type("INVALID", mock_logger)
79+
assert result == QueryType.MULTI
80+
81+
82+
def test_try_str_to_query_type_logs_warning_on_invalid(mocker):
83+
"""Test that try_str_to_query_type logs a warning for invalid input."""
84+
mock_logger = mocker.MagicMock()
85+
try_str_to_query_type("INVALID", mock_logger)
86+
mock_logger.info.assert_called_once()
87+
88+
89+
def test_try_str_to_query_type_falls_back_on_empty_string(mocker):
90+
"""Test that try_str_to_query_type falls back to MULTI for empty string."""
91+
mock_logger = mocker.MagicMock()
92+
result = try_str_to_query_type("", mock_logger)
93+
assert result == QueryType.MULTI

0 commit comments

Comments
 (0)