Spaces:
Sleeping
Sleeping
File size: 2,246 Bytes
c96b98a | 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 | from phi.utils.string import extract_valid_json
def test_extract_valid_json_with_valid_json():
content = 'Here is some text {"key": "value"} and more text.'
expected_json = {"key": "value"}
extracted_json = extract_valid_json(content)
assert extracted_json == expected_json
def test_extract_valid_json_with_nested_json():
content = 'Start {"key": {"nested_key": "nested_value"}} End'
expected_json = {"key": {"nested_key": "nested_value"}}
extracted_json = extract_valid_json(content)
assert extracted_json == expected_json
def test_extract_valid_json_with_multiple_json_objects():
content = 'First {"key1": "value1"} Second {"key2": "value2"}'
expected_json = {"key1": "value1"} # Only the first JSON should be returned
extracted_json = extract_valid_json(content)
assert extracted_json == expected_json
def test_extract_valid_json_with_no_json():
content = "This is a string without JSON."
extracted_json = extract_valid_json(content)
assert extracted_json is None
def test_extract_valid_json_with_invalid_json():
content = "This string contains {invalid JSON}."
extracted_json = extract_valid_json(content)
assert extracted_json is None
def test_extract_valid_json_with_json_array():
content = 'Here is a JSON array: ["item1", "item2"].'
extracted_json = extract_valid_json(content)
assert extracted_json is None # Only JSON objects are extracted
def test_extract_valid_json_with_empty_json():
content = "Some text {} more text."
expected_json = {}
extracted_json = extract_valid_json(content)
assert extracted_json == expected_json
def test_extract_valid_json_with_multiline_json():
content = """
Here is some text {
"key": "value",
"another_key": "another_value"
} and more text.
"""
expected_json = {"key": "value", "another_key": "another_value"}
extracted_json = extract_valid_json(content)
assert extracted_json == expected_json
def test_extract_valid_json_with_json_in_quotes():
content = 'Text before "{\\"key\\": \\"value\\"}" text after.'
extracted_json = extract_valid_json(content)
assert extracted_json is None # JSON inside quotes should not be parsed
|