Spaces:
Paused
Paused
File size: 5,716 Bytes
a5784e9 | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | """
High-quality tests for api_utils/utils.py pure functions (zero mocking).
Focus: Test real business logic with no mocks, only pure function testing.
"""
import json
def test_extract_json_from_text_valid_json():
"""
Test scenario: Extract valid JSON from text
Strategy: Pure function test, no mocking needed
"""
from api_utils.utils import _extract_json_from_text
text = 'Some text before {"key": "value", "num": 42} and text after'
result = _extract_json_from_text(text)
assert result is not None
parsed = json.loads(result)
assert parsed["key"] == "value"
assert parsed["num"] == 42
def test_extract_json_from_text_nested_json():
"""
Test scenario: Extract nested JSON object
Verify: Able to handle complex structures
"""
from api_utils.utils import _extract_json_from_text
text = '{"outer": {"inner": {"deep": "value"}}, "array": [1, 2, 3]}'
result = _extract_json_from_text(text)
assert result is not None
parsed = json.loads(result)
assert parsed["outer"]["inner"]["deep"] == "value"
assert parsed["array"] == [1, 2, 3]
def test_extract_json_from_text_invalid_json():
"""
Test scenario: Invalid JSON string
Expected: Return None
"""
from api_utils.utils import _extract_json_from_text
text = "{invalid json syntax"
result = _extract_json_from_text(text)
assert result is None
def test_extract_json_from_text_empty_string():
"""
Test scenario: Empty string
Expected: Return None
"""
from api_utils.utils import _extract_json_from_text
result = _extract_json_from_text("")
assert result is None
def test_extract_json_from_text_no_braces():
"""
Test scenario: Text without braces
Expected: Return None
"""
from api_utils.utils import _extract_json_from_text
text = "just plain text without any braces"
result = _extract_json_from_text(text)
assert result is None
def test_extract_json_from_text_multiple_json_objects():
"""
Test scenario: Text containing multiple JSON objects (invalid case)
Verify: Function handles invalid multi-object text
Description: Function extracts from the first '{' to the last '}',
so for '{"first": "obj"} text {"second": "obj"}' it gets the whole string,
which is not valid JSON, thus returns None.
"""
from api_utils.utils import _extract_json_from_text
text = '{"first": "obj"} some text {"second": "obj"}'
result = _extract_json_from_text(text)
# Expected to return None because extracted string is not valid JSON
assert result is None
def test_extract_json_from_text_json_with_unicode():
"""
Test scenario: JSON with Unicode characters
Verify: Correctly handle non-ASCII characters
"""
from api_utils.utils import _extract_json_from_text
text = '{"message": "hello world", "emoji": "smile"}'
result = _extract_json_from_text(text)
assert result is not None
parsed = json.loads(result)
assert parsed["message"] == "hello world"
assert parsed["emoji"] == "smile"
def test_extract_json_from_text_json_with_escaped_quotes():
"""
Test scenario: JSON with escaped quotes
Verify: Correctly handle escape characters
"""
from api_utils.utils import _extract_json_from_text
text = '{"quote": "He said \\"hello\\""}'
result = _extract_json_from_text(text)
assert result is not None
parsed = json.loads(result)
assert parsed["quote"] == 'He said "hello"'
def test_generate_sse_stop_chunk_with_usage_basic():
"""
Test scenario: Generate basic SSE stop chunk
Strategy: Pure function test, verify output format
"""
from api_utils.utils import generate_sse_stop_chunk_with_usage
usage_stats = {"prompt_tokens": 100, "completion_tokens": 50, "total_tokens": 150}
result = generate_sse_stop_chunk_with_usage(
req_id="test123", model="gemini-1.5-pro", usage_stats=usage_stats, reason="stop"
)
# Verify output is in SSE format
assert isinstance(result, str)
assert "data:" in result
# Extract JSON part to verify
# SSE format: data: {json}\n\n
lines = result.strip().split("\n")
data_line = None
for line in lines:
if line.startswith("data:"):
data_line = line[5:].strip() # Remove "data:" prefix
break
if data_line and data_line != "[DONE]":
try:
chunk_data = json.loads(data_line)
assert "choices" in chunk_data or "usage" in chunk_data
except json.JSONDecodeError:
# Some SSE chunks might not be JSON
pass
def test_generate_sse_stop_chunk_with_usage_custom_reason():
"""
Test scenario: Use custom stop reason
Verify: reason parameter passed correctly
"""
from api_utils.utils import generate_sse_stop_chunk_with_usage
usage_stats = {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
result = generate_sse_stop_chunk_with_usage(
req_id="test456",
model="gemini-2.0-flash-exp",
usage_stats=usage_stats,
reason="length", # Custom reason
)
assert isinstance(result, str)
assert "data:" in result
# Verify contains stop info
assert result # non-empty
def test_generate_sse_stop_chunk_with_usage_empty_usage():
"""
Test scenario: Empty usage statistics
Verify: Able to handle empty dict
"""
from api_utils.utils import generate_sse_stop_chunk_with_usage
result = generate_sse_stop_chunk_with_usage(
req_id="test789", model="test-model", usage_stats={}, reason="stop"
)
assert isinstance(result, str)
assert "data:" in result
|