-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(json_utils): fix function find_json_objects (#2289)
Co-authored-by: dongzhancai1 <[email protected]> Co-authored-by: aries_ckt <[email protected]>
- Loading branch information
1 parent
ddfb435
commit d521300
Showing
2 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pytest | ||
|
||
from dbgpt.util.json_utils import find_json_objects | ||
|
||
# 定义参数化测试数据 | ||
test_data = [ | ||
( | ||
""" | ||
```json | ||
{ | ||
"serial_number": "1", | ||
"agent": "CodeOptimizer", | ||
"content": "```json | ||
select * | ||
from table | ||
where column = 'value' | ||
``` optimize the code above.", | ||
"rely": "" | ||
} | ||
``` | ||
""", | ||
[ | ||
{ | ||
"serial_number": "1", | ||
"agent": "CodeOptimizer", | ||
"content": "```json\nselect * \nfrom table\nwhere column = 'value'\n``` optimize the code above.", | ||
"rely": "", | ||
} | ||
], | ||
"Test case with nested code block", | ||
), | ||
( | ||
""" | ||
{ | ||
"key": "value" | ||
} | ||
""", | ||
[{"key": "value"}], | ||
"Test case with simple JSON", | ||
), | ||
( | ||
""" | ||
{ | ||
"key1": "value1" | ||
} | ||
{ | ||
"key2": "value2" | ||
} | ||
""", | ||
[{"key1": "value1"}, {"key2": "value2"}], | ||
"Test case with multiple JSON objects", | ||
), | ||
("", [], "Test case with empty input"), | ||
("This is not a JSON string", [], "Test case with non-JSON input"), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("text, expected, description", test_data) | ||
def test_find_json_objects(text, expected, description): | ||
result = find_json_objects(text) | ||
assert ( | ||
result == expected | ||
), f"Test failed: {description}\nExpected: {expected}\nGot: {result}" |