-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_structured.py
More file actions
77 lines (61 loc) · 2.36 KB
/
test_structured.py
File metadata and controls
77 lines (61 loc) · 2.36 KB
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
"""Test structured output implementation."""
from l0._utils import auto_correct_json
def test_auto_correct():
"""Test auto_correct_json function."""
# Test 1: Text prefix removal
result = auto_correct_json(
'Sure! Here is the JSON: {"name": "Alice"}', track_corrections=True
)
assert '{"name": "Alice"}' in result.text, f"Got: {result.text}"
print("Test 1 (text prefix): PASS")
# Test 2: Trailing comma
result = auto_correct_json('{"a": 1,}', track_corrections=True)
assert result.text == '{"a": 1}', f"Got: {result.text}"
assert result.corrected
print("Test 2 (trailing comma): PASS")
# Test 3: Missing brace
result = auto_correct_json('{"name": "Alice"', track_corrections=True)
assert result.text == '{"name": "Alice"}', f"Got: {result.text}"
assert result.corrected
assert any("brace" in c.lower() for c in result.corrections)
print("Test 3 (missing brace): PASS")
# Test 4: Single quotes
result = auto_correct_json("{'name': 'Alice'}", track_corrections=True)
assert '"name"' in result.text, f"Got: {result.text}"
assert '"Alice"' in result.text, f"Got: {result.text}"
print("Test 4 (single quotes): PASS")
# Test 5: Markdown fence
result = auto_correct_json('```json\n{"a": 1}\n```', track_corrections=True)
assert result.text == '{"a": 1}', f"Got: {result.text}"
print("Test 5 (markdown fence): PASS")
# Test 6: Text suffix removal
result = auto_correct_json(
'{"a": 1} Let me know if you need anything else!', track_corrections=True
)
assert result.text == '{"a": 1}', f"Got: {result.text}"
print("Test 6 (text suffix): PASS")
# Test 7: Complex case
text = """Sure! Here's the user data:
```json
{"name": "Bob", "age": 30,}
```
Hope this helps!"""
result = auto_correct_json(text, track_corrections=True)
assert '"name"' in result.text
assert '"Bob"' in result.text
assert ",}" not in result.text # Trailing comma removed
print("Test 7 (complex case): PASS")
print("\nAll auto_correct tests passed!")
def test_imports():
"""Test all structured imports."""
from l0 import (
structured,
structured_stream,
StructuredResult,
StructuredStreamResult,
AutoCorrectInfo,
)
print("All imports successful!")
if __name__ == "__main__":
test_imports()
test_auto_correct()