mirror of
https://github.com/beetbox/beets.git
synced 2026-01-30 03:54:21 +01:00
Add tests for computed attributes.
This commit is contained in:
parent
936acf0236
commit
69213ad574
2 changed files with 68 additions and 27 deletions
|
|
@ -106,7 +106,7 @@ def album(lib=None):
|
|||
mb_albumartistid = 'someID-1'
|
||||
)
|
||||
if lib:
|
||||
lib.add_album(i)
|
||||
lib.add(i)
|
||||
return i
|
||||
|
||||
# Dummy import session.
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ class DummyDataTestCase(_common.TestCase):
|
|||
for album in albums:
|
||||
self.lib.add(album)
|
||||
|
||||
items = [_common.item() for _ in range(3)]
|
||||
items = [_common.item() for _ in range(4)]
|
||||
items[0].title = 'foo bar'
|
||||
items[0].artist = 'one'
|
||||
items[0].album = 'baz'
|
||||
|
|
@ -72,29 +72,17 @@ class DummyDataTestCase(_common.TestCase):
|
|||
items[2].flex1 = "flex1-2"
|
||||
items[2].flex2 = "flex1-B"
|
||||
items[2].album_id = albums[1].id
|
||||
items[3].title = 'beets 4 eva'
|
||||
items[3].artist = 'three'
|
||||
items[3].album = 'foo2'
|
||||
items[3].year = 2004
|
||||
items[3].comp = False
|
||||
items[3].flex1 = "flex1-2"
|
||||
items[3].flex2 = "flex1-C"
|
||||
items[3].album_id = albums[2].id
|
||||
for item in items:
|
||||
self.lib.add(item)
|
||||
|
||||
albums = [_common.album() for _ in range(3)]
|
||||
albums[0].album = "album A"
|
||||
albums[0].genre = "Jazz"
|
||||
albums[0].year = "2001"
|
||||
albums[0].flex1 = "flex1-1"
|
||||
albums[0].flex2 = "flex2-A"
|
||||
albums[1].album = "album B"
|
||||
albums[1].genre = "Jazz"
|
||||
albums[1].year = "2001"
|
||||
albums[1].flex1 = "flex1-2"
|
||||
albums[1].flex2 = "flex2-A"
|
||||
albums[2].album = "album C"
|
||||
albums[2].genre = "Rock"
|
||||
albums[2].year = "2005"
|
||||
albums[2].flex1 = "flex1-1"
|
||||
albums[2].flex2 = "flex2-B"
|
||||
|
||||
for album in albums:
|
||||
self.lib.add(album)
|
||||
|
||||
|
||||
class SortFixedFieldTest(DummyDataTestCase):
|
||||
def test_sort_asc(self):
|
||||
|
|
@ -114,7 +102,7 @@ class SortFixedFieldTest(DummyDataTestCase):
|
|||
sort = dbcore.query.FixedFieldSort("year", False)
|
||||
results = self.lib.items(q, sort)
|
||||
self.assertGreaterEqual(results[0]['year'], results[1]['year'])
|
||||
self.assertEqual(results[0]['year'],2003)
|
||||
self.assertEqual(results[0]['year'],2004)
|
||||
# same thing with query string
|
||||
q = 'year-'
|
||||
results2 = self.lib.items(q)
|
||||
|
|
@ -158,7 +146,9 @@ class SortFlexFieldTest(DummyDataTestCase):
|
|||
q = ''
|
||||
sort = dbcore.query.FlexFieldSort(beets.library.Item, "flex1", False)
|
||||
results = self.lib.items(q, sort)
|
||||
self.assertTrue(results[0]['flex1']>results[1]['flex1'])
|
||||
self.assertGreaterEqual(results[0]['flex1'], results[1]['flex1'])
|
||||
self.assertGreaterEqual(results[1]['flex1'], results[2]['flex1'])
|
||||
self.assertGreaterEqual(results[2]['flex1'], results[3]['flex1'])
|
||||
self.assertEqual(results[0]['flex1'],'flex1-2')
|
||||
# same thing with query string
|
||||
q = 'flex1-'
|
||||
|
|
@ -221,9 +211,9 @@ class SortAlbumFixedFieldTest(DummyDataTestCase):
|
|||
results = self.lib.albums(q, sort)
|
||||
self.assertLessEqual(results[0]['genre'], results[1]['genre'])
|
||||
self.assertLessEqual(results[1]['genre'], results[2]['genre'])
|
||||
self.assertEqual(results[0]['genre'],'Jazz')
|
||||
self.assertEqual(results[1]['genre'],'Jazz')
|
||||
self.assertLessEqual(results[0]['album'], results[1]['album'])
|
||||
self.assertEqual(results[1]['genre'],'Rock')
|
||||
self.assertEqual(results[2]['genre'],'Rock')
|
||||
self.assertLessEqual(results[1]['album'], results[2]['album'])
|
||||
#same thing with query string
|
||||
q = 'genre+ album+'
|
||||
results2 = self.lib.albums(q)
|
||||
|
|
@ -276,6 +266,57 @@ class SortAlbumFlexdFieldTest(DummyDataTestCase):
|
|||
self.assertEqual(r1.id, r2.id)
|
||||
|
||||
|
||||
class SortAlbumComputedFieldTest(DummyDataTestCase):
|
||||
def test_sort_asc(self):
|
||||
q = ''
|
||||
sort = dbcore.query.ComputedFieldSort(beets.library.Album, "path", True)
|
||||
results = self.lib.albums(q, sort)
|
||||
self.assertLessEqual(results[0]['path'], results[1]['path'])
|
||||
self.assertLessEqual(results[1]['path'], results[2]['path'])
|
||||
# same thing with query string
|
||||
q = 'path+'
|
||||
results2 = self.lib.albums(q)
|
||||
for r1, r2 in zip(results, results2) :
|
||||
self.assertEqual(r1.id, r2.id)
|
||||
|
||||
def test_sort_asc(self):
|
||||
q = ''
|
||||
sort = dbcore.query.ComputedFieldSort(beets.library.Album, "path", False)
|
||||
results = self.lib.albums(q, sort)
|
||||
self.assertGreaterEqual(results[0]['path'], results[1]['path'])
|
||||
self.assertGreaterEqual(results[1]['path'], results[2]['path'])
|
||||
# same thing with query string
|
||||
q = 'path-'
|
||||
results2 = self.lib.albums(q)
|
||||
for r1, r2 in zip(results, results2) :
|
||||
self.assertEqual(r1.id, r2.id)
|
||||
|
||||
|
||||
class SortCombinedFieldTest(DummyDataTestCase):
|
||||
def test_computed_first(self):
|
||||
q=''
|
||||
s1 = dbcore.query.ComputedFieldSort(beets.library.Album, "path", True)
|
||||
s2 = dbcore.query.FixedFieldSort("year", True)
|
||||
sort = dbcore.query.MultipleSort()
|
||||
sort.add_criteria(s1)
|
||||
sort.add_criteria(s2)
|
||||
results = self.lib.albums(q, sort)
|
||||
self.assertLessEqual(results[0]['path'], results[1]['path'])
|
||||
self.assertLessEqual(results[1]['path'], results[2]['path'])
|
||||
|
||||
def test_computed_second(self):
|
||||
q=''
|
||||
s1 = dbcore.query.FixedFieldSort("year", True)
|
||||
s2 = dbcore.query.ComputedFieldSort(beets.library.Album, "path", True)
|
||||
sort = dbcore.query.MultipleSort()
|
||||
sort.add_criteria(s1)
|
||||
sort.add_criteria(s2)
|
||||
results = self.lib.albums(q, sort)
|
||||
self.assertLessEqual(results[0]['year'], results[1]['year'])
|
||||
self.assertLessEqual(results[1]['year'], results[2]['year'])
|
||||
self.assertLessEqual(results[0]['path'], results[1]['path'])
|
||||
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue