-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_mbid_mapping.py
72 lines (55 loc) · 2.31 KB
/
test_mbid_mapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import unittest
import unittest.mock
import troi
import troi.musicbrainz.mbid_mapping
return_data = [
{
"artist_credit_arg": "morcheeba",
"artist_credit_id": 963,
"artist_credit_name": "Morcheeba",
"index": 0,
"recording_arg": "trigger hippie",
"recording_mbid": "97e69767-5d34-4c97-b36a-f3b2b1ef9dae",
"recording_name": "Trigger Hippie",
"release_mbid": "9db51cd6-38f6-3b42-8ad5-559963d68f35",
"release_name": "Who Can You Trust?",
"year": 1996
}
]
def mocked_requests_post(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
e = troi.musicbrainz.mbid_mapping.MBIDMappingLookupElement()
if args[0] == e.SERVER_URL:
return MockResponse(return_data, 200)
return MockResponse(None, 404)
class TestMBIDMapping(unittest.TestCase):
@unittest.mock.patch('requests.post', side_effect=mocked_requests_post)
def test_read(self, req):
e = troi.musicbrainz.mbid_mapping.MBIDMappingLookupElement()
r = [ troi.Recording("trigger hippie", artist=troi.Artist("morcheeba")) ]
entities = e.read([r])
req.assert_called_with(e.SERVER_URL, json=[{'[artist_credit_name]': 'morcheeba', '[recording_name]': 'trigger hippie'}])
assert len(entities) == 1
assert entities[0].artist.artist_credit_id == 963
assert entities[0].artist.name == "Morcheeba"
assert entities[0].release.mbid == "9db51cd6-38f6-3b42-8ad5-559963d68f35"
assert entities[0].release.name == "Who Can You Trust?"
assert entities[0].mbid == "97e69767-5d34-4c97-b36a-f3b2b1ef9dae"
assert entities[0].name == "Trigger Hippie"
assert entities[0].year == 1996
@unittest.mock.patch('requests.post')
def test_read_remove_unmatched(self, req):
mock = unittest.mock.MagicMock()
mock.status_code = 200
mock.text = "{}"
req.return_value = mock
e = troi.musicbrainz.mbid_mapping.MBIDMappingLookupElement(True)
r = [ troi.Recording("track not found", artist=troi.Artist("artist not found")) ]
entities = e.read([r])
assert len(entities) == 0