1 # -*- coding: utf-8 -*-
5 from sima.lib.meta import Meta, Artist, Album, MetaContainer, is_uuid4
6 from sima.lib.meta import MetaException, SEPARATOR
8 VALID = '110e8100-e29b-41d1-a716-116655250000'
10 class TestMetaObject(unittest.TestCase):
12 def test_uuid_integrity(self):
14 self.assertFalse(is_uuid4(wrong))
15 # test UUID4 format validation
16 self.assertFalse(is_uuid4(VALID.replace('a', 'z')))
24 with self.assertRaises(MetaException,
25 msg='{} does not raise an except.'.format(args)):
28 def test_equality(self):
29 a = Meta(mbid=VALID, name='a')
30 b = Meta(mbid=VALID, name='b')
31 c = Meta(mbid=VALID.upper(), name='c')
32 self.assertEqual(a, b)
33 self.assertEqual(a, c)
36 a = Meta(mbid=VALID, name='a')
37 b = Meta(mbid=VALID, name='b')
38 c = Meta(mbid=VALID, name='c')
39 self.assertTrue(len({a, b, c}) == 1)
40 self.assertTrue(a in [c, b])
41 self.assertTrue(a in {c, b})
43 self.assertRaises(AttributeError, a.__setattr__, 'mbid', VALID)
45 def test_aliases(self):
46 art0 = Meta(name='Silver Mt. Zion')
47 art0.add_alias('A Silver Mt. Zion')
50 # Controls 'Silver Mt. Zion' is not in aliases
51 self.assertTrue('Silver Mt. Zion' not in art0.aliases)
53 # test equality str with Obj.__aliases
54 self.assertTrue(art0 == 'A Silver Mt. Zion')
55 self.assertTrue('A Silver Mt. Zion' == art0)
56 # test equality Obj.__name with OgjBis.__aliases
57 self.assertTrue(art0 == Meta(name='A Silver Mt. Zion'))
59 art1 = Meta(name='Silver Mt. Zion')
61 self.assertIn('A Silver Mt. Zion', art1.aliases)
63 art3 = Meta(name='foo')
64 art3.add_alias('Silver Mt. Zion')
66 self.assertNotIn('Silver Mt. Zion', art1.aliases)
69 art00 = Meta(name='Aphex Twin',
70 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
71 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
72 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
73 art03 = Meta(name='Aphex Twin',
74 mbid='322942a1-6f70-4f48-866e-238cb2308fbd')
76 self.assertTrue(len({art00, art02}) == 1)
77 art00._Meta__name = art02._Meta__name = 'Aphex Twin'
78 self.assertTrue(len({art00, art02}) == 1)
80 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
81 art00._Meta__mbid = None
82 self.assertTrue(len({art00, art02}) == 2,
83 'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
84 # equivalent: self.assertTrue(hash(art00) != hash(art02))
86 # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
87 art00._Meta__mbid = art02._Meta__mbid = None
88 # equivalent: self.assertTrue(hash(art00) == hash(art02))
89 self.assertTrue(len({art00, art02}) == 1,
90 'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
92 self.assertTrue(hash(art00) != hash(art03),
93 'wrong: hash({!r}) == hash({!r})'.format(art00, art03))
95 def test_comparison(self):
96 art00 = Meta(name='Aphex Twin',
97 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
98 art01 = Meta(name='Aphex Twin',)
99 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
100 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
101 art10 = Meta(name='Aphex Twin',
102 mbid='d22942a1-6f70-4f48-866e-238cb2308fbd')
103 # testing name/mbid == name/None
104 self.assertTrue(art00 == art01, 'wrong: %r != %r' % (art00, art01))
105 # testing name/mbid == other_name/mbid
106 self.assertTrue(art00 == art02, 'wrong: %r != %r' % (art00, art02))
107 # testing name/mbid != name/other_mbid
108 self.assertTrue(art00 != art10, 'wrong: %r == %r' % (art00, art10))
109 # Testing name/None == name/None
110 art10._Meta__mbid = None
111 self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01))
113 def test_mpd_serialization(self):
114 """Controls serialization of names"""
115 name = "Heaven's Door"
116 heavens_door = Meta(name=name)
117 target = r"Heaven\'s Door"
118 self.assertEqual(heavens_door.name_sz, target)
119 self.assertEqual(heavens_door.name, name)
120 self.assertEqual(heavens_door.names_sz, {target})
121 heavens_door.add_alias(name+" LP")
122 self.assertEqual(heavens_door.aliases_sz, {target+" LP"})
123 # Controls inheritance
124 heavens_door = Album(name=name)
125 self.assertEqual(heavens_door.name_sz, target)
126 heavens_door = Artist(name=name)
127 self.assertEqual(heavens_door.name_sz, target)
128 # Same with double quote
129 name = 'Bonnie "Prince" Billy'
130 bonnie = Meta(name=name)
131 target = r"Bonnie \"Prince\" Billy"
132 self.assertEqual(bonnie.name_sz, target)
133 self.assertEqual(bonnie.name, name)
137 class TestArtistObject(unittest.TestCase):
141 SEPARATOR.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
142 'albumartist': 'Name',
143 'musicbrainz_artistid':
144 SEPARATOR.join([VALID, VALID.replace('0000', '1312')]),
146 art = Artist(**artist)
147 self.assertEqual(art.name, 'Original Name')
148 self.assertEqual(art.mbid, VALID)
149 self.assertEqual(art.albumartist, artist['albumartist'])
150 artist.pop('albumartist')
151 art = Artist(**artist)
152 self.assertEqual(art.name, 'Original Name')
153 self.assertEqual(art.albumartist, 'Original Name')
155 def test_empty_name(self):
161 with self.assertRaises(MetaException,
162 msg='{} does not raise an except.'.format(args)):
165 class TestMetaContainers(unittest.TestCase):
168 a = Meta(mbid=VALID, name='a')
169 b = Meta(mbid=VALID, name='b')
170 c = Meta(mbid=VALID.replace('11', '22'), name='b')
171 # redondant with Meta test_comparison, but anyway
172 cont = MetaContainer([a, b, c])
173 self.assertTrue(len(cont) == 2)
174 self.assertTrue(a in cont)
175 self.assertTrue(b in cont)
176 self.assertTrue(Meta(name='a') in cont)
178 def test_intersection_difference(self):
179 # Now set works as expected with composite (name/mbid) collections of Meta
181 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
183 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
184 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
185 art01 = Meta(name='Aphex Twin', mbid=None)
186 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
187 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
188 art01._Meta__mbid = art00.mbid
189 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
190 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
191 art01._Meta__mbid = art00.mbid.replace('229', '330')
192 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
194 # vim: ai ts=4 sw=4 sts=4 expandtab