From b57b086e040c33e4643fd107ac20b398e053c151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alban=20F=C3=A9ron?= Date: Sat, 14 Jan 2023 16:45:44 +0100 Subject: [PATCH] Enforce foreign keys on SQLite --- supysonic/db.py | 1 + tests/api/test_album_songs.py | 1 + tests/base/test_db.py | 12 ++++++------ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/supysonic/db.py b/supysonic/db.py index 7086834..53ac7dc 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -588,6 +588,7 @@ def init_database(database_uri): provider = "postgres" elif uri.scheme.startswith("sqlite"): provider = "sqlite" + args["pragmas"] = {"foreign_keys": 1} else: raise RuntimeError(f"Unsupported database: {uri.scheme}") diff --git a/tests/api/test_album_songs.py b/tests/api/test_album_songs.py index 055983c..17578f8 100644 --- a/tests/api/test_album_songs.py +++ b/tests/api/test_album_songs.py @@ -137,6 +137,7 @@ class AlbumSongsTestCase(ApiTestBase): ) self.assertEqual(len(child), 0) + Track.delete().execute() Folder[1].delete_instance() rv, child = self._make_request( "getAlbumList", {"type": "random"}, tag="albumList" diff --git a/tests/base/test_db.py b/tests/base/test_db.py index 7acbc34..bde95d0 100644 --- a/tests/base/test_db.py +++ b/tests/base/test_db.py @@ -1,7 +1,7 @@ # This file is part of Supysonic. # Supysonic is a Python implementation of the Subsonic server API. # -# Copyright (C) 2017-2022 Alban 'spl0k' Féron +# Copyright (C) 2017-2023 Alban 'spl0k' Féron # # Distributed under terms of the GNU AGPLv3 license. @@ -10,6 +10,7 @@ import unittest import uuid from collections import namedtuple +from peewee import IntegrityError from supysonic import db @@ -20,11 +21,6 @@ class DbTestCase(unittest.TestCase): def setUp(self): db.init_database("sqlite:") - try: - self.assertRegex - except AttributeError: - self.assertRegex = self.assertRegexpMatches - def tearDown(self): db.release_database() @@ -114,6 +110,10 @@ class DbTestCase(unittest.TestCase): return playlist + def test_ensure_sqlite_foreign_keys(self): + root, _, _ = self.create_some_folders() + self.assertRaises(IntegrityError, root.delete_instance) + def test_folder_base(self): root_folder, child_folder, child_noart = self.create_some_folders() track_embededart = self.create_track_in(child_noart, root_folder)