File: test_condense_json.py

package info (click to toggle)
python-condense-json 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: python: 176; makefile: 3
file content (104 lines) | stat: -rw-r--r-- 3,236 bytes parent folder | download
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
from condense_json import condense_json
from typing import Dict, Any, List


def test_condense_json() -> None:
    input_json: Dict[str, Any] = {
        "foo": {
            "bar": {
                "string": "This is a string with foxes in it",
                "nested": {
                    "more": ["Here is a string", "another with foxes in it too"]
                },
            }
        }
    }

    replacements: Dict[str, str] = {"1": "with foxes in it"}

    expected_output: Dict[str, Any] = {
        "foo": {
            "bar": {
                "string": {"$r": ["This is a string ", {"$": "1"}]},
                "nested": {
                    "more": [
                        "Here is a string",
                        {"$r": ["another ", {"$": "1"}, " too"]},
                    ]
                },
            }
        }
    }

    assert condense_json(input_json, replacements) == expected_output


def test_no_replacements() -> None:
    input_json: Dict[str, str] = {"text": "This is a normal string"}
    replacements: Dict[str, str] = {"1": "not in the text"}
    expected_output: Dict[str, str] = {"text": "This is a normal string"}

    assert condense_json(input_json, replacements) == expected_output


def test_replacement_not_used() -> None:
    input = {"messages": [{"role": "user", "content": "What is 1231 * 2331?"}]}
    replacements = {"r:01jv577ycee7re8wqdebbvygys": ""}
    output = condense_json(input, replacements)
    assert output == input


def test_empty_json() -> None:
    input_json: Dict[str, Any] = {}
    replacements: Dict[str, str] = {"1": "anything"}
    expected_output: Dict[str, Any] = {}

    assert condense_json(input_json, replacements) == expected_output


def test_multiple_replacements() -> None:
    input_json: Dict[str, Any] = {
        "sentence": "The quick brown fox jumps over the lazy dog",
        "nested": {"list": ["fast fox", "lazy dog", "just some text"]},
    }

    replacements: Dict[str, str] = {"1": "quick brown fox", "2": "lazy dog"}

    expected_output: Dict[str, Any] = {
        "sentence": {"$r": ["The ", {"$": "1"}, " jumps over the ", {"$": "2"}]},
        "nested": {"list": ["fast fox", {"$": "2"}, "just some text"]},
    }

    assert condense_json(input_json, replacements) == expected_output


def test_nested_replacements() -> None:
    input_json: Dict[str, Any] = {
        "outer": {"inner": {"deep": "something deep inside with foxes in it"}}
    }

    replacements: Dict[str, str] = {"1": "with foxes in it"}

    expected_output: Dict[str, Any] = {
        "outer": {"inner": {"deep": {"$r": ["something deep inside ", {"$": "1"}]}}}
    }

    assert condense_json(input_json, replacements) == expected_output


def test_blank_or_none_replacements() -> None:
    input_json: Dict[str, Any] = {
        "outer": {"inner": {"deep": "something deep inside with foxes in it"}}
    }

    replacements: Dict[str, Any] = {"1": "deep", "2": None, "3": ""}

    expected_output: Dict[str, Any] = {
        "outer": {
            "inner": {
                "deep": {"$r": ["something ", {"$": "1"}, " inside with foxes in it"]}
            }
        }
    }

    assert condense_json(input_json, replacements) == expected_output