Skip to content

Commit d51d64a

Browse files
committed
Refactor: SnapshotSave
1 parent 4728fce commit d51d64a

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

mod/controller/rest/snapshot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ def get(self):
5252
snapshots = SESSION.host.pedalboard_snapshots
5353
snapshots = dict((i, snapshots[i]['name']) for i in range(len(snapshots)) if snapshots[i] is not None)
5454
self.write(snapshots)
55+
56+
57+
class SnapshotSave(JsonRequestHandler):
58+
59+
# TODO: Replace POST /snapshot/save
60+
# to POST /pedalboards/current/snapshots/current
61+
def post(self):
62+
"""
63+
Update the current snapshot status
64+
65+
:return: `true` if it was successfully updated
66+
"""
67+
ok = SESSION.host.snapshot_save()
68+
self.write(ok)

mod/webserver.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from mod.controller.handler.json_request_handler import JsonRequestHandler
2424
from mod.controller.handler.timeless_request_handler import TimelessRequestHandler
25-
from mod.controller.rest.snapshot import SnapshotName, SnapshotList
25+
from mod.controller.rest.snapshot import SnapshotName, SnapshotList, SnapshotSave
2626

2727
try:
2828
from signal import signal, SIGUSR1, SIGUSR2
@@ -1555,11 +1555,6 @@ def post(self, mode):
15551555
ok = yield gen.Task(SESSION.web_set_sync_mode, transport_sync)
15561556
self.write(ok)
15571557

1558-
class SnapshotSave(JsonRequestHandler):
1559-
def post(self):
1560-
ok = SESSION.host.snapshot_save()
1561-
self.write(ok)
1562-
15631558
class SnapshotSaveAs(JsonRequestHandler):
15641559
@web.asynchronous
15651560
@gen.engine

test/controller/snapshot_list_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from tornado.testing import AsyncHTTPTestCase
99

10-
from mod.settings import DEFAULT_SNAPSHOT_NAME
1110
from mod.webserver import application
1211

1312

test/controller/snapshot_save_test.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
# SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
3+
# SPDX-License-Identifier: AGPL-3.0-or-later
4+
# This test uses coroutine style.
5+
import json
6+
from uuid import uuid4
7+
8+
from tornado.httpclient import HTTPRequest
9+
from tornado.testing import AsyncHTTPTestCase
10+
11+
from mod.webserver import application
12+
13+
14+
class SnapshotSaveTestCase(AsyncHTTPTestCase):
15+
def get_app(self):
16+
return application
17+
18+
def test_save(self):
19+
# Create a snapshot
20+
name = str(uuid4())
21+
snapshot_index = self._save_as(name)
22+
23+
# Load and save snapshot created
24+
self.fetch("/snapshot/load?id=" + str(snapshot_index))
25+
response = self.post("/snapshot/save")
26+
27+
# Assert is saved
28+
self.assertEqual(response.code, 200)
29+
self.assertTrue(json.loads(response.body))
30+
31+
# Clear
32+
self.fetch("/snapshot/remove?id=" + str(snapshot_index))
33+
34+
def test_save_deleted_snapshot(self):
35+
# Create two snapshots
36+
# because it is necessary to exists at least one
37+
snapshot_1 = str(uuid4())
38+
snapshot_2 = str(uuid4())
39+
40+
snapshot_1_index = self._save_as(snapshot_1)
41+
snapshot_2_index = self._save_as(snapshot_2)
42+
43+
# Save snapshot created
44+
self.fetch("/snapshot/load?id=" + str(snapshot_2_index))
45+
response = self.post("/snapshot/save")
46+
47+
# Assert is saved
48+
self.assertEqual(response.code, 200)
49+
self.assertTrue(json.loads(response.body))
50+
51+
# Delete created snapshot
52+
self.fetch("/snapshot/remove?id=" + str(snapshot_2_index))
53+
54+
# Try to save deleted snapshot
55+
response = self.post("/snapshot/save")
56+
self.assertEqual(response.code, 200)
57+
self.assertFalse(json.loads(response.body))
58+
59+
# Clear
60+
self.fetch("/snapshot/remove?id=" + str(snapshot_1_index))
61+
62+
def post(self, url):
63+
self.http_client.fetch(HTTPRequest(self.get_url(url), "POST", allow_nonstandard_methods=True), self.stop)
64+
return self.wait()
65+
66+
def _save_as(self, name):
67+
response = json.loads(self.fetch("/snapshot/saveas?title=" + name).body)
68+
return response['id']

0 commit comments

Comments
 (0)