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('4', '3')))
17 self.assertFalse(is_uuid4(VALID.replace('a', 'z')))
25 with self.assertRaises(MetaException,
26 msg='{} does not raise an except.'.format(args)):
29 def test_equality(self):
30 a = Meta(mbid=VALID, name='a')
31 b = Meta(mbid=VALID, name='b')
32 c = Meta(mbid=VALID.upper(), name='c')
33 self.assertEqual(a, b)
34 self.assertEqual(a, c)
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_aliases(self):
47 art0 = Meta(name='Silver Mt. Zion')
48 art0.add_alias('A Silver Mt. Zion')
51 # Controls 'Silver Mt. Zion' is not in aliases
52 self.assertTrue('Silver Mt. Zion' not in art0.aliases)
54 # test equality str with Obj.__aliases
55 self.assertTrue(art0 == 'A Silver Mt. Zion')
56 self.assertTrue('A Silver Mt. Zion' == art0)
57 # test equality Obj.__name with OgjBis.__aliases
58 self.assertTrue(art0 == Meta(name='A Silver Mt. Zion'))
61 art00 = Meta(name='Aphex Twin',
62 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
63 art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
64 mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
65 art03 = Meta(name='Aphex Twin',
66 mbid='322942a1-6f70-4f48-866e-238cb2308fbd')
68 self.assertTrue(len({art00, art02}) == 1)
69 art00._Meta__name = art02._Meta__name = 'Aphex Twin'
70 self.assertTrue(len({art00, art02}) == 1)
72 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
73 art00._Meta__mbid = None
74 self.assertTrue(len({art00, art02}) == 2,
75 'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
76 # equivalent: self.assertTrue(hash(art00) != hash(art02))
78 # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
79 art00._Meta__mbid = art02._Meta__mbid = None
80 # equivalent: self.assertTrue(hash(art00) == hash(art02))
81 self.assertTrue(len({art00, art02}) == 1,
82 'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
84 self.assertTrue(hash(art00) != hash(art03),
85 'wrong: hash({!r}) == hash({!r})'.format(art00, art03))
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))
105 def test_mpd_serialization(self):
106 """Controls serialization of names"""
107 name = "Heaven's Door"
108 heavens_door = Meta(name=name)
109 target = r"Heaven\'s Door"
110 self.assertEqual(heavens_door.name_sz, target)
111 self.assertEqual(heavens_door.name, name)
112 self.assertEqual(heavens_door.names_sz, {target})
113 heavens_door.add_alias(name+" LP")
114 self.assertEqual(heavens_door.aliases_sz, {target+" LP"})
115 # Controls inheritance
116 heavens_door = Album(name=name)
117 self.assertEqual(heavens_door.name_sz, target)
118 heavens_door = Artist(name=name)
119 self.assertEqual(heavens_door.name_sz, target)
122 class TestArtistObject(unittest.TestCase):
125 artist = {'artist': SEPARATOR.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
126 'albumartist': 'Name',
127 'musicbrainz_artistid': VALID,
128 'musicbrainz_albumartistid': VALID.replace('11', '22'),
130 art = Artist(**artist)
131 self.assertTrue(art.name == 'Name')
132 self.assertTrue(art.mbid == VALID.replace('11', '22'))
133 artist.pop('musicbrainz_albumartistid')
134 art = Artist(**artist)
135 self.assertTrue(art.mbid == VALID)
136 artist.pop('albumartist')
137 art = Artist(**artist)
138 self.assertTrue(art.name == 'Original Name', art.name)
140 def test_empty_name(self):
146 with self.assertRaises(MetaException,
147 msg='{} does not raise an except.'.format(args)):
150 class TestMetaContainers(unittest.TestCase):
153 a = Meta(mbid=VALID, name='a')
154 b = Meta(mbid=VALID, name='b')
155 c = Meta(mbid=VALID.replace('11', '22'), name='b')
156 # redondant with Meta test_comparison, but anyway
157 cont = MetaContainer([a, b, c])
158 self.assertTrue(len(cont) == 2)
159 self.assertTrue(a in cont)
160 self.assertTrue(b in cont)
161 self.assertTrue(Meta(name='a') in cont)
163 def test_intersection_difference(self):
164 # Now set works as expected with composite (name/mbid) collections of Meta
166 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
168 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
169 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
170 art01 = Meta(name='Aphex Twin', mbid=None)
171 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
172 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
173 art01._Meta__mbid = art00.mbid
174 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
175 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
176 art01._Meta__mbid = art00.mbid.replace('229', '330')
177 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
179 # vim: ai ts=4 sw=4 sts=4 expandtab