]> kaliko git repositories - mpd-sima.git/commitdiff
simadb: Fixed early connection close
authorkaliko <kaliko@azylum.org>
Mon, 10 May 2021 09:05:14 +0000 (11:05 +0200)
committerkaliko <kaliko@azylum.org>
Mon, 10 May 2021 09:05:14 +0000 (11:05 +0200)
sima/lib/simadb.py
tests/test_simadb.py

index 5646a498f4632f4f11f01b346af6beeaa1ed0a3c..e52386cb6fe7554814f0323a70b6836fb8f95982 100644 (file)
@@ -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,))
index a57791695613b181c89b99abd45c8157c43a3b31..c3e400deb00f7e35a57b6607be2c3b1020e8e39a 100644 (file)
@@ -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')