Yading/musixmatch-master/tests/api.py
Go to the documentation of this file.
1 import unittest
2 from musixmatch import *
3 try:
4  from cStringIO import StringIO
5 except ImportError:
6  from StringIO import StringIO
7 
8 class TestError(unittest.TestCase):
9 
10  def test__str__(self):
11  error = api.Error('test error', Exception('test exception'))
12  self.assertEqual(str(error), 'test error: test exception')
13 
14 class TestResponseStatusCode(unittest.TestCase):
15 
16  def test__init__(self):
17  self.assertRaises(ValueError, api.ResponseStatusCode, 'fail')
18 
19  self.assertRaises(TypeError, api.ResponseStatusCode, [1,2,3])
20 
21  def test__int__(self):
22  self.assertEqual(int(api.ResponseStatusCode('1')), 1)
23 
24  def test__str__(self):
25  self.assertEqual(str(api.ResponseStatusCode('200')),
26  'The request was successful.')
27 
28  self.assertEqual(str(api.ResponseStatusCode('-1')),
29  'Unknown status code -1!')
30 
31  def test__nonzero__(self):
32  self.assertEqual(bool(api.ResponseStatusCode('200')), True)
33  self.assertEqual(bool(api.ResponseStatusCode('404')), False)
34 
35 class TestResponseMessage(unittest.TestCase):
36 
37  def test__init__(self):
38  self.assertRaises(NotImplementedError, api.ResponseMessage, '')
39 
40 class TestXMLResponseMessage(unittest.TestCase):
41 
42  message = """<message>
43  <header>
44  <status_code>200</status_code>
45  </header>
46  <body>
47  </body>
48 </message>"""
49 
50  def test_status_code(self):
51  message = api.XMLResponseMessage(StringIO(self.message))
52  self.assertEqual(isinstance(message.status_code, api.ResponseStatusCode), True)
53 
54 class TestJsonResponseMessage(unittest.TestCase):
55  message = """{"message":{
56  "header":{
57  "status_code":200},
58  "body":{
59 }}}"""
60 
61  def test_status_code(self):
62  message = api.JsonResponseMessage(StringIO(self.message))
63  self.assertEqual(isinstance(message.status_code, api.ResponseStatusCode), True)
64 
65 class TestQueryString(unittest.TestCase):
66  def test__str__(self):
67  keywords = { 'country': 'it', 'page': 1, 'page_size': 3 }
68  query_string = api.QueryString(keywords)
69  self.assertEqual(str(query_string), 'country=it&page=1&page_size=3')
70  def test__repr__(self):
71  keywords = { 'apikey': 'it', 'id': 12345, 'format': 'json' }
72  query_string = api.QueryString(keywords)
73  self.assertEqual(repr(query_string).count('apikey'), 0)
74 
75 class TestRequest(unittest.TestCase):
76  def test__str__(self):
77  url = 'http://api.musixmatch.com/ws/1.1/test?apikey=apikey&format=format'.encode('utf-8')
78  method = api.Method('test')
79  query_string = api.QueryString({'apikey':'apikey','format':'format'})
80  request = api.Request(method, query_string)
81  self.assertEqual(str(request), url)
82 
83 class TestMethod(unittest.TestCase):
84 
86  method = api.Method('test')
87  self.assertEqual(hasattr(method, 'subtest'), True)
88  self.assertEqual(hasattr(method, '__nothing__'), False)
89 
90 if __name__ == '__main__':
91  unittest.main()
void INT64 INT64 count
Definition: avisynth_c.h:594