1 # -*- coding: utf-8 -*-
5 from sima.lib.meta import Meta, Artist, is_uuid4
6 from sima.lib.meta import WrongUUID4, MetaException
8 VALID = '110E8100-E29B-41D1-A716-116655250000'
10 class TestMetaObject(unittest.TestCase):
12 def test_uuid_integrity(self):
13 Meta(mbid=VALID, name='test')
14 Meta(mbid=VALID.lower(), name='test')
16 self.assertRaises(WrongUUID4, is_uuid4, wrong)
17 # test UUID4 format validation
18 self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('4', '3'))
19 self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('A', 'Z'))
27 with self.assertRaises(MetaException,
28 msg='{} does not raise an except.'.format(args)):
31 def test_equality(self):
32 a = Meta(mbid=VALID, name='a')
33 b = Meta(mbid=VALID, name='b')
34 self.assertEqual(a, b)
37 a = Meta(mbid=VALID, name='a')
38 b = Meta(mbid=VALID, name='b')
39 c = Meta(mbid=VALID, name='c')
40 self.assertTrue(len({a,b,c}) == 1)
41 self.assertTrue(a in [c, b])
42 self.assertTrue(a in {c, b})
44 self.assertRaises(AttributeError, a.__setattr__, 'mbid', VALID)
46 def test_identity(self):
47 a = Meta(mbid=VALID, name='a')
48 b = Meta(mbid=VALID, name='a')
49 self.assertTrue(a is not b)
51 def test_aliases(self):
52 art0 = Meta(name='Silver Mt. Zion')
53 art0.add_alias('A Silver Mt. Zion')
56 # Controls 'Silver Mt. Zion' is not in aliases
57 self.assertTrue('Silver Mt. Zion' not in art0.aliases)
59 # test equality str with Obj.__aliases
60 self.assertTrue(art0 == 'A Silver Mt. Zion')
61 self.assertTrue('A Silver Mt. Zion' == art0)
62 # test equality Obj.__name with OgjBis.__aliases
63 self.assertTrue(art0 == Meta(name='A Silver Mt. Zion'))
66 art00 = Meta(name='Aphex Twin',
67 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
68 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
69 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
71 self.assertTrue(len({art00, art02}) == 1)
72 art00._Meta__name = art02._Meta__name = 'Aphex Twin'
73 self.assertTrue(len({art00, art02}) == 1)
75 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
76 art00._Meta__mbid = None
77 self.assertTrue(len({art00, art02}) == 2,
78 'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
79 # equivalent: self.assertTrue(hash(art00) != hash(art02))
81 # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
82 art00._Meta__mbid = art02._Meta__mbid = None
83 # equivalent: self.assertTrue(hash(art00) == hash(art02))
84 self.assertTrue(len({art00, art02}) == 1,
85 'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
87 def test_comparison(self):
88 art00 = Meta(name='Aphex Twin',
89 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
90 art01 = Meta(name='Aphex Twin',)
91 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
92 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
93 art10 = Meta(name='Aphex Twin',
94 mbid='d22942a1-6f70-4f48-866e-238cb2308fbd')
95 # testing name/mbid == name/None
96 self.assertTrue(art00 == art01, 'wrong: %r != %r' % (art00, art01))
97 # testing name/mbid == other_name/mbid
98 self.assertTrue(art00 == art02, 'wrong: %r != %r' % (art00, art02))
99 # testing name/mbid != name/other_mbid
100 self.assertTrue(art00 != art10, 'wrong: %r == %r' % (art00, art10))
101 # Testing name/None == name/None
102 art10._Meta__mbid = None
103 self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01))
106 class TestArtistObject(unittest.TestCase):
109 artist = {'artist': ['Name featuring', 'Feature'],
110 'albumartist': 'Name',
111 'musicbrainz_artistid': VALID,
112 'musicbrainz_albumartistid': VALID.replace('11', '22'),
114 art = Artist(**artist)
115 self.assertTrue(art.name == 'Name')
116 self.assertTrue(art.mbid == VALID.replace('11', '22'))
117 artist.pop('musicbrainz_albumartistid')
118 art = Artist(**artist)
119 self.assertTrue(art.mbid == VALID)
120 artist.pop('albumartist')
121 art = Artist(**artist)
123 # vim: ai ts=4 sw=4 sts=4 expandtab