Skip to content

Commit ad2188e

Browse files
committed
TST: unit tests for 'tag_get', 'tag_set' and 'tag_delete' API
1 parent cb1cb7a commit ad2188e

File tree

2 files changed

+85
-67
lines changed

2 files changed

+85
-67
lines changed

tests/test_tag_control.py

Lines changed: 85 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
base_url,
1212
clear_sar, # noqa: F401
1313
create_root_folder,
14-
user_password, # noqa: F401
15-
user_username,
1614
)
1715

1816
# =============================================================================================
@@ -24,97 +22,117 @@
2422
@pytest.mark.parametrize("usesetauth", [True, False])
2523
@pytest.mark.parametrize("library", ["THREADS", "ASYNC"])
2624
# fmt: on
27-
def test_config_create_01(clear_sar, library, usesetauth): # noqa: F811
25+
def test_tags_01(clear_sar, library, usesetauth): # noqa: F811
2826
"""
29-
Tests for the 'config_create' and 'config_get' API.
27+
Tests for the 'tags_get', 'tags_add' and 'tags_delete' API.
3028
"""
3129

3230
root_folder_uid = create_root_folder()
3331

34-
pv_list = [
35-
{
36-
"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinX"
37-
},
38-
{
39-
"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinY",
40-
"comparison": {
41-
"comparisonMode": "ABSOLUTE",
42-
"tolerance": 2.7
43-
}
44-
},
45-
{
46-
"pvName": "13SIM1:{SimDetector-Cam:2}cam2:BinX",
47-
"readbackPvName": None,
48-
"readOnly": False,
49-
},
50-
{
51-
"pvName": "13SIM1:{SimDetector-Cam:2}cam2:BinY",
52-
"readbackPvName": None,
53-
"readOnly": False,
54-
}
55-
]
56-
57-
configurationNode = {
58-
"name": "Config",
59-
}
60-
configurationData = {
61-
"pvList": pv_list,
62-
}
63-
6432
if not _is_async(library):
6533
with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR:
6634
auth = _select_auth(SR=SR, usesetauth=usesetauth)
6735

68-
response = SR.node_add(root_folder_uid, name="Child Folder", nodeType="FOLDER", **auth)
36+
response = SR.node_add(root_folder_uid, name="folder", nodeType="FOLDER", **auth)
6937
folder_uid = response["uniqueId"]
7038

39+
response = SR.config_create(
40+
folder_uid, configurationNode={"name": "config_1"}, configurationData={"pvList": []}, **auth
41+
)
42+
config_1_uid = response["configurationNode"]["uniqueId"]
7143

7244
response = SR.config_create(
73-
folder_uid, configurationNode=configurationNode, configurationData=configurationData, **auth
45+
folder_uid, configurationNode={"name": "config_2"}, configurationData={"pvList": []}, **auth
7446
)
75-
assert response["configurationNode"]["name"] == "Config"
76-
assert response["configurationNode"]["nodeType"] == "CONFIGURATION"
77-
assert response["configurationNode"]["userName"] == user_username
78-
assert len(response["configurationData"]["pvList"]) == len(pv_list)
47+
config_2_uid = response["configurationNode"]["uniqueId"]
48+
49+
tag_1 = {"name": "tag_1"}
50+
tag_2 = {"name": "tag_2", "comment": "This is tag 2"}
51+
52+
response = SR.tags_get()
53+
assert len(response) == 0
7954

80-
config_uid = response["configurationNode"]["uniqueId"]
55+
response = SR.tags_add(uniqueNodeIds=[config_1_uid], tag=tag_1, **auth)
56+
assert len(response) == 1
57+
assert response[0]["uniqueId"] == config_1_uid
58+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1"]
8159

82-
response = SR.config_get(config_uid)
83-
assert response["uniqueId"] == config_uid
84-
assert len(response["pvList"]) == len(pv_list)
60+
response = SR.tags_add(uniqueNodeIds=[config_1_uid, config_2_uid, folder_uid], tag=tag_2, **auth)
61+
assert len(response) == 3
62+
assert response[0]["uniqueId"] == config_1_uid
63+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1", "tag_2"]
64+
assert response[1]["uniqueId"] == config_2_uid
65+
assert [_["name"] for _ in response[1]["tags"]] == ["tag_2"]
66+
assert response[2]["uniqueId"] == folder_uid
67+
assert [_["name"] for _ in response[2]["tags"]] == ["tag_2"]
8568

86-
response = SR.node_get(config_uid)
87-
assert response["uniqueId"] == config_uid
88-
assert response["name"] == "Config"
89-
assert response["nodeType"] == "CONFIGURATION"
90-
assert response["userName"] == user_username
69+
response = SR.tags_get() # Returns the list of ALL tags
70+
assert len(response) == 4
71+
72+
response = SR.tags_delete(uniqueNodeIds=[config_1_uid, folder_uid], tag={"name": "tag_2"}, **auth)
73+
assert len(response) == 2
74+
assert response[0]["uniqueId"] == config_1_uid
75+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1"]
76+
assert response[1]["uniqueId"] == folder_uid
77+
assert [_["name"] for _ in response[1]["tags"]] == []
78+
79+
response = SR.tags_get()
80+
assert len(response) == 2
9181

9282
else:
9383
async def testing():
9484
async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR:
9585
auth = _select_auth(SR=SR, usesetauth=usesetauth)
9686

97-
response = await SR.node_add(root_folder_uid, name="Child Folder", nodeType="FOLDER", **auth)
87+
response = await SR.node_add(root_folder_uid, name="folder", nodeType="FOLDER", **auth)
9888
folder_uid = response["uniqueId"]
9989

10090
response = await SR.config_create(
101-
folder_uid, configurationNode=configurationNode, configurationData=configurationData, **auth
91+
folder_uid, configurationNode={"name": "config_1"}, configurationData={"pvList": []}, **auth
92+
)
93+
config_1_uid = response["configurationNode"]["uniqueId"]
94+
95+
response = await SR.config_create(
96+
folder_uid, configurationNode={"name": "config_2"}, configurationData={"pvList": []}, **auth
10297
)
103-
assert response["configurationNode"]["name"] == "Config"
104-
assert response["configurationNode"]["nodeType"] == "CONFIGURATION"
105-
assert response["configurationNode"]["userName"] == user_username
106-
assert len(response["configurationData"]["pvList"]) == len(pv_list)
107-
108-
config_uid = response["configurationNode"]["uniqueId"]
109-
110-
response = await SR.config_get(config_uid)
111-
assert response["uniqueId"] == config_uid
112-
assert len(response["pvList"]) == len(pv_list)
113-
114-
response = await SR.node_get(config_uid)
115-
assert response["uniqueId"] == config_uid
116-
assert response["name"] == "Config"
117-
assert response["nodeType"] == "CONFIGURATION"
118-
assert response["userName"] == user_username
98+
config_2_uid = response["configurationNode"]["uniqueId"]
99+
100+
tag_1 = {"name": "tag_1"}
101+
tag_2 = {"name": "tag_2", "comment": "This is tag 2"}
102+
103+
response = await SR.tags_get()
104+
assert len(response) == 0
105+
106+
response = await SR.tags_add(uniqueNodeIds=[config_1_uid], tag=tag_1, **auth)
107+
assert len(response) == 1
108+
assert response[0]["uniqueId"] == config_1_uid
109+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1"]
110+
111+
response = await SR.tags_add(
112+
uniqueNodeIds=[config_1_uid, config_2_uid, folder_uid], tag=tag_2, **auth
113+
)
114+
assert len(response) == 3
115+
assert response[0]["uniqueId"] == config_1_uid
116+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1", "tag_2"]
117+
assert response[1]["uniqueId"] == config_2_uid
118+
assert [_["name"] for _ in response[1]["tags"]] == ["tag_2"]
119+
assert response[2]["uniqueId"] == folder_uid
120+
assert [_["name"] for _ in response[2]["tags"]] == ["tag_2"]
121+
122+
response = await SR.tags_get() # Returns the list of ALL tags
123+
assert len(response) == 4
124+
125+
response = await SR.tags_delete(
126+
uniqueNodeIds=[config_1_uid, folder_uid], tag={"name": "tag_2"}, **auth
127+
)
128+
assert len(response) == 2
129+
assert response[0]["uniqueId"] == config_1_uid
130+
assert [_["name"] for _ in response[0]["tags"]] == ["tag_1"]
131+
assert response[1]["uniqueId"] == folder_uid
132+
assert [_["name"] for _ in response[1]["tags"]] == []
133+
134+
response = await SR.tags_get()
135+
assert len(response) == 2
136+
119137

120138
asyncio.run(testing())

0 commit comments

Comments
 (0)