Yading/musixmatch-master/tests/base.py
Go to the documentation of this file.
1 import unittest
2 from musixmatch import *
3 
4 class TestBase(unittest.TestCase):
5 
6  Class = base.Base
7  def test_label(self):
8  self.assertEqual(self.Class.label(), self.Class.__name__.lower())
9 
10 class TestItem(TestBase):
11  Class = base.Item
12  item = { "item_id": "9673" }
13  item_str = "{ 'item_id': '9673'}"
14  item_repr = "Item({'item_id': '9673'})"
15  item_hash = 9673
16  item_id = 'item_id'
17 
18  # def test_fromResponseMessage(self):
19  # self.assertRaises(api.Error,
20  # self.Class.fromResponseMessage, self.fail)
21  # success = self.Class.fromResponseMessage(self.success)
22  # self.assertEqual(success[item_id],
23  # self.success['body'][self.label()][self.item_id])
25  item = self.Class.fromDictionary(self.item)
26  # Data integrity
27  for k in self.item.keys():
28  self.assertEqual(item[k], self.item[k])
29 
30  def test__str__(self):
31  item = self.Class.fromDictionary(self.item)
32  self.assertEqual(str(item),self.item_str)
33 
34  def test__repr__(self):
35  item = self.Class.fromDictionary(self.item)
36  self.assertEqual(repr(item),self.item_repr)
37 
38  def test__hash__(self):
39  item = self.Class.fromDictionary(self.item)
40  self.assertEqual(hash(item), self.item_hash)
41 
42 class TestCollection(unittest.TestCase):
43 
44  Class = base.ItemsCollection
45  AllowedContent = base.ItemsCollection.allowedin()
46  item_list = 'item_list'
47  item_id = 'item_id'
48  item = 'item'
49  message = {
50  "body": {
51  "item_list": [
52  {
53  "item": {
54  "item_id": "292",
55  "item_name": "item_292"
56  }
57  },
58  {
59  "item": {
60  "item_id": "8976",
61  "item_name": "item_8976"
62  }
63  },
64  {
65  "item": {
66  "item_id": "9673",
67  "item_name": "item_9673"
68  }
69  }
70  ]
71  },
72  "header": {
73  "execute_time": 0.14144802093506001,
74  "status_code": 200
75  }
76  }
77 
78  def test_insert(self):
79  collection = self.Class()
80  saved = self.message['body'][self.item_list][0][self.item]
81  item = self.AllowedContent(saved)
82  collection.insert(0, item)
83  # Item correctly inserted
84  self.assertEqual(collection[0], item)
85 
86  saved = self.message['body'][self.item_list][1][self.item]
87  collection.insert(0, saved)
88  # Item corectly casted to self.AllowedContent
89  self.assertEqual(type(collection[0]), self.AllowedContent)
90  # Item content integrity
91  self.assertEqual(collection[0][self.item_id], saved[self.item_id])
92  # Previously inserted item has shifted position
93  self.assertEqual(collection[1], item)
94 
95  def test_append(self):
96  collection = self.Class()
97  saved = self.message['body'][self.item_list][1][self.item]
98  item = self.AllowedContent.fromDictionary(saved)
99  collection.append(item)
100  # Item correctly appended
101  self.assertEqual(collection[0], item)
102 
103  saved = self.message['body'][self.item_list][2][self.item]
104  collection.append(saved)
105  # Item correctly appended
106  self.assertEqual(collection[1][self.item_id], saved[self.item_id])
107 
108  saved = self.message['body'][self.item_list][0][self.item]
109  collection.append(saved)
110  # Item corectly casted to self.AllowedContent
111  self.assertEqual(type(collection[2]), self.AllowedContent)
112  # Item content integrity
113  self.assertEqual(collection[2][self.item_id], saved[self.item_id])
114 
115  def test_extend(self):
116  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
117  collection = self.Class(self.AllowedContent(items[0]))
118  self.assertEqual(type(collection[0]), self.AllowedContent)
119  typed = [ self.AllowedContent(i) for i in items[1:] ]
120  for i in typed:
121  self.assertEqual(type(i), self.AllowedContent)
122  collection.extend(typed)
123  # Collection correctly extended
124  self.assertEqual(collection[1], typed[0])
125  self.assertEqual(collection[2], typed[1])
126 
127  row = items[:2] + items[1:3]
128  collection.extend(row)
129  # Items content integrity: no duplicate
130  self.assertEqual(len(collection), 3)
131 
132  collection = self.Class()
133  collection.extend(row)
134  self.assertEqual(len(row), 4)
135  self.assertEqual(len(collection), 3)
136  # Items corectly casted to self.AllowedContent
137  for i in range(3):
138  self.assertEqual(type(collection[i]), self.AllowedContent)
139 
140  # def test__setitem__(self):
141  # collection = self.Class(self.AllowedContent(
142  # self.message['body'][self.item_list][1][self.item]))
143  # saved = self.message['body'][self.item_list][2][self.item]
144  # item = self.AllowedContent(saved)
145  # collection[0] = item
146  # # Index of ItemsCollection correctly set
147  # self.assertEqual(collection[0], item)
148 
149  # saved = self.message['body'][self.item_list][0][self.item]
150  # collection[0] = saved
151  # # Item corectly casted to self.AllowedContent
152  # self.assertEqual(type(collection[0]),self.AllowedContent)
153  # # Item content integrity
154  # self.assertEqual(collection[0][self.item_id], saved[self.item_id])
155  # # Wrong setting
156  # self.assertRaises(IndexError, collection.__setitem__, 9, saved)
157  # self.assertRaises(TypeError, collection.__setitem__, 'test', saved)
158 
159  def test_page(self):
160  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
161  collection = self.Class(*items)
162  self.assertEqual(len(collection), 3)
163  for i in range(3):
164  self.assertEqual(type(collection[i]), self.AllowedContent)
165  page = collection.page(1,2)
166  self.assertEqual(len(page), 1)
167  self.assertEqual(type(page), self.Class)
168  self.assertEqual(type(page[0]), self.AllowedContent)
169 
170  def test_pages(self):
171  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
172  collection = self.Class(*items)
173  self.assertEqual(len(collection), 3)
174  pages = collection.pages(2)
175  self.assertEqual(pages, 2)
176 
177  def test_paged(self):
178  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
179  collection = self.Class(*items)
180  self.assertEqual(len(collection), 3)
181  for i in range(3):
182  self.assertEqual(type(collection[i]), self.AllowedContent)
183  paged = collection.paged(2)
184  self.assertEqual(len(paged), 2)
185  for i,l in zip(range(2), (2,1)):
186  self.assertEqual(len(paged[i]), l)
187  self.assertEqual(type(paged[i]), self.Class)
188  for p,i in [(0,0),(0,1),(1,0)]:
189  self.assertEqual(id(paged[p][i]), id(collection[(2*p)+i]))
190 
191  def test_pager(self):
192  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
193  collection = self.Class(*items)
194  self.assertEqual(len(collection), 3)
195  for i in range(3):
196  self.assertEqual(type(collection[i]), self.AllowedContent)
197  pager = []
198  for page in collection.pager(2):
199  self.assertEqual(type(page), self.Class)
200  self.assertEqual(type(page[0]), self.AllowedContent)
201  pager.append(page)
202  self.assertEqual(len(pager), 2)
203  self.assertEqual(len(pager[0]), 2)
204  self.assertEqual(len(pager[1]), 1)
205 
206  def test__add__(self):
207  items = [ i[self.item] for i in self.message['body'][self.item_list] ]
208  collection1 = self.Class(*items[:2])
209  collection2 = self.Class(*items[1:3])
210  collection3 = collection1 + collection2
211  # Collection correctly created
212  self.assertEqual(type(collection3), self.Class)
213  self.assertEqual(len(collection3), 3)
214 
enum AVCodecID id
Definition: mxfenc.c:89
static int hash(int head, const int add)
Hash function adding character.
Definition: lzwenc.c:74
location of range
int len