]> kaliko git repositories - mpd-sima.git/blob - tests/test_db.py
2419d44a97784a6bad49e12bb5ce1278bef460a0
[mpd-sima.git] / tests / test_db.py
1 # coding: utf-8
2
3 import unittest
4 import os
5 import datetime
6
7 from sima.lib.db import SimaDB
8 from sima.lib.track import Track
9
10
11 DEVOLT = {
12   'album': 'Grey',
13   'albumartist': 'Devolt',
14   'albumartistsort': 'Devolt',
15   'artist': 'Devolt',
16   'date': '2011-12-01',
17   'disc': '1/1',
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',
24   'time': '220',
25   'duration': '220.000',
26   'title': 'Crazy',
27   'track': '3/6'}
28
29
30 class Main_TestDB(unittest.TestCase):
31     db_file = 'file::memory:?cache=shared'
32     #db_file = '/dev/shm/unittest.sqlite'
33
34     @classmethod
35     def setUpClass(self):
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()
39
40     @classmethod
41     def tearDownClass(self):
42         self.conn.close()
43
44
45 class TestDB(Main_TestDB):
46
47     def test_00_recreation(self):
48         self.db.create_db()
49
50     def test_01_add_track(self):
51         trk = Track(**DEVOLT)
52         trk_id = self.db.get_track(trk)
53         self.assertEqual(trk_id, self.db.get_track(trk),
54                          'Same track, same record')
55
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)
60         trk = Track(**DEVOLT)
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')
65
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)
70
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)
77
78         # Controls we got history in the right order
79         # recent first, oldest last
80         hist = list()
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')
83             hist.append(trk)
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)
89
90     def test_04_triggers(self):
91         self.db.purge_history(duration=0)
92         curr = datetime.datetime.utcnow()
93         tracks_ids = list()
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 = ?',
116                   (tracks_ids[-1],))
117         # triggers purge other tables if possible
118         c.execute('SELECT albums.name FROM albums;')
119         albums = c.fetchall()
120         self.assertNotIn(('foolbum',), albums)
121         conn.close()
122
123
124 # VIM MODLINE
125 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8