From 6e55148a76140b0e24435a4bcf2478174e2d90c8 Mon Sep 17 00:00:00 2001 From: kaliko Date: Mon, 10 May 2021 11:05:14 +0200 Subject: [PATCH] simadb: Fixed early connection close --- sima/lib/simadb.py | 10 +++++++--- tests/test_simadb.py | 12 +++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index 5646a49..e52386c 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -45,8 +45,7 @@ class SimaDB: def get_database_connection(self): """get database reference""" - connection = sqlite3.connect( - self._db_path, isolation_level=None) + connection = sqlite3.connect(self._db_path, isolation_level=None) return connection def get_info(self): @@ -383,7 +382,8 @@ class SimaDB: connection.close() return row[0] if not add: # Not adding non existing track - connection.close() + if not with_connection: + connection.close() return None # Get an artist record or None if track.artist: @@ -609,6 +609,8 @@ class SimaDB: "SELECT id FROM blocklist WHERE track = ?", (track_id,)) if not rows.fetchone(): if not add: + if not with_connection: + connection.close() return None connection.execute('INSERT INTO blocklist (track) VALUES (?)', (track_id,)) @@ -634,6 +636,8 @@ class SimaDB: "SELECT id FROM blocklist WHERE album = ?", (album_id,)) if not rows.fetchone(): if not add: + if not with_connection: + connection.close() return None connection.execute('INSERT INTO blocklist (album) VALUES (?)', (album_id,)) diff --git a/tests/test_simadb.py b/tests/test_simadb.py index a577916..c3e400d 100644 --- a/tests/test_simadb.py +++ b/tests/test_simadb.py @@ -70,7 +70,7 @@ class Test_00DB(Main): def test_01_add_track(self): trk = Track(**DEVOLT) trk_id = self.db.get_track(trk) - self.assertEqual(trk_id, self.db.get_track(trk), + self.assertEqual(trk_id, self.db.get_track(trk, add=False), 'Same track, same record') def test_02_history(self): @@ -248,6 +248,8 @@ class Test_00DB(Main): class Test_01BlockList(Main): def test_blocklist_addition(self): + trk = Track(file='/foo/bar/baz', name='title') + self.assertIs(self.db.get_bl_track(trk, add=False), None) tracks_ids = list() # Set 6 records, same album for i in range(1, 6): # starts at 1 to ensure records are in the past @@ -255,16 +257,16 @@ class Test_01BlockList(Main): albumartist='fooalbart', album='foolbum',) # Add track, save its DB id tracks_ids.append(self.db.get_track(trk)) - # set records in the past to ease purging then - last = CURRENT - datetime.timedelta(minutes=i) - self.db.add_history(trk, date=last) # Add to history if i == 1: self.db.get_bl_track(trk) + self.assertIsNot(self.db.get_bl_track(trk, add=False), None) if i == 2: - self.db.get_bl_track(trk) self.db.get_bl_album(Album(name=trk.album)) if i == 3: self.db.get_bl_artist(trk.Artist) + if i == 4: + self.assertIs(self.db.get_bl_track(trk, add=False), None) + def test_blocklist_triggers_00(self): trk01 = Track(file='01', name='01', artist='artist A', album='album A') -- 2.39.5