Skip to content

Commit 1d0c21e

Browse files
committed
TST: tests for config_update API
1 parent 90aecf5 commit 1d0c21e

File tree

4 files changed

+271
-118
lines changed

4 files changed

+271
-118
lines changed

src/save_and_restore_api/_api_threads.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def node_get_parent(self, uniqueNodeId):
121121

122122
def config_get(self, uniqueNodeId):
123123
"""
124-
Returns the config data for the node with specified node UID.
124+
Returns the config data for the node with specified node UID. Returns only the configuration
125+
data. To get the node metadata use ``node_get()``.
125126
126127
API: GET /config/{uniqueNodeId}
127128
"""
@@ -130,7 +131,19 @@ def config_get(self, uniqueNodeId):
130131

131132
def config_create(self, parentNodeId, *, configurationNode, configurationData, auth=None):
132133
"""
133-
Creates a new configuration node under the specified parent node.
134+
Creates a new configuration node under the specified parent node. Parameters:
135+
``configurationNode`` - the node metadata, ``configurationData`` - the configuration data.
136+
137+
Minimum required fields:
138+
139+
configurationNode = {"name": "test_config"}
140+
configurationData = {"pvList": [{"name": "PV1"}, {"name": "PV2"}]}
141+
142+
The fields ``uniqueId``, ``nodeType``, ``userName`` in ``configurationNode`` are ignored
143+
and overwritten by the server.
144+
145+
The function returns the dictionary with ``configurationNode`` and ``configurationData``
146+
as returned by the server.
134147
135148
API: PUT /config?parentNodeId={parentNodeId}
136149
"""
@@ -141,7 +154,9 @@ def config_create(self, parentNodeId, *, configurationNode, configurationData, a
141154

142155
def config_update(self, *, configurationNode, configurationData=None, auth=None):
143156
"""
144-
Updates an existing configuration node.
157+
Updates an existing configuration node. Parameters ``configurationNode`` and ``configurationData``
158+
should be loaded using ``node_get()`` and ``config_get()`` respectively. Both parameters must
159+
contain correct ``uniqueID`` field values.
145160
146161
API: POST /config
147162
"""

tests/common.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ def _is_async(library):
1818
raise ValueError(f"Unknown library: {library!r}")
1919

2020

21+
def _select_auth(*, SR, usesetauth):
22+
"""
23+
Switch between using ``SR.auth_set()`` to set authentication for the whole session or
24+
using ``SR.auth_gen()`` to generate authentication object and pass it with each request.
25+
26+
Parameters
27+
----------
28+
SR : SaveRestoreAPI
29+
Instance of SaveRestoreAPI
30+
usesetauth : bool
31+
If True, use ``SR.auth_set()``, otherwise use ``SR.auth_gen()``.
32+
33+
Returns
34+
-------
35+
dict
36+
If ``usesetauth`` is False, returns dictionary with authentication to be passed with each request,
37+
otherwise returns empty dictionary.
38+
"""
39+
auth = {}
40+
if usesetauth:
41+
SR.auth_set(username=user_username, password=user_password)
42+
else:
43+
auth["auth"] = SR.auth_gen(username=user_username, password=user_password)
44+
return auth
45+
46+
2147
@pytest.fixture
2248
def clear_sar():
2349
"""

tests/test_config_control.py

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from save_and_restore_api import SaveRestoreAPI as SaveRestoreAPI_Threads
6+
from save_and_restore_api.aio import SaveRestoreAPI as SaveRestoreAPI_Async
7+
8+
from .common import (
9+
_is_async,
10+
_select_auth,
11+
base_url,
12+
clear_sar, # noqa: F401
13+
user_password, # noqa: F401
14+
user_username,
15+
)
16+
17+
# =============================================================================================
18+
# CONFIGURATION-CONTROLLER API METHODS
19+
# =============================================================================================
20+
21+
22+
# fmt: off
23+
@pytest.mark.parametrize("usesetauth", [True, False])
24+
@pytest.mark.parametrize("library", ["THREADS", "ASYNC"])
25+
# fmt: on
26+
def test_config_create_01(clear_sar, library, usesetauth): # noqa: F811
27+
"""
28+
Tests for the 'config_create' and 'config_get' API.
29+
"""
30+
31+
pv_list = [
32+
{
33+
"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinX"
34+
},
35+
{
36+
"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinY",
37+
"comparison": {
38+
"comparisonMode": "ABSOLUTE",
39+
"tolerance": 2.7
40+
}
41+
},
42+
{
43+
"pvName": "13SIM1:{SimDetector-Cam:2}cam2:BinX",
44+
"readbackPvName": None,
45+
"readOnly": False,
46+
},
47+
{
48+
"pvName": "13SIM1:{SimDetector-Cam:2}cam2:BinY",
49+
"readbackPvName": None,
50+
"readOnly": False,
51+
}
52+
]
53+
54+
configurationNode = {
55+
"name": "Config",
56+
}
57+
configurationData = {
58+
"pvList": pv_list,
59+
}
60+
61+
if not _is_async(library):
62+
with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR:
63+
auth = _select_auth(SR=SR, usesetauth=usesetauth)
64+
65+
response = SR.node_add(SR.ROOT_NODE_UID, name="Child Folder", nodeType="FOLDER", **auth)
66+
folder_uid = response["uniqueId"]
67+
68+
69+
response = SR.config_create(
70+
folder_uid, configurationNode=configurationNode, configurationData=configurationData, **auth
71+
)
72+
assert response["configurationNode"]["name"] == "Config"
73+
assert response["configurationNode"]["nodeType"] == "CONFIGURATION"
74+
assert response["configurationNode"]["userName"] == user_username
75+
assert len(response["configurationData"]["pvList"]) == len(pv_list)
76+
77+
config_uid = response["configurationNode"]["uniqueId"]
78+
79+
response = SR.config_get(config_uid)
80+
assert response["uniqueId"] == config_uid
81+
assert len(response["pvList"]) == len(pv_list)
82+
83+
response = SR.node_get(config_uid)
84+
assert response["uniqueId"] == config_uid
85+
assert response["name"] == "Config"
86+
assert response["nodeType"] == "CONFIGURATION"
87+
assert response["userName"] == user_username
88+
89+
else:
90+
async def testing():
91+
async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR:
92+
auth = _select_auth(SR=SR, usesetauth=usesetauth)
93+
94+
response = await SR.node_add(SR.ROOT_NODE_UID, name="Child Folder", nodeType="FOLDER", **auth)
95+
folder_uid = response["uniqueId"]
96+
97+
response = await SR.config_create(
98+
folder_uid, configurationNode=configurationNode, configurationData=configurationData, **auth
99+
)
100+
assert response["configurationNode"]["name"] == "Config"
101+
assert response["configurationNode"]["nodeType"] == "CONFIGURATION"
102+
assert response["configurationNode"]["userName"] == user_username
103+
assert len(response["configurationData"]["pvList"]) == len(pv_list)
104+
105+
config_uid = response["configurationNode"]["uniqueId"]
106+
107+
response = await SR.config_get(config_uid)
108+
assert response["uniqueId"] == config_uid
109+
assert len(response["pvList"]) == len(pv_list)
110+
111+
response = await SR.node_get(config_uid)
112+
assert response["uniqueId"] == config_uid
113+
assert response["name"] == "Config"
114+
assert response["nodeType"] == "CONFIGURATION"
115+
assert response["userName"] == user_username
116+
117+
asyncio.run(testing())
118+
119+
120+
# fmt: off
121+
@pytest.mark.parametrize("usesetauth", [True, False])
122+
@pytest.mark.parametrize("library", ["THREADS", "ASYNC"])
123+
# fmt: on
124+
def test_config_update_01(clear_sar, library, usesetauth): # noqa: F811
125+
"""
126+
Tests for the 'config_update' API.
127+
"""
128+
129+
pv_list1 = [{"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinY"}]
130+
pv_list2 = [{"pvName": "13SIM1:{SimDetector-Cam:1}cam1:BinX"}]
131+
132+
if not _is_async(library):
133+
with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR:
134+
auth = _select_auth(SR=SR, usesetauth=usesetauth)
135+
136+
response = SR.node_add(SR.ROOT_NODE_UID, name="Child Folder", nodeType="FOLDER", **auth)
137+
folder_uid = response["uniqueId"]
138+
139+
response = SR.config_create(
140+
folder_uid, configurationNode={"name": "Config"}, configurationData={"pvList": pv_list1}, **auth
141+
)
142+
configurationNode = response["configurationNode"]
143+
configurationData = response["configurationData"]
144+
145+
assert configurationNode["name"] == "Config"
146+
assert configurationNode["nodeType"] == "CONFIGURATION"
147+
assert configurationNode["userName"] == user_username
148+
assert len(configurationData["pvList"]) == 1
149+
assert configurationData["pvList"][0]["pvName"] == pv_list1[0]["pvName"]
150+
151+
config_uid = configurationNode["uniqueId"]
152+
153+
configurationData["pvList"] = pv_list2
154+
response = SR.config_update(
155+
configurationNode=configurationNode, configurationData=configurationData, **auth
156+
)
157+
158+
configurationNode = response["configurationNode"]
159+
configurationData = response["configurationData"]
160+
161+
assert configurationNode["name"] == "Config"
162+
assert configurationNode["uniqueId"] == config_uid
163+
assert configurationData["pvList"][0]["pvName"] == pv_list2[0]["pvName"]
164+
assert configurationData["uniqueId"] == config_uid
165+
166+
response = SR.config_get(config_uid)
167+
assert response["uniqueId"] == config_uid
168+
assert len(response["pvList"]) == len(pv_list2)
169+
assert response["pvList"][0]["pvName"] == pv_list2[0]["pvName"]
170+
171+
response = SR.node_get(config_uid)
172+
assert response["uniqueId"] == config_uid
173+
assert response["name"] == "Config"
174+
assert response["nodeType"] == "CONFIGURATION"
175+
assert response["userName"] == user_username
176+
177+
else:
178+
async def testing():
179+
async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR:
180+
auth = _select_auth(SR=SR, usesetauth=usesetauth)
181+
182+
response = await SR.node_add(SR.ROOT_NODE_UID, name="Child Folder", nodeType="FOLDER", **auth)
183+
folder_uid = response["uniqueId"]
184+
185+
response = await SR.config_create(
186+
folder_uid,
187+
configurationNode={"name": "Config"},
188+
configurationData={"pvList": pv_list1},
189+
**auth,
190+
)
191+
configurationNode = response["configurationNode"]
192+
configurationData = response["configurationData"]
193+
194+
assert configurationNode["name"] == "Config"
195+
assert configurationNode["nodeType"] == "CONFIGURATION"
196+
assert configurationNode["userName"] == user_username
197+
assert len(configurationData["pvList"]) == 1
198+
assert configurationData["pvList"][0]["pvName"] == pv_list1[0]["pvName"]
199+
200+
config_uid = configurationNode["uniqueId"]
201+
202+
configurationData["pvList"] = pv_list2
203+
response = await SR.config_update(
204+
configurationNode=configurationNode, configurationData=configurationData, **auth
205+
)
206+
207+
configurationNode = response["configurationNode"]
208+
configurationData = response["configurationData"]
209+
210+
assert configurationNode["name"] == "Config"
211+
assert configurationNode["uniqueId"] == config_uid
212+
assert configurationData["pvList"][0]["pvName"] == pv_list2[0]["pvName"]
213+
assert configurationData["uniqueId"] == config_uid
214+
215+
response = await SR.config_get(config_uid)
216+
assert response["uniqueId"] == config_uid
217+
assert len(response["pvList"]) == len(pv_list2)
218+
assert response["pvList"][0]["pvName"] == pv_list2[0]["pvName"]
219+
220+
response = await SR.node_get(config_uid)
221+
assert response["uniqueId"] == config_uid
222+
assert response["name"] == "Config"
223+
assert response["nodeType"] == "CONFIGURATION"
224+
assert response["userName"] == user_username
225+
226+
asyncio.run(testing())

0 commit comments

Comments
 (0)