Skip to content

Commit 7bb98a8

Browse files
committed
TST: unit tests for snapshot_add, snapshot_update, snapshot_get API
1 parent 77c792e commit 7bb98a8

File tree

5 files changed

+355
-16
lines changed

5 files changed

+355
-16
lines changed

src/save_and_restore_api/_api_async.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def config_create(self, parentNodeId, *, configurationNode, configurationD
101101
)
102102
return await self.send_request(method, url, params=params, auth=auth)
103103

104-
async def config_update(self, *, configurationNode, configurationData=None, auth=None):
104+
async def config_update(self, *, configurationNode, configurationData, auth=None):
105105
# Reusing docstrings from the threaded version
106106
method, url, params = self._prepare_config_update(
107107
configurationNode=configurationNode, configurationData=configurationData
@@ -131,17 +131,57 @@ async def tags_delete(self, *, uniqueNodeIds, tag, auth=None):
131131
# TAKE-SNAPSHOT-CONTROLLER API METHODS
132132
# =============================================================================================
133133

134-
def take_snapshot_get(self, uniqueNodeId):
134+
async def take_snapshot_get(self, uniqueNodeId):
135135
# Reusing docstrings from the threaded version
136136
method, url = self._prepare_take_snapshot_get(uniqueNodeId=uniqueNodeId)
137-
return self.send_request(method, url)
137+
return await self.send_request(method, url)
138138

139-
def take_snapshot_save(self, uniqueNodeId, *, name=None, comment=None, auth=None):
139+
async def take_snapshot_save(self, uniqueNodeId, *, name=None, comment=None, auth=None):
140140
# Reusing docstrings from the threaded version
141141
method, url, url_params = self._prepare_take_snapshot_save(
142142
uniqueNodeId=uniqueNodeId, name=name, comment=comment
143143
)
144-
return self.send_request(method, url, url_params=url_params, auth=auth)
144+
return await self.send_request(method, url, url_params=url_params, auth=auth)
145+
146+
# =============================================================================================
147+
# SNAPSHOT-CONTROLLER API METHODS
148+
# =============================================================================================
149+
150+
async def snapshot_get(self, uniqueId):
151+
# Reusing docstrings from the threaded version
152+
method, url = self._prepare_snapshot_get(uniqueId=uniqueId)
153+
return await self.send_request(method, url)
154+
155+
async def snapshot_add(self, parentNodeId, *, snapshotNode, snapshotData, auth=None):
156+
"""
157+
Upload data for the new snapshot and save it to the database. The new node is created
158+
under the existing configuration node specified by ``parentNodeId``.
159+
160+
API: PUT /snapshot?parentNodeId={parentNodeId}
161+
"""
162+
method, url, params = self._prepare_snapshot_add(
163+
parentNodeId=parentNodeId, snapshotNode=snapshotNode, snapshotData=snapshotData
164+
)
165+
return await self.send_request(method, url, params=params, auth=auth)
166+
167+
async def snapshot_update(self, *, snapshotNode, snapshotData, auth=None):
168+
"""
169+
Upload and update data for an existing snapshot. Both ``snapshotNode`` and ``snapshotData``
170+
must have valid ``uniqueId`` fields pointing to an existing node.
171+
172+
API: POST /snapshot
173+
"""
174+
method, url, params = self._prepare_snapshot_update(snapshotNode=snapshotNode, snapshotData=snapshotData)
175+
return await self.send_request(method, url, params=params, auth=auth)
176+
177+
async def snapshots_get(self):
178+
"""
179+
Returns a list of all existing snapshots (list of ``snapshotNode`` objects).
180+
181+
API: GET /snapshots
182+
"""
183+
method, url = self._prepare_snapshots_get()
184+
return await self.send_request(method, url)
145185

146186

147187
SaveRestoreAPI.node_get.__doc__ = _SaveRestoreAPI_Threads.node_get.__doc__
@@ -159,3 +199,7 @@ def take_snapshot_save(self, uniqueNodeId, *, name=None, comment=None, auth=None
159199
SaveRestoreAPI.tags_delete.__doc__ = _SaveRestoreAPI_Threads.tags_delete.__doc__
160200
SaveRestoreAPI.take_snapshot_get.__doc__ = _SaveRestoreAPI_Threads.take_snapshot_get.__doc__
161201
SaveRestoreAPI.take_snapshot_save.__doc__ = _SaveRestoreAPI_Threads.take_snapshot_save.__doc__
202+
SaveRestoreAPI.snapshot_get.__doc__ = _SaveRestoreAPI_Threads.snapshot_get.__doc__
203+
SaveRestoreAPI.snapshot_add.__doc__ = _SaveRestoreAPI_Threads.snapshot_add.__doc__
204+
SaveRestoreAPI.snapshot_update.__doc__ = _SaveRestoreAPI_Threads.snapshot_update.__doc__
205+
SaveRestoreAPI.snapshots_get.__doc__ = _SaveRestoreAPI_Threads.snapshots_get.__doc__

src/save_and_restore_api/_api_base.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ def _prepare_config_create(self, *, parentNodeId, configurationNode, configurati
219219

220220
def _prepare_config_update(self, *, configurationNode, configurationData):
221221
method, url = "POST", "/config"
222-
configurationData = configurationData or {}
223222
params = {"configurationNode": configurationNode, "configurationData": configurationData}
224223
return method, url, params
225224

@@ -250,10 +249,32 @@ def _prepare_take_snapshot_get(self, *, uniqueNodeId):
250249
return method, url
251250

252251
def _prepare_take_snapshot_save(self, *, uniqueNodeId, name, comment):
253-
method, url = "PUT", "/take-snapshot"
252+
method, url = "PUT", f"/take-snapshot/{uniqueNodeId}"
254253
url_params = {"name": name, "comment": comment}
255254
return method, url, url_params
256255

256+
# =============================================================================================
257+
# SNAPSHOT-CONTROLLER API METHODS
258+
# =============================================================================================
259+
260+
def _prepare_snapshot_get(self, *, uniqueId):
261+
method, url = "GET", f"/snapshot/{uniqueId}"
262+
return method, url
263+
264+
def _prepare_snapshot_add(self, *, parentNodeId, snapshotNode, snapshotData):
265+
method, url = "PUT", f"/snapshot?parentNodeId={parentNodeId}"
266+
params = {"snapshotNode": snapshotNode, "snapshotData": snapshotData}
267+
return method, url, params
268+
269+
def _prepare_snapshot_update(self, *, snapshotNode, snapshotData):
270+
method, url = "POST", "/snapshot"
271+
params = {"snapshotNode": snapshotNode, "snapshotData": snapshotData}
272+
return method, url, params
273+
274+
def _prepare_snapshots_get(self):
275+
method, url = "GET", "/snapshots"
276+
return method, url
277+
257278
# =============================================================================================
258279

259280
# def create_config(self, parent_node_uid, name, pv_list):

src/save_and_restore_api/_api_threads.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def config_create(self, parentNodeId, *, configurationNode, configurationData, a
152152
)
153153
return self.send_request(method, url, params=params, auth=auth)
154154

155-
def config_update(self, *, configurationNode, configurationData=None, auth=None):
155+
def config_update(self, *, configurationNode, configurationData, auth=None):
156156
"""
157157
Updates an existing configuration node. Parameters ``configurationNode`` and ``configurationData``
158158
should be loaded using ``node_get()`` and ``config_get()`` respectively. Both parameters must
@@ -204,8 +204,9 @@ def tags_delete(self, *, uniqueNodeIds, tag, auth=None):
204204

205205
def take_snapshot_get(self, uniqueNodeId):
206206
"""
207-
Reads and returns PV values based on configuration specified by ``uniqueNodeId``.
208-
The API does not create any nodes in the database.
207+
Reads and returns a list of PV values based on configuration specified by
208+
``uniqueNodeId``. The API does not create any nodes in the database.
209+
The returned list format matches the format of ``snapshotData["snapshotItems"]``.
209210
210211
API: GET /take-snapshot/{uniqueNodeId}
211212
"""
@@ -224,3 +225,47 @@ def take_snapshot_save(self, uniqueNodeId, *, name=None, comment=None, auth=None
224225
uniqueNodeId=uniqueNodeId, name=name, comment=comment
225226
)
226227
return self.send_request(method, url, url_params=url_params, auth=auth)
228+
229+
# =============================================================================================
230+
# SNAPSHOT-CONTROLLER API METHODS
231+
# =============================================================================================
232+
233+
def snapshot_get(self, uniqueId):
234+
"""
235+
Returns snapshot data (``snapshotData``) for the snapshot specified by ``uniqueId``.
236+
237+
API: GET /snapshot/{uniqueId}
238+
"""
239+
method, url = self._prepare_snapshot_get(uniqueId=uniqueId)
240+
return self.send_request(method, url)
241+
242+
def snapshot_add(self, parentNodeId, *, snapshotNode, snapshotData, auth=None):
243+
"""
244+
Upload data for the new snapshot and save it to the database. The new node is created
245+
under the existing configuration node specified by ``parentNodeId``.
246+
247+
API: PUT /snapshot?parentNodeId={parentNodeId}
248+
"""
249+
method, url, params = self._prepare_snapshot_add(
250+
parentNodeId=parentNodeId, snapshotNode=snapshotNode, snapshotData=snapshotData
251+
)
252+
return self.send_request(method, url, params=params, auth=auth)
253+
254+
def snapshot_update(self, *, snapshotNode, snapshotData, auth=None):
255+
"""
256+
Upload and update data for an existing snapshot. Both ``snapshotNode`` and ``snapshotData``
257+
must have valid ``uniqueId`` fields pointing to an existing node.
258+
259+
API: POST /snapshot
260+
"""
261+
method, url, params = self._prepare_snapshot_update(snapshotNode=snapshotNode, snapshotData=snapshotData)
262+
return self.send_request(method, url, params=params, auth=auth)
263+
264+
def snapshots_get(self):
265+
"""
266+
Returns a list of all existing snapshots (list of ``snapshotNode`` objects).
267+
268+
API: GET /snapshots
269+
"""
270+
method, url = self._prepare_snapshots_get()
271+
return self.send_request(method, url)

tests/common.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ def _clear():
9494
uids = [root_folder_uid]
9595
while n_uid < len(uids):
9696
uid = uids[n_uid]
97-
res_1 = SR.node_get(uid)
98-
if res_1["nodeType"] == "FOLDER":
99-
res_2 = SR.node_get_children(uid)
100-
ch_uids = [_["uniqueId"] for _ in res_2]
101-
if ch_uids:
102-
uids.extend(ch_uids)
97+
children = SR.node_get_children(uid)
98+
ch_uids = [_["uniqueId"] for _ in children]
99+
if ch_uids:
100+
uids.extend(ch_uids)
103101
n_uid += 1
104102

105103
# Delete all nodes starting with children, including the root folder

0 commit comments

Comments
 (0)