Skip to content

Commit 9a3e1a2

Browse files
committed
bugfix(test): Refactor jsonpatch test fixture
1 parent b7dbdd9 commit 9a3e1a2

1 file changed

Lines changed: 57 additions & 60 deletions

File tree

tests/common/test_jsonpatch.py

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,150 +4,147 @@
44

55
from lsst.cmservice.common.jsonpatch import JSONPatch, JSONPatchError, apply_json_patch
66

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:
2326
"""Tests the use of an add operation with a JSON Patch."""
24-
global TARGET_OBJECT
2527

2628
# Fail to add a value to an element that does not exist
2729
op = JSONPatch(op="add", path="/spec/b_list/0", value="a")
2830
with pytest.raises(JSONPatchError):
29-
_ = apply_json_patch(op, TARGET_OBJECT)
31+
_ = apply_json_patch(op, target_object)
3032

3133
# Fix the missing "four" property in the spec
3234
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
3537

3638
# Insert the missing "d" value in the spec's a_list property
3739
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"
4143

4244
# Append to an existing list using "-"
4345
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"
4749

4850

49-
def test_jsonpatch_replace() -> None:
51+
def test_jsonpatch_replace(target_object: dict[str, Any]) -> None:
5052
"""Tests the use of a replace operation with a JSON Patch."""
51-
global TARGET_OBJECT
5253

5354
# Fail to replace a value for a missing key
5455
op = JSONPatch(op="replace", path="/spec/five", value=5)
5556
with pytest.raises(JSONPatchError):
56-
_ = apply_json_patch(op, TARGET_OBJECT)
57+
_ = apply_json_patch(op, target_object)
5758

5859
# Fail to replace a value for a missing index
5960
op = JSONPatch(op="replace", path="/spec/a_list/4", value="e")
6061
with pytest.raises(JSONPatchError):
61-
_ = apply_json_patch(op, TARGET_OBJECT)
62+
_ = apply_json_patch(op, target_object)
6263

6364
# Fix the incorrect "three" property in the spec
6465
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
6768

6869

69-
def test_jsonpatch_remove() -> None:
70+
def test_jsonpatch_remove(target_object: dict[str, Any]) -> None:
7071
"""Tests the use of a remove operation with a JSON Patch."""
71-
global TARGET_OBJECT
7272

7373
# Remove the first element ("a") of the "a_list" property in the spec
7474
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"
7777

7878
# Remove the a non-existent index from the same list (not an error)
7979
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
8282

8383
# Remove the previously added key "four" element in the spec
8484
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()
8787

8888
# Repeat the previous removal (not an error)
8989
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)
9191

9292

93-
def test_jsonpatch_move() -> None:
93+
def test_jsonpatch_move(target_object: dict[str, Any]) -> None:
9494
"""Tests the use of a move operation with a JSON Patch."""
95-
global TARGET_OBJECT
9695

9796
# move the tags list from spec to metadata
9897
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()
102101

103102
# Fail to move a nonexistent object
104103
op = JSONPatch(op="move", path="/spec/yes_such_list", from_="/spec/no_such_list")
105104
with pytest.raises(JSONPatchError):
106-
_ = apply_json_patch(op, TARGET_OBJECT)
105+
_ = apply_json_patch(op, target_object)
107106

108107

109-
def test_jsonpatch_copy() -> None:
108+
def test_jsonpatch_copy(target_object: dict[str, Any]) -> None:
110109
"""Tests the use of a copy operation with a JSON Patch."""
111-
global TARGET_OBJECT
112110

113111
# copy the owner from metadata to spec as the name "pilot"
114112
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"]
117115

118116
# Fail to copy a nonexistent object
119117
op = JSONPatch(op="copy", path="/spec/yes_such_list", from_="/spec/no_such_list")
120118
with pytest.raises(JSONPatchError):
121-
_ = apply_json_patch(op, TARGET_OBJECT)
119+
_ = apply_json_patch(op, target_object)
122120

123121

124-
def test_jsonpatch_test() -> None:
122+
def test_jsonpatch_test(target_object: dict[str, Any]) -> None:
125123
"""Tests the use of a test/assert operation with a JSON Patch."""
126-
global TARGET_OBJECT
127124

128125
# test successful assertion
129126
op = JSONPatch(op="test", path="/metadata/owner", value="bob_loblaw")
130-
_ = apply_json_patch(op, TARGET_OBJECT)
127+
_ = apply_json_patch(op, target_object)
131128

132129
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)
134131

135132
# test value mismatch
136133
op = JSONPatch(op="test", path="/metadata/owner", value="bob_alice")
137134
with pytest.raises(JSONPatchError):
138-
_ = apply_json_patch(op, TARGET_OBJECT)
135+
_ = apply_json_patch(op, target_object)
139136

140137
# test missing key
141138
op = JSONPatch(op="test", path="/metadata/pilot", value="bob_alice")
142139
with pytest.raises(JSONPatchError):
143-
_ = apply_json_patch(op, TARGET_OBJECT)
140+
_ = apply_json_patch(op, target_object)
144141

145142
# test missing index
146143
op = JSONPatch(op="test", path="/spec/a_list/8", value="bob_alice")
147144
with pytest.raises(JSONPatchError):
148-
_ = apply_json_patch(op, TARGET_OBJECT)
145+
_ = apply_json_patch(op, target_object)
149146

150147
# test bad reference token
151148
op = JSONPatch(op="test", path="/spec/a_list/-", value="bob_alice")
152149
with pytest.raises(JSONPatchError):
153-
_ = apply_json_patch(op, TARGET_OBJECT)
150+
_ = apply_json_patch(op, target_object)

0 commit comments

Comments
 (0)