|
| 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 SnapshotSaveAsTestCase(AsyncHTTPTestCase): |
| 15 | + def get_app(self): |
| 16 | + return application |
| 17 | + |
| 18 | + def test_save_as(self): |
| 19 | + # Create a snapshot |
| 20 | + name = str(uuid4()) |
| 21 | + snapshot = self._save_as(name) |
| 22 | + |
| 23 | + # Assert is saved |
| 24 | + self.assertDictEqual( |
| 25 | + { |
| 26 | + "ok": True, |
| 27 | + "id": snapshot['id'], |
| 28 | + "title": name |
| 29 | + }, |
| 30 | + snapshot |
| 31 | + ) |
| 32 | + |
| 33 | + # Clear |
| 34 | + self.fetch("/snapshot/remove?id=" + str(snapshot['id'])) |
| 35 | + |
| 36 | + def test_save_as_deleted_snapshot(self): |
| 37 | + """ |
| 38 | + It's possible, because the snapshot is created based on pedalboard's |
| 39 | + current state instead of a last snapshot loaded |
| 40 | + """ |
| 41 | + # Create two snapshots |
| 42 | + # because it is necessary to exists at least one |
| 43 | + snapshot_1_name = str(uuid4()) |
| 44 | + snapshot_2_name = str(uuid4()) |
| 45 | + |
| 46 | + snapshot_1 = self._save_as(snapshot_1_name) |
| 47 | + snapshot_2 = self._save_as(snapshot_2_name) |
| 48 | + |
| 49 | + # Save snapshot created |
| 50 | + self.fetch("/snapshot/load?id=" + str(snapshot_2['id'])) |
| 51 | + response = self.post("/snapshot/save") |
| 52 | + |
| 53 | + # Assert is saved |
| 54 | + self.assertTrue(json.loads(response.body)) |
| 55 | + |
| 56 | + # Delete created snapshot |
| 57 | + self.fetch("/snapshot/remove?id=" + str(snapshot_2['id'])) |
| 58 | + |
| 59 | + # Save as deleted snapshot |
| 60 | + snapshot_3_name = str(uuid4()) |
| 61 | + snapshot_3 = self._save_as(snapshot_3_name) |
| 62 | + self.assertEqual(response.code, 200) |
| 63 | + self.assertDictEqual( |
| 64 | + { |
| 65 | + "ok": True, |
| 66 | + "id": snapshot_3['id'], |
| 67 | + "title": snapshot_3_name |
| 68 | + }, |
| 69 | + snapshot_3 |
| 70 | + ) |
| 71 | + |
| 72 | + # Clear |
| 73 | + self.fetch("/snapshot/remove?id=" + str(snapshot_3['id'])) |
| 74 | + self.fetch("/snapshot/remove?id=" + str(snapshot_1['id'])) |
| 75 | + |
| 76 | + def test_save_duplicated_name(self): |
| 77 | + snapshot_1_name = snapshot_2_name = str(uuid4()) |
| 78 | + |
| 79 | + self.assertEqual(snapshot_1_name, snapshot_2_name) |
| 80 | + |
| 81 | + snapshot_1 = self._save_as(snapshot_1_name) |
| 82 | + snapshot_2 = self._save_as(snapshot_2_name) |
| 83 | + |
| 84 | + self.assertNotEquals(snapshot_1['title'], snapshot_2['title']) |
| 85 | + self.assertTrue(snapshot_1['title'] < snapshot_2['title']) |
| 86 | + |
| 87 | + # Clear |
| 88 | + self.fetch("/snapshot/remove?id=" + str(snapshot_2['id'])) |
| 89 | + self.fetch("/snapshot/remove?id=" + str(snapshot_1['id'])) |
| 90 | + |
| 91 | + def post(self, url): |
| 92 | + self.http_client.fetch(HTTPRequest(self.get_url(url), "POST", allow_nonstandard_methods=True), self.stop) |
| 93 | + return self.wait() |
| 94 | + |
| 95 | + def _save_as(self, name): |
| 96 | + response = self.fetch("/snapshot/saveas?title=" + name) |
| 97 | + self.assertEqual(response.code, 200) |
| 98 | + |
| 99 | + return json.loads(response.body) |
0 commit comments