Skip to content

Commit 854a932

Browse files
committed
🚧 added additional tests to increase code coverage
1 parent 03da091 commit 854a932

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/test_embedding_topic_detector.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import asyncio
17+
from types import SimpleNamespace
1718

1819
from nemoguardrails import RailsConfig
1920
from tests.utils import TestChat
@@ -81,3 +82,72 @@ def test_detector_logic():
8182
off_topic = asyncio.run(detector.detect("Who won the Super Bowl?"))
8283
assert off_topic["on_topic"] is False
8384
assert off_topic["confidence"] < 0.5
85+
86+
87+
def test_empty_query_handling():
88+
"""Test that empty queries are handled gracefully."""
89+
from nemoguardrails.library.embedding_topic_detector.actions import _check
90+
91+
llm_task_manager = SimpleNamespace(
92+
config=SimpleNamespace(
93+
rails=SimpleNamespace(
94+
config=SimpleNamespace(
95+
embedding_topic_detector={
96+
"embedding_model": "BAAI/bge-small-en-v1.5",
97+
"embedding_engine": "FastEmbed",
98+
"examples": {"coffee": ["espresso"]},
99+
"threshold": 0.5,
100+
"top_k": 3,
101+
}
102+
)
103+
)
104+
)
105+
)
106+
107+
# Test with None context
108+
result = asyncio.run(_check(None, llm_task_manager, "user_message"))
109+
assert result == {
110+
"on_topic": True,
111+
"confidence": 0.0,
112+
"top_category": None,
113+
"category_scores": {},
114+
}
115+
116+
# Test with empty message in context
117+
result = asyncio.run(_check({}, llm_task_manager, "user_message"))
118+
assert result["on_topic"] is True
119+
assert result["confidence"] == 0.0
120+
121+
122+
def test_output_rail():
123+
"""Test that output rail (bot message checking) works."""
124+
yaml_with_output = """
125+
models:
126+
- type: main
127+
engine: fake
128+
model: test
129+
130+
rails:
131+
config:
132+
embedding_topic_detector:
133+
embedding_model: "BAAI/bge-small-en-v1.5"
134+
embedding_engine: "FastEmbed"
135+
threshold: 0.5
136+
top_k: 3
137+
examples:
138+
coffee:
139+
- "how to brew coffee"
140+
- "espresso tips"
141+
142+
output:
143+
flows:
144+
- embedding topic check output
145+
"""
146+
147+
config = RailsConfig.from_content(
148+
colang_content=COLANG_CONFIG, yaml_content=yaml_with_output
149+
)
150+
chat = TestChat(config, llm_completions=["Who won the Super Bowl yesterday?"])
151+
152+
chat >> "Hello"
153+
chat << "I'm sorry, I can't respond to that."

0 commit comments

Comments
 (0)