]> kaliko git repositories - mpd-sima.git/blob - tests/test_meta.py
Add unittest for Meta object
[mpd-sima.git] / tests / test_meta.py
1 # -*- coding: utf-8 -*-
2
3 import unittest
4
5 from sima.lib.meta import Meta, Artist, is_uuid4
6 from sima.lib.meta import WrongUUID4, MetaException
7
8 VALID = '110E8100-E29B-41D1-A716-116655250000'
9
10 class TestMetaObject(unittest.TestCase):
11
12     def test_uuid_integrity(self):
13         Meta(mbid=VALID, name='test')
14         Meta(mbid=VALID.lower(), name='test')
15         wrong = VALID +'a'
16         self.assertRaises(WrongUUID4, is_uuid4, wrong)
17         #  test UUID4 format validation
18         self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('4', '3'))
19         self.assertRaises(WrongUUID4, is_uuid4, VALID.replace('A', 'Z'))
20
21     def test_init(self):
22         for args in [
23                 {'mbid':VALID},
24                 {'name': None},
25                 {},
26                 ]:
27             with self.assertRaises(MetaException,
28                                    msg='{} does not raise an except.'.format(args)):
29                 Meta(**args)
30
31     def test_equality(self):
32         a = Meta(mbid=VALID, name='a')
33         b = Meta(mbid=VALID, name='b')
34         self.assertEqual(a, b)
35
36     def test_hash(self):
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})
43         # mbid is immutable
44         self.assertRaises(AttributeError, a.__setattr__, 'mbid', VALID)
45
46     def test_identity(self):
47         a = Meta(mbid=VALID, name='a')
48         b = Meta(mbid=VALID, name='a')
49         self.assertTrue(a is not b)
50
51     def test_aliases(self):
52         art0 = Meta(name='Silver Mt. Zion')
53         art0.add_alias('A Silver Mt. Zion')
54         art0.add_alias(art0)
55
56         # Controls 'Silver Mt. Zion' is not in aliases
57         self.assertTrue('Silver Mt. Zion' not in art0.aliases)
58
59         # test equality str with Obj.__aliases
60         self.assertTrue(art0 == 'A Silver Mt. Zion')
61         self.assertTrue('A Silver Mt. Zion' == art0)
62         # test equality Obj.__name with OgjBis.__aliases
63         self.assertTrue(art0 == Meta(name='A Silver Mt. Zion'))
64
65     def test_union(self):
66         art00 = Meta(name='Aphex Twin',
67                            mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
68         art02 = Meta(name='Some Other Name not even close, avoid fuzzy match',
69                            mbid='f22942a1-6f70-4f48-866e-238cb2308fbd')
70
71         self.assertTrue(len({art00, art02}) == 1)
72         art00._Meta__name = art02._Meta__name = 'Aphex Twin'
73         self.assertTrue(len({art00, art02}) == 1)
74
75         # >>> len({Artist(name='Name'), Artist(name='Name', mbid=<UUID4>)}) == 2
76         art00._Meta__mbid = None
77         self.assertTrue(len({art00, art02}) == 2,
78                         'wrong: hash({!r}) == hash({!r})'.format(art00, art02))
79         # equivalent: self.assertTrue(hash(art00) != hash(art02))
80
81         # >>> len({Artist(name='Name'), Artist(name='Name')}) == 1
82         art00._Meta__mbid = art02._Meta__mbid = None
83         # equivalent: self.assertTrue(hash(art00) == hash(art02))
84         self.assertTrue(len({art00, art02}) == 1,
85                         'wrong: hash({!r}) != hash({!r})'.format(art00, art02))
86
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))
104
105
106 class TestArtistObject(unittest.TestCase):
107
108     def test_init(self):
109         artist = {'artist': ['Name featuring', 'Feature'],
110                   'albumartist': 'Name',
111                   'musicbrainz_artistid': VALID,
112                   'musicbrainz_albumartistid': VALID.replace('11', '22'),
113                   }
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
123 # vim: ai ts=4 sw=4 sts=4 expandtab