1 # -*- coding: utf-8 -*-
5 from sima.lib.meta import Meta, Artist, MetaContainer, 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 c = Meta(mbid=VALID.lower(), name='c')
35 self.assertEqual(a, b)
36 self.assertEqual(a, c)
39 a = Meta(mbid=VALID, name='a')
40 b = Meta(mbid=VALID, name='b')
41 c = Meta(mbid=VALID, name='c')
42 self.assertTrue(len({a, b, c}) == 1)
43 self.assertTrue(a in [c, b])
44 self.assertTrue(a in {c, b})
46 self.assertRaises(AttributeError, a.__setattr__, 'mbid', VALID)
48 def test_aliases(self):
49 art0 = Meta(name='Silver Mt. Zion')
50 art0.add_alias('A Silver Mt. Zion')
53 # Controls 'Silver Mt. Zion' is not in aliases
54 self.assertTrue('Silver Mt. Zion' not in art0.aliases)
56 # test equality str with Obj.__aliases
57 self.assertTrue(art0 == 'A Silver Mt. Zion')
58 self.assertTrue('A Silver Mt. Zion' == art0)
59 # test equality Obj.__name with OgjBis.__aliases
60 self.assertTrue(art0 == Meta(name='A Silver Mt. Zion'))
63 art00 = Meta(name='Aphex Twin',
64 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
65 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
66 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
67 art03 = Meta(name='Aphex Twin',
68 mbid='322942a1-6f70-4f48-866e-238cb2308fbd')
70 self.assertTrue(len({art00, art02}) == 1)
71 art00._Meta__name = art02._Meta__name = 'Aphex Twin'
72 self.assertTrue(len({art00, art02}) == 1)
74 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
75 art00._Meta__mbid = None
76 self.assertTrue(len({art00, art02}) == 2,
77 'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
78 # equivalent: self.assertTrue(hash(art00) != hash(art02))
80 # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
81 art00._Meta__mbid = art02._Meta__mbid = None
82 # equivalent: self.assertTrue(hash(art00) == hash(art02))
83 self.assertTrue(len({art00, art02}) == 1,
84 'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
86 self.assertTrue(hash(art00) != hash(art03),
87 'wrong: hash({!r}) == hash({!r})'.format(art00, art03))
89 def test_comparison(self):
90 art00 = Meta(name='Aphex Twin',
91 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
92 art01 = Meta(name='Aphex Twin',)
93 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
94 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
95 art10 = Meta(name='Aphex Twin',
96 mbid='d22942a1-6f70-4f48-866e-238cb2308fbd')
97 # testing name/mbid == name/None
98 self.assertTrue(art00 == art01, 'wrong: %r != %r' % (art00, art01))
99 # testing name/mbid == other_name/mbid
100 self.assertTrue(art00 == art02, 'wrong: %r != %r' % (art00, art02))
101 # testing name/mbid != name/other_mbid
102 self.assertTrue(art00 != art10, 'wrong: %r == %r' % (art00, art10))
103 # Testing name/None == name/None
104 art10._Meta__mbid = None
105 self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01))
108 class TestArtistObject(unittest.TestCase):
111 artist = {'artist': ', '.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
112 'albumartist': 'Name',
113 'musicbrainz_artistid': VALID,
114 'musicbrainz_albumartistid': VALID.replace('11', '22'),
116 art = Artist(**artist)
117 self.assertTrue(art.name == 'Name')
118 self.assertTrue(art.mbid == VALID.replace('11', '22'))
119 artist.pop('musicbrainz_albumartistid')
120 art = Artist(**artist)
121 self.assertTrue(art.mbid == VALID)
122 artist.pop('albumartist')
123 art = Artist(**artist)
124 self.assertTrue(art.name == 'Original Name', art.name)
127 class TestMetaContainers(unittest.TestCase):
130 a = Meta(mbid=VALID, name='a')
131 b = Meta(mbid=VALID, name='b')
132 c = Meta(mbid=VALID.replace('11', '22'), name='b')
133 # redondant with Meta test_comparison, but anyway
134 cont = MetaContainer([a, b, c])
135 self.assertTrue(len(cont) == 2)
136 self.assertTrue(a in cont)
137 self.assertTrue(b in cont)
138 self.assertTrue(Meta(name='a') in cont)
140 def test_intersection_difference(self):
141 # Now set works as expected with composite (name/mbid) collections of Meta
143 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
145 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
146 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
147 art01 = Meta(name='Aphex Twin', mbid=None)
148 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
149 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
150 art01._Meta__mbid = art00.mbid
151 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
152 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
153 art01._Meta__mbid = art00.mbid.replace('229', '330')
154 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
156 # vim: ai ts=4 sw=4 sts=4 expandtab