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'))
60 art00 = Meta(name='Aphex Twin',
61 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
62 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
63 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
64 art03 = Meta(name='Aphex Twin',
65 mbid='322942a1-6f70-4f48-866e-238cb2308fbd')
67 self.assertTrue(len({art00, art02}) == 1)
68 art00._Meta__name = art02._Meta__name = 'Aphex Twin'
69 self.assertTrue(len({art00, art02}) == 1)
71 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
72 art00._Meta__mbid = None
73 self.assertTrue(len({art00, art02}) == 2,
74 'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
75 # equivalent: self.assertTrue(hash(art00) != hash(art02))
77 # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
78 art00._Meta__mbid = art02._Meta__mbid = None
79 # equivalent: self.assertTrue(hash(art00) == hash(art02))
80 self.assertTrue(len({art00, art02}) == 1,
81 'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
83 self.assertTrue(hash(art00) != hash(art03),
84 'wrong: hash({!r}) == hash({!r})'.format(art00, art03))
86 def test_comparison(self):
87 art00 = Meta(name='Aphex Twin',
88 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
89 art01 = Meta(name='Aphex Twin',)
90 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
91 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
92 art10 = Meta(name='Aphex Twin',
93 mbid='d22942a1-6f70-4f48-866e-238cb2308fbd')
94 # testing name/mbid == name/None
95 self.assertTrue(art00 == art01, 'wrong: %r != %r' % (art00, art01))
96 # testing name/mbid == other_name/mbid
97 self.assertTrue(art00 == art02, 'wrong: %r != %r' % (art00, art02))
98 # testing name/mbid != name/other_mbid
99 self.assertTrue(art00 != art10, 'wrong: %r == %r' % (art00, art10))
100 # Testing name/None == name/None
101 art10._Meta__mbid = None
102 self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01))
104 def test_mpd_serialization(self):
105 """Controls serialization of names"""
106 name = "Heaven's Door"
107 heavens_door = Meta(name=name)
108 target = r"Heaven\'s Door"
109 self.assertEqual(heavens_door.name_sz, target)
110 self.assertEqual(heavens_door.name, name)
111 self.assertEqual(heavens_door.names_sz, {target})
112 heavens_door.add_alias(name+" LP")
113 self.assertEqual(heavens_door.aliases_sz, {target+" LP"})
114 # Controls inheritance
115 heavens_door = Album(name=name)
116 self.assertEqual(heavens_door.name_sz, target)
117 heavens_door = Artist(name=name)
118 self.assertEqual(heavens_door.name_sz, target)
121 class TestArtistObject(unittest.TestCase):
125 SEPARATOR.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
126 'albumartist': 'Name',
127 'musicbrainz_artistid':
128 SEPARATOR.join([VALID, VALID.replace('0000', '1312')]),
130 art = Artist(**artist)
131 self.assertEqual(art.name, 'Original Name')
132 self.assertEqual(art.mbid, VALID)
133 self.assertEqual(art.albumartist, artist['albumartist'])
134 artist.pop('albumartist')
135 art = Artist(**artist)
136 self.assertEqual(art.name, 'Original Name')
137 self.assertEqual(art.albumartist, 'Original Name')
139 def test_empty_name(self):
145 with self.assertRaises(MetaException,
146 msg='{} does not raise an except.'.format(args)):
149 class TestMetaContainers(unittest.TestCase):
152 a = Meta(mbid=VALID, name='a')
153 b = Meta(mbid=VALID, name='b')
154 c = Meta(mbid=VALID.replace('11', '22'), name='b')
155 # redondant with Meta test_comparison, but anyway
156 cont = MetaContainer([a, b, c])
157 self.assertTrue(len(cont) == 2)
158 self.assertTrue(a in cont)
159 self.assertTrue(b in cont)
160 self.assertTrue(Meta(name='a') in cont)
162 def test_intersection_difference(self):
163 # Now set works as expected with composite (name/mbid) collections of Meta
165 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
167 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
168 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
169 art01 = Meta(name='Aphex Twin', mbid=None)
170 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
171 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
172 art01._Meta__mbid = art00.mbid
173 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
174 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
175 art01._Meta__mbid = art00.mbid.replace('229', '330')
176 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
178 # vim: ai ts=4 sw=4 sts=4 expandtab