AutoGPT

Форк
0
/
test_json_utils.py 
93 строки · 3.1 Кб
1
import json
2

3
import pytest
4

5
from autogpt.core.utils.json_utils import json_loads
6

7
_JSON_FIXABLE: list[tuple[str, str]] = [
8
    # Missing comma
9
    ('{"name": "John Doe"   "age": 30,}', '{"name": "John Doe", "age": 30}'),
10
    ("[1, 2 3]", "[1, 2, 3]"),
11
    # Trailing comma
12
    ('{"name": "John Doe", "age": 30,}', '{"name": "John Doe", "age": 30}'),
13
    ("[1, 2, 3,]", "[1, 2, 3]"),
14
    # Extra comma in object
15
    ('{"name": "John Doe",, "age": 30}', '{"name": "John Doe", "age": 30}'),
16
    # Extra newlines
17
    ('{"name": "John Doe",\n"age": 30}', '{"name": "John Doe", "age": 30}'),
18
    ("[1, 2,\n3]", "[1, 2, 3]"),
19
    # Missing closing brace or bracket
20
    ('{"name": "John Doe", "age": 30', '{"name": "John Doe", "age": 30}'),
21
    ("[1, 2, 3", "[1, 2, 3]"),
22
    # Different numerals
23
    ("[+1, ---2, .5, +-4.5, 123.]", "[1, -2, 0.5, -4.5, 123]"),
24
    ('{"bin": 0b1001, "hex": 0x1A, "oct": 0o17}', '{"bin": 9, "hex": 26, "oct": 15}'),
25
    # Broken array
26
    (
27
        '[1, 2 3, "yes" true, false null, 25, {"obj": "var"}',
28
        '[1, 2, 3, "yes", true, false, null, 25, {"obj": "var"}]',
29
    ),
30
    # Codeblock
31
    (
32
        '```json\n{"name": "John Doe", "age": 30}\n```',
33
        '{"name": "John Doe", "age": 30}',
34
    ),
35
    # Mutliple problems
36
    (
37
        '{"name":"John Doe" "age": 30\n "empty": "","address": '
38
        "// random comment\n"
39
        '{"city": "New York", "state": "NY"},'
40
        '"skills": ["Python" "C++", "Java",""],',
41
        '{"name": "John Doe", "age": 30, "empty": "", "address": '
42
        '{"city": "New York", "state": "NY"}, '
43
        '"skills": ["Python", "C++", "Java", ""]}',
44
    ),
45
    # All good
46
    (
47
        '{"name": "John Doe", "age": 30, "address": '
48
        '{"city": "New York", "state": "NY"}, '
49
        '"skills": ["Python", "C++", "Java"]}',
50
        '{"name": "John Doe", "age": 30, "address": '
51
        '{"city": "New York", "state": "NY"}, '
52
        '"skills": ["Python", "C++", "Java"]}',
53
    ),
54
    ("true", "true"),
55
    ("false", "false"),
56
    ("null", "null"),
57
    ("123.5", "123.5"),
58
    ('"Hello, World!"', '"Hello, World!"'),
59
    ("{}", "{}"),
60
    ("[]", "[]"),
61
]
62

63
_JSON_UNFIXABLE: list[tuple[str, str]] = [
64
    # Broken booleans and null
65
    ("[TRUE, False, NULL]", "[true, false, null]"),
66
    # Missing values in array
67
    ("[1, , 3]", "[1, 3]"),
68
    # Leading zeros (are treated as octal)
69
    ("[0023, 015]", "[23, 15]"),
70
    # Missing quotes
71
    ('{"name": John Doe}', '{"name": "John Doe"}'),
72
    # Missing opening braces or bracket
73
    ('"name": "John Doe"}', '{"name": "John Doe"}'),
74
    ("1, 2, 3]", "[1, 2, 3]"),
75
]
76

77

78
@pytest.fixture(params=_JSON_FIXABLE)
79
def fixable_json(request: pytest.FixtureRequest) -> tuple[str, str]:
80
    return request.param
81

82

83
@pytest.fixture(params=_JSON_UNFIXABLE)
84
def unfixable_json(request: pytest.FixtureRequest) -> tuple[str, str]:
85
    return request.param
86

87

88
def test_json_loads_fixable(fixable_json: tuple[str, str]):
89
    assert json_loads(fixable_json[0]) == json.loads(fixable_json[1])
90

91

92
def test_json_loads_unfixable(unfixable_json: tuple[str, str]):
93
    assert json_loads(unfixable_json[0]) != json.loads(unfixable_json[1])
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.