1 # -*- coding: utf-8 -*-
5 from sima.lib.meta import Meta, Artist, 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))
106 class TestArtistObject(unittest.TestCase):
109 artist = {'artist': SEPARATOR.join(['Original Name', 'Featuring Nane', 'Featureā¦']),
110 'albumartist': 'Name',
111 'musicbrainz_artistid': VALID,
112 'musicbrainz_albumartistid': VALID.replace('11', '22'),
114 art = Artist(**artist)
115 self.assertTrue(art.name == 'Name')
116 self.assertTrue(art.mbid == VALID.replace('11', '22'))
117 artist.pop('musicbrainz_albumartistid')
118 art = Artist(**artist)
119 self.assertTrue(art.mbid == VALID)
120 artist.pop('albumartist')
121 art = Artist(**artist)
122 self.assertTrue(art.name == 'Original Name', art.name)
124 def test_empty_name(self):
130 with self.assertRaises(MetaException,
131 msg='{} does not raise an except.'.format(args)):
134 class TestMetaContainers(unittest.TestCase):
137 a = Meta(mbid=VALID, name='a')
138 b = Meta(mbid=VALID, name='b')
139 c = Meta(mbid=VALID.replace('11', '22'), name='b')
140 # redondant with Meta test_comparison, but anyway
141 cont = MetaContainer([a, b, c])
142 self.assertTrue(len(cont) == 2)
143 self.assertTrue(a in cont)
144 self.assertTrue(b in cont)
145 self.assertTrue(Meta(name='a') in cont)
147 def test_intersection_difference(self):
148 # Now set works as expected with composite (name/mbid) collections of Meta
150 # >>> len(MetaContainer([Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)]) == 1
152 # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>}) == 2
153 art00 = Meta(name='Aphex Twin', mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
154 art01 = Meta(name='Aphex Twin', mbid=None)
155 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
156 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
157 art01._Meta__mbid = art00.mbid
158 self.assertTrue(MetaContainer([art00]) & MetaContainer([art01]))
159 self.assertFalse(MetaContainer([art01]) - MetaContainer([art01]))
160 art01._Meta__mbid = art00.mbid.replace('229', '330')
161 self.assertFalse(MetaContainer([art00]) & MetaContainer([art01]))
163 # vim: ai ts=4 sw=4 sts=4 expandtab