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)
130 class TestArtistObject(unittest.TestCase):
134 SEPARATOR.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
135 'albumartist': 'Name',
136 'musicbrainz_artistid':
137 SEPARATOR.join([VALID, VALID.replace('0000', '1312')]),
139 art = Artist(**artist)
140 self.assertEqual(art.name, 'Original Name')
141 self.assertEqual(art.mbid, VALID)
142 self.assertEqual(art.albumartist, artist['albumartist'])
143 artist.pop('albumartist')
144 art = Artist(**artist)
145 self.assertEqual(art.name, 'Original Name')
146 self.assertEqual(art.albumartist, 'Original Name')
148 def test_empty_name(self):
154 with self.assertRaises(MetaException,
155 msg='{} does not raise an except.'.format(args)):
158 class TestMetaContainers(unittest.TestCase):
161 a = Meta(mbid=VALID, name='a')
162 b = Meta(mbid=VALID, name='b')
163 c = Meta(mbid=VALID.replace('11', '22'), name='b')
164 # redondant with Meta test_comparison, but anyway
165 cont = MetaContainer([a, b, c])
166 self.assertTrue(len(cont) == 2)
167 self.assertTrue(a in cont)
168 self.assertTrue(b in cont)
169 self.assertTrue(Meta(name='a') in cont)
171 def test_intersection_difference(self):
172 # Now set works as expected with composite (name/mbid) collections of Meta
174 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
176 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
177 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
178 art01 = Meta(name='Aphex Twin', mbid=None)
179 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
180 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
181 art01._Meta__mbid = art00.mbid
182 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
183 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
184 art01._Meta__mbid = art00.mbid.replace('229', '330')
185 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
187 # vim: ai ts=4 sw=4 sts=4 expandtab