7 from sima.lib.db import SimaDB
8 from sima.lib.track import Track
13 'albumartist': 'Devolt',
14 'albumartistsort': 'Devolt',
18 'file': 'music/Devolt/2011-Grey/03-Devolt - Crazy.mp3',
19 'last-modified': '2012-04-02T20:48:59Z',
20 'musicbrainz_albumartistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99',
21 'musicbrainz_albumid': 'ea2ef2cf-59e1-443a-817e-9066e3e0be4b',
22 'musicbrainz_artistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99',
23 'musicbrainz_trackid': 'fabf8fc9-2ae5-49c9-8214-a839c958d872',
25 'duration': '220.000',
30 class Main_TestDB(unittest.TestCase):
31 db_file = 'file::memory:?cache=shared'
32 #db_file = '/dev/shm/unittest.sqlite'
36 self.db = SimaDB(db_path=self.db_file)
37 # Maintain a connection to keep the database between test cases
38 self.conn = self.db.get_database_connection()
41 def tearDownClass(self):
45 class TestDB(Main_TestDB):
47 def test_00_recreation(self):
50 def test_01_add_track(self):
52 trk_id = self.db.get_track(trk)
53 self.assertEqual(trk_id, self.db.get_track(trk),
54 'Same track, same record')
56 def test_02_history(self):
57 curr = datetime.datetime.utcnow()
58 # set records in the past to ease purging then
59 last = curr - datetime.timedelta(hours=1)
61 self.db.add_history(trk, date=last)
62 self.db.add_history(trk, date=last)
63 hist = self.db.get_history()
64 self.assertEqual(len(hist), 1, 'same track results in a single record')
66 trk_foo = Track(file="/foo/bar/baz.flac")
67 self.db.add_history(trk_foo, date=last)
68 hist = self.db.get_history()
69 self.assertEqual(len(hist), 2)
71 self.db.add_history(trk, date=last)
72 hist = self.db.get_history()
73 self.assertEqual(len(hist), 2)
74 self.db.purge_history(duration=0)
75 hist = self.db.get_history()
76 self.assertEqual(len(hist), 0)
78 # Controls we got history in the right order
79 # recent first, oldest last
81 for i in range(1, 5): # starts at 1 to ensure records are in the past
82 trk = Track(file=f'/foo/bar.{i}', name='{i}-baz', album='foolbum')
84 last = curr - datetime.timedelta(minutes=i)
85 self.db.add_history(trk, date=last)
86 hist_records = self.db.get_history()
87 self.assertEqual(hist, hist_records)
88 self.db.purge_history(duration=0)
90 def test_04_triggers(self):
91 self.db.purge_history(duration=0)
92 curr = datetime.datetime.utcnow()
94 # Set 4 records, same album
95 for i in range(1, 6): # starts at 1 to ensure records are in the past
96 trk = Track(file=f'/foo/{i}', name=f'{i}', artist='fooart',
97 albumartist='fooalbart', album='foolbum',)
98 tracks_ids.append(self.db.get_track(trk)) # Add track, save its DB id
99 # set records in the past to ease purging then
100 last = curr - datetime.timedelta(minutes=i)
101 self.db.add_history(trk, date=last) # Add to history
102 conn = self.db.get_database_connection()
103 # Add another track not related (not same album)
104 track = Track(file='/baz/bar.baz', name='baz', artist='fooart',
105 albumartist='not-same', album='not-same',)
106 self.db.get_track(track)
107 # for tid in tracks_ids:
108 for tid in tracks_ids[:-1]:
109 # Delete lastest record
110 conn.execute('DELETE FROM history WHERE history.track = ?', (tid,))
111 c = conn.execute('SELECT albums.name FROM albums;')
112 # There are still albums records (still a history using it)
113 self.assertIn((trk.album,), c.fetchall())
114 # purging last entry in history or album == trk.album
115 c.execute('DELETE FROM history WHERE history.track = ?',
117 # triggers purge other tables if possible
118 c.execute('SELECT albums.name FROM albums;')
119 albums = c.fetchall()
120 self.assertNotIn(('foolbum',), albums)
125 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8