7 from sima.lib.db import SimaDB
8 from sima.lib.track import Track
9 from sima.lib.meta import Album
14 'albumartist': 'Devolt',
15 'albumartistsort': 'Devolt',
19 'file': 'music/Devolt/2011-Grey/03-Devolt - Crazy.mp3',
20 'last-modified': '2012-04-02T20:48:59Z',
21 'musicbrainz_albumartistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99',
22 'musicbrainz_albumid': 'ea2ef2cf-59e1-443a-817e-9066e3e0be4b',
23 'musicbrainz_artistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99',
24 'musicbrainz_trackid': 'fabf8fc9-2ae5-49c9-8214-a839c958d872',
26 'duration': '220.000',
30 DB_FILE = 'file::memory:?cache=shared'
31 KEEP_FILE = False # File db in file to ease debug
33 DB_FILE = '/dev/shm/unittest.sqlite'
34 CURRENT = datetime.datetime.utcnow()
35 IN_THE_PAST = CURRENT - datetime.timedelta(hours=1)
38 class Main(unittest.TestCase):
39 """Deal with database creation and purge between tests"""
43 self.db = SimaDB(db_path=DB_FILE)
46 # Maintain a connection to keep the database (when stored in memory)
47 self.conn = self.db.get_database_connection()
57 def tearDownClass(self):
60 if os.path.isfile(DB_FILE):
64 class Test_00DB(Main):
66 def test_00_recreation(self):
69 def test_01_add_track(self):
71 trk_id = self.db.get_track(trk)
72 self.assertEqual(trk_id, self.db.get_track(trk),
73 'Same track, same record')
75 def test_02_history(self):
76 # set records in the past to ease purging then
77 last = CURRENT - datetime.timedelta(hours=1)
79 self.db.add_history(trk, date=last)
80 self.db.add_history(trk, date=last)
81 hist = self.db.fetch_history()
82 self.assertEqual(len(hist), 1, 'same track results in a single record')
84 trk_foo = Track(file="/foo/bar/baz.flac")
85 self.db.add_history(trk_foo, date=last)
86 hist = self.db.fetch_history()
87 self.assertEqual(len(hist), 2)
89 self.db.add_history(trk, date=last)
90 hist = self.db.fetch_history()
91 self.assertEqual(len(hist), 2)
92 self.db.purge_history(duration=0)
93 hist = self.db.fetch_history()
94 self.assertEqual(len(hist), 0)
96 # Controls we got history in the right order
97 # recent first, oldest last
99 for i in range(1, 5): # starts at 1 to ensure records are in the past
100 trk = Track(file=f'/foo/bar.{i}', name='{i}-baz', album='foolbum')
102 last = CURRENT - datetime.timedelta(minutes=i)
103 self.db.add_history(trk, date=last)
104 hist_records = self.db.fetch_history()
105 self.assertEqual(hist, hist_records)
106 self.db.purge_history(duration=0)
108 def test_history_to_tracks(self):
111 trk01 = Track(file='01', **tr)
112 self.db.add_history(trk01, CURRENT-datetime.timedelta(minutes=1))
114 tr.pop('musicbrainz_artistid')
115 trk02 = Track(file='02', **tr)
116 self.db.add_history(trk02, CURRENT-datetime.timedelta(minutes=2))
118 tr.pop('musicbrainz_albumid')
119 trk03 = Track(file='03', **tr)
120 self.db.add_history(trk03, CURRENT-datetime.timedelta(minutes=3))
122 tr.pop('musicbrainz_albumartistid')
123 trk04 = Track(file='04', **tr)
124 self.db.add_history(trk04, CURRENT-datetime.timedelta(minutes=4))
126 tr.pop('musicbrainz_trackid')
127 trk05 = Track(file='05', **tr)
128 self.db.add_history(trk05, CURRENT-datetime.timedelta(minutes=5))
129 history = self.db.fetch_history()
130 self.assertEqual(len(history), 5)
131 # Controls history ordering, recent first
132 self.assertEqual(history, [trk01, trk02, trk03, trk04, trk05])
134 def test_history_to_artists(self):
137 tr.pop('musicbrainz_artistid')
139 trk01 = Track(file='01', **tr)
140 self.db.add_history(trk01, CURRENT-datetime.timedelta(hours=1))
142 trk02 = Track(file='02', **tr)
143 self.db.add_history(trk02, CURRENT-datetime.timedelta(hours=1))
144 self.db.add_history(trk02, CURRENT-datetime.timedelta(hours=1))
146 trk03 = Track(file='03', **tr)
147 self.db.add_history(trk03, CURRENT-datetime.timedelta(hours=1))
148 # got multiple tracks, same artist, got artist history len == 1
149 art_history = self.db.fetch_artists_history()
150 self.assertEqual(len(art_history), 1)
151 self.assertEqual(art_history, [trk01.Artist])
153 # Now add new artist to history
154 trk04 = Track(file='04', artist='New Art')
155 trk05 = Track(file='05', artist='New² Art')
156 self.db.add_history(trk04, CURRENT-datetime.timedelta(minutes=3))
157 self.db.add_history(trk03, CURRENT-datetime.timedelta(minutes=2))
158 self.db.add_history(trk05, CURRENT-datetime.timedelta(minutes=1))
159 art_history = self.db.fetch_artists_history()
160 # Now we should have 4 artists in history
161 self.assertEqual(len(art_history), 4)
162 # Controling order, recent first
163 self.assertEqual([trk05.artist, trk03.artist,
164 trk04.artist, trk03.artist],
167 def test_04_triggers(self):
168 self.db.purge_history(duration=0)
171 track = Track(file='/baz/bar.baz', name='baz', artist='fooart',
172 albumartist='not-same', album='not-same',)
173 self.db.get_track(track)
174 # Set 6 more records from same artist but not same album
175 for i in range(1, 6): # starts at 1 to ensure records are in the past
176 trk = Track(file=f'/foo/{i}', name=f'{i}', artist='fooart',
177 albumartist='fooalbart', album='foolbum',)
178 # Add track, save its DB id
179 tracks_ids.append(self.db.get_track(trk))
180 # set records in the past to ease purging then
181 last = CURRENT - datetime.timedelta(minutes=i)
182 self.db.add_history(trk, date=last) # Add to history
183 conn = self.db.get_database_connection()
184 # for tid in tracks_ids:
185 for tid in tracks_ids[:-1]:
186 # Delete lastest record
187 conn.execute('DELETE FROM history WHERE history.track = ?',
189 c = conn.execute('SELECT albums.name FROM albums;')
190 # There are still albums records (still a history using it)
191 self.assertIn((trk.album,), c.fetchall())
192 # purging last entry in history for album == trk.album
193 conn.execute('DELETE FROM history WHERE history.track = ?',
195 # triggers purge other tables if possible
196 conn.execute('SELECT albums.name FROM albums;')
197 albums = c.fetchall()
198 # No more "foolbum" in the table albums
199 self.assertNotIn(('foolbum',), albums)
200 # There is still "fooart" though
201 c = conn.execute('SELECT artists.name FROM artists;')
202 artists = c.fetchall()
203 # No more "foolbum" in the table albums
204 self.assertIn(('fooart',), artists)
208 class Test_01BlockList(Main):
210 def test_blocklist_addition(self):
212 # Set 6 records, same album
213 for i in range(1, 6): # starts at 1 to ensure records are in the past
214 trk = Track(file=f'/foo/{i}', name=f'{i}', artist='fooart',
215 albumartist='fooalbart', album='foolbum',)
216 # Add track, save its DB id
217 tracks_ids.append(self.db.get_track(trk))
218 # set records in the past to ease purging then
219 last = CURRENT - datetime.timedelta(minutes=i)
220 self.db.add_history(trk, date=last) # Add to history
222 self.db.get_bl_track(trk)
224 self.db.get_bl_track(trk)
225 self.db.get_bl_album(Album(name=trk.album))
227 self.db.get_bl_artist(trk.Artist)
229 def test_blocklist_triggers(self):
230 trk01 = Track(file='01', name='01', artist='artist A', album='album A')
231 trk02 = Track(file='02', name='01', artist='artist A', album='album B')
232 trk01_id = self.db.get_bl_track(trk01)
233 trk02_id = self.db.get_bl_track(trk02)
234 self.db.add_history(trk01, IN_THE_PAST)
235 self.db._remove_blocklist_id(trk01_id)
237 # albums/artists table not affected since trk01_id still in history
238 conn = self.db.get_database_connection()
239 albums = conn.execute('SELECT albums.name FROM albums;').fetchall()
240 artists = conn.execute('SELECT artists.name FROM artists;').fetchall()
241 self.assertIn(('album A',), albums)
242 self.assertIn(('artist A',), artists)
243 self.db.purge_history(0)
244 # remove last reference to trk01
245 albums = conn.execute('SELECT albums.name FROM albums;').fetchall()
246 self.assertNotIn(('album A',), albums)
247 self.assertIn(('artist A',), artists)
249 self.db._remove_blocklist_id(trk02_id)
250 albums = conn.execute('SELECT albums.name FROM albums;').fetchall()
251 artists = conn.execute('SELECT artists.name FROM artists;').fetchall()
252 self.assertNotIn(('album B',), albums)
253 self.assertNotIn(('artist A'), artists)
258 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8