Skip to content

Commit 4728fce

Browse files
committed
Refactor: SnapshotList
1 parent e1663e7 commit 4728fce

4 files changed

Lines changed: 94 additions & 28 deletions

File tree

mod/controller/rest/snapshot.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class SnapshotName(JsonRequestHandler):
1010

11-
1211
# TODO: Replace GET /snapshot/name
1312
# to GET /pedalboards/<pedalboard id>/snapshots/
1413
def get(self):
@@ -31,3 +30,25 @@ def get(self):
3130
'ok': bool(name), # FIXME: Always true
3231
'name': name
3332
})
33+
34+
35+
class SnapshotList(JsonRequestHandler):
36+
37+
# TODO: Replace GET /snapshot/list
38+
# to GET /pedalboards/<pedalboard id>/snapshots/
39+
def get(self):
40+
"""
41+
Get snapshots name of the loaded pedalboard
42+
43+
.. code-block:: json
44+
45+
{
46+
0: "First snapshot",
47+
1: "Second snapshot"
48+
}
49+
50+
:return: names of the current pedalboard snapshots
51+
"""
52+
snapshots = SESSION.host.pedalboard_snapshots
53+
snapshots = dict((i, snapshots[i]['name']) for i in range(len(snapshots)) if snapshots[i] is not None)
54+
self.write(snapshots)

mod/webserver.py

Lines changed: 1 addition & 7 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
25+
from mod.controller.rest.snapshot import SnapshotName, SnapshotList
2626

2727
try:
2828
from signal import signal, SIGUSR1, SIGUSR2
@@ -1600,12 +1600,6 @@ def get(self):
16001600
ok = SESSION.host.snapshot_remove(idx)
16011601
self.write(ok)
16021602

1603-
class SnapshotList(JsonRequestHandler):
1604-
def get(self):
1605-
snapshots = SESSION.host.pedalboard_snapshots
1606-
snapshots = dict((i, snapshots[i]['name']) for i in range(len(snapshots)) if snapshots[i] is not None)
1607-
self.write(snapshots)
1608-
16091603
class SnapshotLoad(JsonRequestHandler):
16101604
@web.asynchronous
16111605
@gen.engine
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.testing import AsyncHTTPTestCase
9+
10+
from mod.settings import DEFAULT_SNAPSHOT_NAME
11+
from mod.webserver import application
12+
13+
14+
class SnapshotListTestCase(AsyncHTTPTestCase):
15+
def get_app(self):
16+
return application
17+
18+
# Pedalboard starts empty, but after there is one, it isn't possible to exclude all of them
19+
# def test_empty(self):
20+
# response = self.fetch("/snapshot/list")
21+
#
22+
# self.assertEqual(response.code, 200)
23+
# self.assert_equal([], response.body)
24+
25+
def test_populated_name(self):
26+
original_snapshots = json.loads(self.fetch("/snapshot/list").body)
27+
28+
# Populate
29+
expected_name_0 = str(uuid4())
30+
expected_name_1 = str(uuid4())
31+
32+
self.fetch("/snapshot/saveas?title=" + expected_name_0)
33+
self.fetch("/snapshot/saveas?title=" + expected_name_1)
34+
35+
response = self.fetch("/snapshot/list")
36+
37+
# Assert list
38+
new_snapshots = self._names(original_snapshots) + [expected_name_0, expected_name_1]
39+
self.assert_equal(new_snapshots, response.body)
40+
41+
# Clear created
42+
for index in reversed(range(len(original_snapshots), len(new_snapshots)+1)):
43+
self.fetch("/snapshot/remove?id=" + str(index))
44+
45+
def assert_equal(self, lista, body):
46+
self.assertDictEqual(
47+
{str(index): item for index, item in enumerate(lista)},
48+
json.loads(body)
49+
)
50+
51+
def _names(self, snapshots):
52+
return list(snapshots.values())

test/controller/snapshot_controllers_test.py renamed to test/controller/snapshot_name_test.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44
# This test uses coroutine style.
55
import json
6+
from uuid import uuid4
67

78
from tornado.testing import AsyncHTTPTestCase
89

@@ -18,41 +19,31 @@ def test_name_missing_snapshot(self):
1819
response = self.fetch("/snapshot/name?id=1000")
1920

2021
self.assertEqual(response.code, 200)
21-
self.assertDictEqual(
22-
{
23-
'ok': True, # FIXME: Always true
24-
'name': DEFAULT_SNAPSHOT_NAME
25-
},
26-
json.loads(response.body)
27-
)
22+
self.assert_equal(DEFAULT_SNAPSHOT_NAME, response.body)
2823

2924
def test_name_negative_snapshot(self):
3025
response = self.fetch("/snapshot/name?id=-1")
3126

3227
self.assertEqual(response.code, 200)
33-
self.assertDictEqual(
34-
{
35-
'ok': True, # FIXME: Always true
36-
'name': DEFAULT_SNAPSHOT_NAME
37-
},
38-
json.loads(response.body)
39-
)
28+
self.assert_equal(DEFAULT_SNAPSHOT_NAME, response.body)
4029

4130
def test_name(self):
42-
expected_name_0 = 'Title-0'
43-
expected_name_1 = 'Title-1'
31+
expected_name_0 = str(uuid4())
32+
expected_name_1 = str(uuid4())
4433

4534
self.fetch("/snapshot/saveas?title=" + expected_name_0)
4635
self.fetch("/snapshot/saveas?title=" + expected_name_1)
4736

48-
response_0 = self.fetch("/snapshot/name?id=0")
49-
response_1 = self.fetch("/snapshot/name?id=1")
37+
snapshots = self.saved_items()
38+
39+
response_0 = self.fetch("/snapshot/name?id=" + snapshots[expected_name_0])
40+
response_1 = self.fetch("/snapshot/name?id=" + snapshots[expected_name_1])
5041

5142
self.assert_equal(expected_name_0, response_0.body)
5243
self.assert_equal(expected_name_1, response_1.body)
5344

54-
self.fetch("/snapshot/remove?id=1")
55-
self.fetch("/snapshot/remove?id=0")
45+
for index in reversed(range(len(snapshots), 0)):
46+
self.fetch("/snapshot/remove?id=" + str(index))
5647

5748
def assert_equal(self, name, body):
5849
self.assertDictEqual(
@@ -62,3 +53,11 @@ def assert_equal(self, name, body):
6253
},
6354
json.loads(body)
6455
)
56+
57+
def saved_items(self):
58+
items = self.fetch("/snapshot/list")
59+
60+
return {
61+
value: key
62+
for key, value in json.loads(items.body).items()
63+
}

0 commit comments

Comments
 (0)