X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=tests%2Ftest_meta.py;h=ed241cf1a04f85f9c2ee4db53d1ad148a1f3ef1d;hb=f2d54b31a8da9852fc532cc90b29623eabe27b9d;hp=031999fe38d2e6b1cf93f9384776f40378150138;hpb=62e74e0a4a5d5b992fdbfb2613d91ce683fcd968;p=mpd-sima.git diff --git a/tests/test_meta.py b/tests/test_meta.py index 031999f..ed241cf 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -2,27 +2,25 @@ import unittest -from sima.lib.meta import Meta, Artist, is_uuid4 -from sima.lib.meta import WrongUUID4, MetaException +from sima.lib.meta import Meta, Artist, Album, MetaContainer, is_uuid4 +from sima.lib.meta import MetaException, SEPARATOR -VALID = '110E8100-E29B-41D1-A716-116655250000' +VALID = '110e8100-e29b-41d1-a716-116655250000' class TestMetaObject(unittest.TestCase): def test_uuid_integrity(self): - Meta(mbid=VALID, name='test') - Meta(mbid=VALID.lower(), name='test') wrong = VALID +'a' - self.assertRaises(WrongUUID4, is_uuid4, wrong) + self.assertFalse(is_uuid4(wrong)) # test UUID4 format validation - self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('4', '3')) - self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('A', 'Z')) + self.assertFalse(is_uuid4(VALID.replace('4', '3'))) + self.assertFalse(is_uuid4(VALID.replace('a', 'z'))) def test_init(self): for args in [ {'mbid':VALID}, {'name': None}, - {}, + {'name': 42}, ]: with self.assertRaises(MetaException, msg='{} does not raise an except.'.format(args)): @@ -31,23 +29,20 @@ class TestMetaObject(unittest.TestCase): def test_equality(self): a = Meta(mbid=VALID, name='a') b = Meta(mbid=VALID, name='b') + c = Meta(mbid=VALID.upper(), name='c') self.assertEqual(a, b) + self.assertEqual(a, c) def test_hash(self): a = Meta(mbid=VALID, name='a') b = Meta(mbid=VALID, name='b') c = Meta(mbid=VALID, name='c') - self.assertTrue(len({a,b,c}) == 1) + self.assertTrue(len({a, b, c}) == 1) self.assertTrue(a in [c, b]) self.assertTrue(a in {c, b}) # mbid is immutable self.assertRaises(AttributeError, a.__setattr__, 'mbid', VALID) - def test_identity(self): - a = Meta(mbid=VALID, name='a') - b = Meta(mbid=VALID, name='a') - self.assertTrue(a is not b) - def test_aliases(self): art0 = Meta(name='Silver Mt. Zion') art0.add_alias('A Silver Mt. Zion') @@ -67,6 +62,8 @@ class TestMetaObject(unittest.TestCase): mbid='f22942a1-6f70-4f48-866e-238cb2308fbd') art02 = Meta(name='Some Other Name not even close, avoid fuzzy match', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd') + art03 = Meta(name='Aphex Twin', + mbid='322942a1-6f70-4f48-866e-238cb2308fbd') self.assertTrue(len({art00, art02}) == 1) art00._Meta__name = art02._Meta__name = 'Aphex Twin' @@ -84,6 +81,9 @@ class TestMetaObject(unittest.TestCase): self.assertTrue(len({art00, art02}) == 1, 'wrong: hash({!r}) != hash({!r})'.format(art00, art02)) + self.assertTrue(hash(art00) != hash(art03), + 'wrong: hash({!r}) == hash({!r})'.format(art00, art03)) + def test_comparison(self): art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd') @@ -102,11 +102,27 @@ class TestMetaObject(unittest.TestCase): art10._Meta__mbid = None self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01)) + def test_mpd_serialization(self): + """Controls serialization of names""" + name = "Heaven's Door" + heavens_door = Meta(name=name) + target = r"Heaven\'s Door" + self.assertEqual(heavens_door.name_sz, target) + self.assertEqual(heavens_door.name, name) + self.assertEqual(heavens_door.names_sz, {target}) + heavens_door.add_alias(name+" LP") + self.assertEqual(heavens_door.aliases_sz, {target+" LP"}) + # Controls inheritance + heavens_door = Album(name=name) + self.assertEqual(heavens_door.name_sz, target) + heavens_door = Artist(name=name) + self.assertEqual(heavens_door.name_sz, target) + class TestArtistObject(unittest.TestCase): def test_init(self): - artist = {'artist': ['Name featuring', 'Feature'], + artist = {'artist': SEPARATOR.join(['Original Name', 'Featuring Nane', 'Feature…']), 'albumartist': 'Name', 'musicbrainz_artistid': VALID, 'musicbrainz_albumartistid': VALID.replace('11', '22'), @@ -119,5 +135,45 @@ class TestArtistObject(unittest.TestCase): self.assertTrue(art.mbid == VALID) artist.pop('albumartist') art = Artist(**artist) + self.assertTrue(art.name == 'Original Name', art.name) + + def test_empty_name(self): + for args in [ + {'mbid':VALID}, + {'name': None}, + {}, + ]: + with self.assertRaises(MetaException, + msg='{} does not raise an except.'.format(args)): + Artist(**args) + +class TestMetaContainers(unittest.TestCase): + + def test_init(self): + a = Meta(mbid=VALID, name='a') + b = Meta(mbid=VALID, name='b') + c = Meta(mbid=VALID.replace('11', '22'), name='b') + # redondant with Meta test_comparison, but anyway + cont = MetaContainer([a, b, c]) + self.assertTrue(len(cont) == 2) + self.assertTrue(a in cont) + self.assertTrue(b in cont) + self.assertTrue(Meta(name='a') in cont) + + def test_intersection_difference(self): + # Now set works as expected with composite (name/mbid) collections of Meta + # cf Meta test_union + # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=)]) == 1 + # but + # >>> len({Artist(name='Name'), Artist(name='Name', mbid=}) == 2 + art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd') + art01 = Meta(name='Aphex Twin', mbid=None) + self.assertTrue(MetaContainer([art00]) & MetaContainer([art01])) + self.assertFalse(MetaContainer([art01]) - MetaContainer([art01])) + art01._Meta__mbid = art00.mbid + self.assertTrue(MetaContainer([art00]) & MetaContainer([art01])) + self.assertFalse(MetaContainer([art01]) - MetaContainer([art01])) + art01._Meta__mbid = art00.mbid.replace('229', '330') + self.assertFalse(MetaContainer([art00]) & MetaContainer([art01])) # vim: ai ts=4 sw=4 sts=4 expandtab