|
4 | 4 |
|
5 | 5 | from lsst.cmservice.common.jsonpatch import JSONPatch, JSONPatchError, apply_json_patch |
6 | 6 |
|
7 | | -TARGET_OBJECT: dict[str, Any] = { |
8 | | - "apiVersion": "io.lsst.cmservice/v1", |
9 | | - "spec": { |
10 | | - "one": 1, |
11 | | - "two": 2, |
12 | | - "three": 4, |
13 | | - "a_list": ["a", "b", "c", "e"], |
14 | | - "tag_list": ["yes", "yeah", "yep"], |
15 | | - }, |
16 | | - "metadata": { |
17 | | - "owner": "bob_loblaw", |
18 | | - }, |
19 | | -} |
20 | | - |
21 | | - |
22 | | -def test_jsonpatch_add() -> None: |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def target_object() -> dict[str, Any]: |
| 10 | + return { |
| 11 | + "apiVersion": "io.lsst.cmservice/v1", |
| 12 | + "spec": { |
| 13 | + "one": 1, |
| 14 | + "two": 2, |
| 15 | + "three": 4, |
| 16 | + "a_list": ["a", "b", "c", "e"], |
| 17 | + "tag_list": ["yes", "yeah", "yep"], |
| 18 | + }, |
| 19 | + "metadata": { |
| 20 | + "owner": "bob_loblaw", |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | +def test_jsonpatch_add(target_object: dict[str, Any]) -> None: |
23 | 26 | """Tests the use of an add operation with a JSON Patch.""" |
24 | | - global TARGET_OBJECT |
25 | 27 |
|
26 | 28 | # Fail to add a value to an element that does not exist |
27 | 29 | op = JSONPatch(op="add", path="/spec/b_list/0", value="a") |
28 | 30 | with pytest.raises(JSONPatchError): |
29 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 31 | + _ = apply_json_patch(op, target_object) |
30 | 32 |
|
31 | 33 | # Fix the missing "four" property in the spec |
32 | 34 | op = JSONPatch(op="add", path="/spec/four", value=4) |
33 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
34 | | - assert TARGET_OBJECT["spec"].get("four") == 4 |
| 35 | + target_object = apply_json_patch(op, target_object) |
| 36 | + assert target_object["spec"].get("four") == 4 |
35 | 37 |
|
36 | 38 | # Insert the missing "d" value in the spec's a_list property |
37 | 39 | op = JSONPatch(op="add", path="/spec/a_list/3", value="d") |
38 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
39 | | - assert TARGET_OBJECT["spec"].get("a_list")[3] == "d" |
40 | | - assert TARGET_OBJECT["spec"].get("a_list")[4] == "e" |
| 40 | + target_object = apply_json_patch(op, target_object) |
| 41 | + assert target_object["spec"].get("a_list")[3] == "d" |
| 42 | + assert target_object["spec"].get("a_list")[4] == "e" |
41 | 43 |
|
42 | 44 | # Append to an existing list using "-" |
43 | 45 | op = JSONPatch(op="add", path="/spec/a_list/-", value="f") |
44 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
45 | | - assert len(TARGET_OBJECT["spec"]["a_list"]) == 6 |
46 | | - assert TARGET_OBJECT["spec"]["a_list"][-1] == "f" |
| 46 | + target_object = apply_json_patch(op, target_object) |
| 47 | + assert len(target_object["spec"]["a_list"]) == 6 |
| 48 | + assert target_object["spec"]["a_list"][-1] == "f" |
47 | 49 |
|
48 | 50 |
|
49 | | -def test_jsonpatch_replace() -> None: |
| 51 | +def test_jsonpatch_replace(target_object: dict[str, Any]) -> None: |
50 | 52 | """Tests the use of a replace operation with a JSON Patch.""" |
51 | | - global TARGET_OBJECT |
52 | 53 |
|
53 | 54 | # Fail to replace a value for a missing key |
54 | 55 | op = JSONPatch(op="replace", path="/spec/five", value=5) |
55 | 56 | with pytest.raises(JSONPatchError): |
56 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 57 | + _ = apply_json_patch(op, target_object) |
57 | 58 |
|
58 | 59 | # Fail to replace a value for a missing index |
59 | 60 | op = JSONPatch(op="replace", path="/spec/a_list/4", value="e") |
60 | 61 | with pytest.raises(JSONPatchError): |
61 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 62 | + _ = apply_json_patch(op, target_object) |
62 | 63 |
|
63 | 64 | # Fix the incorrect "three" property in the spec |
64 | 65 | op = JSONPatch(op="replace", path="/spec/three", value=3) |
65 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
66 | | - assert TARGET_OBJECT["spec"]["three"] == 3 |
| 66 | + target_object = apply_json_patch(op, target_object) |
| 67 | + assert target_object["spec"]["three"] == 3 |
67 | 68 |
|
68 | 69 |
|
69 | | -def test_jsonpatch_remove() -> None: |
| 70 | +def test_jsonpatch_remove(target_object: dict[str, Any]) -> None: |
70 | 71 | """Tests the use of a remove operation with a JSON Patch.""" |
71 | | - global TARGET_OBJECT |
72 | 72 |
|
73 | 73 | # Remove the first element ("a") of the "a_list" property in the spec |
74 | 74 | op = JSONPatch(op="remove", path="/spec/a_list/0") |
75 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
76 | | - assert TARGET_OBJECT["spec"]["a_list"][0] == "b" |
| 75 | + target_object = apply_json_patch(op, target_object) |
| 76 | + assert target_object["spec"]["a_list"][0] == "b" |
77 | 77 |
|
78 | 78 | # Remove the a non-existent index from the same list (not an error) |
79 | 79 | op = JSONPatch(op="remove", path="/spec/a_list/8") |
80 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
81 | | - assert len(TARGET_OBJECT["spec"]["a_list"]) == 3 |
| 80 | + target_object = apply_json_patch(op, target_object) |
| 81 | + assert len(target_object["spec"]["a_list"]) == 3 |
82 | 82 |
|
83 | 83 | # Remove the previously added key "four" element in the spec |
84 | 84 | op = JSONPatch(op="remove", path="/spec/four") |
85 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
86 | | - assert "four" not in TARGET_OBJECT["spec"].keys() |
| 85 | + target_object = apply_json_patch(op, target_object) |
| 86 | + assert "four" not in target_object["spec"].keys() |
87 | 87 |
|
88 | 88 | # Repeat the previous removal (not an error) |
89 | 89 | op = JSONPatch(op="remove", path="/spec/four") |
90 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
| 90 | + target_object = apply_json_patch(op, target_object) |
91 | 91 |
|
92 | 92 |
|
93 | | -def test_jsonpatch_move() -> None: |
| 93 | +def test_jsonpatch_move(target_object: dict[str, Any]) -> None: |
94 | 94 | """Tests the use of a move operation with a JSON Patch.""" |
95 | | - global TARGET_OBJECT |
96 | 95 |
|
97 | 96 | # move the tags list from spec to metadata |
98 | 97 | op = JSONPatch(op="move", path="/metadata/tag_list", from_="/spec/tag_list") |
99 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
100 | | - assert "tag_list" not in TARGET_OBJECT["spec"].keys() |
101 | | - assert "tag_list" in TARGET_OBJECT["metadata"].keys() |
| 98 | + target_object = apply_json_patch(op, target_object) |
| 99 | + assert "tag_list" not in target_object["spec"].keys() |
| 100 | + assert "tag_list" in target_object["metadata"].keys() |
102 | 101 |
|
103 | 102 | # Fail to move a nonexistent object |
104 | 103 | op = JSONPatch(op="move", path="/spec/yes_such_list", from_="/spec/no_such_list") |
105 | 104 | with pytest.raises(JSONPatchError): |
106 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 105 | + _ = apply_json_patch(op, target_object) |
107 | 106 |
|
108 | 107 |
|
109 | | -def test_jsonpatch_copy() -> None: |
| 108 | +def test_jsonpatch_copy(target_object: dict[str, Any]) -> None: |
110 | 109 | """Tests the use of a copy operation with a JSON Patch.""" |
111 | | - global TARGET_OBJECT |
112 | 110 |
|
113 | 111 | # copy the owner from metadata to spec as the name "pilot" |
114 | 112 | op = JSONPatch(op="copy", path="/spec/pilot", from_="/metadata/owner") |
115 | | - TARGET_OBJECT = apply_json_patch(op, TARGET_OBJECT) |
116 | | - assert TARGET_OBJECT["spec"]["pilot"] == TARGET_OBJECT["metadata"]["owner"] |
| 113 | + target_object = apply_json_patch(op, target_object) |
| 114 | + assert target_object["spec"]["pilot"] == target_object["metadata"]["owner"] |
117 | 115 |
|
118 | 116 | # Fail to copy a nonexistent object |
119 | 117 | op = JSONPatch(op="copy", path="/spec/yes_such_list", from_="/spec/no_such_list") |
120 | 118 | with pytest.raises(JSONPatchError): |
121 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 119 | + _ = apply_json_patch(op, target_object) |
122 | 120 |
|
123 | 121 |
|
124 | | -def test_jsonpatch_test() -> None: |
| 122 | +def test_jsonpatch_test(target_object: dict[str, Any]) -> None: |
125 | 123 | """Tests the use of a test/assert operation with a JSON Patch.""" |
126 | | - global TARGET_OBJECT |
127 | 124 |
|
128 | 125 | # test successful assertion |
129 | 126 | op = JSONPatch(op="test", path="/metadata/owner", value="bob_loblaw") |
130 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 127 | + _ = apply_json_patch(op, target_object) |
131 | 128 |
|
132 | 129 | op = JSONPatch(op="test", path="/spec/a_list/0", value="a") |
133 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 130 | + _ = apply_json_patch(op, target_object) |
134 | 131 |
|
135 | 132 | # test value mismatch |
136 | 133 | op = JSONPatch(op="test", path="/metadata/owner", value="bob_alice") |
137 | 134 | with pytest.raises(JSONPatchError): |
138 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 135 | + _ = apply_json_patch(op, target_object) |
139 | 136 |
|
140 | 137 | # test missing key |
141 | 138 | op = JSONPatch(op="test", path="/metadata/pilot", value="bob_alice") |
142 | 139 | with pytest.raises(JSONPatchError): |
143 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 140 | + _ = apply_json_patch(op, target_object) |
144 | 141 |
|
145 | 142 | # test missing index |
146 | 143 | op = JSONPatch(op="test", path="/spec/a_list/8", value="bob_alice") |
147 | 144 | with pytest.raises(JSONPatchError): |
148 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 145 | + _ = apply_json_patch(op, target_object) |
149 | 146 |
|
150 | 147 | # test bad reference token |
151 | 148 | op = JSONPatch(op="test", path="/spec/a_list/-", value="bob_alice") |
152 | 149 | with pytest.raises(JSONPatchError): |
153 | | - _ = apply_json_patch(op, TARGET_OBJECT) |
| 150 | + _ = apply_json_patch(op, target_object) |
0 commit comments