Collections: CollectionTest.2.py

File CollectionTest.2.py, 17.0 KB (added by njaal, 4 years ago)
Line 
1import unittest
2
3from Collection import *
4
5class CollectionDBTest(unittest.TestCase):
6
7    def setUp(self):
8        self.db = CollectionDB(":memory:")
9
10    def tearDown(self):
11        self.db.clear()
12       
13    def testcreate(self):
14
15        list = self.db.get_names()
16        self.assertEqual(len(list), 0)
17
18        # Try invalid type
19        try:
20            self.db.new_collection("testName", "shouldFail", "invalid")
21            self.fail("Adding collection with text type does not fail")
22        except:
23            pass
24
25        try:
26            self.db.new_collection("testName", "shouldFail", None)
27            self.fail("Adding collection with NONE type does not fail")
28        except:
29            pass
30       
31        # Try invalid ID
32        try:
33            self.db.new_collection("testName", "shouldFail", id=-1)
34            self.fail("Adding collection with ID=-1 does not fail")
35        except Exception,e:
36            pass
37       
38        try:
39            self.db.new_collection("testName", "shouldFail", id="ivalid")
40            self.fail("Adding collection with invalid does not fail")
41        except:
42            pass
43
44        self.db.clear()
45       
46        # Try to actually create something
47        self.db.new_collection("testName", "testDecription")
48        list = self.db.get_names()
49        self.assertEqual(len(list), 1)
50        self.assertEqual(list, ['Testname'])
51       
52        # Delete it
53        self.db.clear()
54        list = self.db.get_names()
55        self.assertEqual(len(list), 0)
56
57        self.db.new_collection("testName", "testDecription", type=1)
58        list = self.db.get_names()
59        self.assertEqual(len(list), 1)
60        self.assertEqual(list, ['Testname'])
61       
62        # Delete it
63        self.db.clear()
64        list = self.db.get_names()
65        self.assertEqual(len(list), 0)
66
67        self.db.new_collection("testName", "testDecription", id=123)
68        list = self.db.get_names()
69        self.assertEqual(len(list), 1)
70        self.assertEqual(list, ['Testname'])
71       
72        # Delete it
73        self.db.clear()
74        list = self.db.get_names()
75        self.assertEqual(len(list), 0)
76
77        self.db.new_collection("testName", "testDecription", type=1, id=123)
78        list = self.db.get_names()
79        self.assertEqual(len(list), 1)
80        self.assertEqual(list, ['Testname'])
81       
82        # Should fail to create duplicate
83        try:
84            self.db.new_collection("testName", "testDecription", type=1, id=123)
85            self.fail("Was allowed to create two identical collections")
86        except:
87            pass
88       
89        list = self.db.get_names()
90        self.assertEqual(len(list), 1)
91        self.assertEqual(list, ['Testname'])
92
93        try:
94            self.db.new_collection("testName2", "testDecription2", type=2, id=123)
95            self.fail("Was allowed to create two collections with same id")
96        except:
97
98            pass
99       
100        list = self.db.get_names()
101        self.assertEqual(len(list), 1, "Failed create created something!")
102        self.assertEqual(list, ['Testname'])
103
104        # Clean up
105        self.db.clear()
106        list = self.db.get_names()
107
108
109    def testinvalidget(self):
110
111        # Invalid get
112        try:
113            self.db.get_collection()
114            self.fail("get_collection without parameters did not fail")
115        except:
116            pass
117
118        try:
119            self.db.get_collection(id=62537)
120            self.fail("get_collection does not fail with unknown id")
121        except NotFoundException:
122            pass
123
124        try:
125            self.db.get_collection(name="SomeName")
126            self.fail("get_collection does not fail with unknown name")
127        except NotFoundException:
128            pass
129
130        try:
131            self.db.get_collection(id="invalid")
132            self.fail("get_collection does not fail with invalid id")
133        except:
134            pass
135
136    def testget(self):
137       
138        # Valid
139        collection = self.db.new_collection("testName", "testDescription")
140        collection2 = self.db.get_collection(name="testName")
141        self.assert_(collection, "Could not get created collection")
142        self.assertEqual(collection.get_id(), collection2.get_id())
143       
144        self.assertEqual(collection.get_name(), "Testname")
145        self.assertEqual(collection.get_description(), "testDescription")
146
147        id2 = collection.get_id()
148        self.assertEqual(collection.get_id(), id2)
149       
150        collection2 = self.db.get_collection(id=collection.get_id())
151        self.assertEqual(collection.get_id(), collection2.get_id())
152        self.assertEqual(collection2.get_name(), "Testname")
153        self.assertEqual(collection2.get_description(), "testDescription")
154
155    def testremove(self):
156
157        # Invalid
158        try:
159            self.db.remove_collection()
160            self.fail("Accepted remove remove_collection with no arguments")
161        except:
162            pass
163
164        try:
165            self.db.remove_collection(name="nonexistent")
166            self.fail("Removed non-existent collection by name")
167        except NotFoundException:
168            pass
169
170        try:
171            self.db.remove_collection(id=123)
172            self.fail("Removed non-existent collection by id")
173        except NotFoundException:
174            pass
175
176        try:
177            self.db.remove_collection(id="invalid")
178            self.fail("Finds collection by invalid id")
179        except Exception:
180            pass
181
182        # Now remove properly
183        coll = self.db.new_collection("testName", "testDescription")
184        self.db.remove_collection(id=coll.get_id())
185        list = self.db.get_names()
186        self.assertEqual(len(list), 0)
187       
188        # Remove by name
189        coll = self.db.new_collection("testName", "testDescription")
190        self.db.remove_collection(name="testName")
191        list = self.db.get_names()
192        self.assertEqual(len(list), 0)
193
194    def testsmartaddinvalid(self):
195
196        coll = self.db.new_collection("testName", "testDecription")
197        valid_url = "http://somewhere/someplace.torrent"
198        # INVALID
199        try:
200            self.db.smart_add("Invalid", valid_url)
201            self.fail("Smart_add added invalid")
202        except NotFoundException:
203            pass
204       
205        try:
206            self.db.smart_add("valid s01e02 therest.torrent", None)
207            self.fail("Smart_add added resource without url")
208        except Exception:
209            pass
210
211        try:
212            self.db.smart_add("invalid se02 therest.torrent", valid_url)
213            self.fail("Smart_add added resource without season")
214        except Exception:
215            pass
216
217        try:
218            self.db.smart_add("invalid s12e therest.torrent", valid_url)
219            self.fail("Smart_add added resource without episode")
220        except Exception:
221            pass
222
223        try:
224            self.db.smart_add("invalid x12 therest.torrent", valid_url)
225            self.fail("Smart_add added resource without season (SxE)")
226        except Exception:
227            pass
228
229        try:
230            self.db.smart_add("invalid 12x therest.torrent", valid_url)
231            self.fail("Smart_add added resource without episode (SxE)")
232        except Exception:
233            pass
234
235        try:
236            self.db.smart_add("invalid 2007 therest.torrent", valid_url)
237            self.fail("Got false positive")
238        except NotFoundException:
239            pass
240
241        try:
242            self.db.smart_add("invalid season therest.torrent", valid_url)
243            self.fail("Got false positive")
244        except NotFoundException:
245            pass
246
247    def testsmartaddvalid(self):
248
249        valid_url = "http://somewhere/someplace.torrent"
250       
251        # title s1e12
252        (c, entry) = self.db.smart_add("Valid s3e01 therest.torrent", valid_url)
253        self.assertEqual(c.get_name(), "Valid")
254        self.assertEqual(entry, (3, 1, 0, "")) # TODO: Update with type
255
256        (c, entry) = self.db.smart_add("Valid S3e02 therest.torrent", valid_url)
257        self.assertEqual(c.get_name(), "Valid")
258        self.assertEqual(entry, (3, 2, 0, "")) # TODO: Update with type
259       
260        (c, entry) = self.db.smart_add("Valid s3E03 therest.torrent", valid_url)
261        self.assertEqual(c.get_name(), "Valid")
262        self.assertEqual(entry, (3, 3, 0, "")) # TODO: Update with type
263           
264        (c, entry) = self.db.smart_add("valid s3E04.torrent", valid_url)
265        self.assertEqual(c.get_name(), "Valid")
266        self.assertEqual(entry, (3, 4, 0, "")) # TODO: Update with type
267
268        (c, entry) = self.db.smart_add("valid s3E05.torrent", valid_url)
269        self.assertEqual(c.get_name(), "Valid")
270        self.assertEqual(entry, (3, 5, 0, "")) # TODO: Update with type
271
272        # title 1x02
273        (c, entry) = self.db.smart_add("valid 4x01.torrent", valid_url)
274        self.assertEqual(c.get_name(), "Valid")
275        self.assertEqual(entry, (4, 1, 0, "")) # TODO: Update with type
276
277        (c, entry) = self.db.smart_add("valid 4x02 the rest.torrent", valid_url)
278        self.assertEqual(c.get_name(), "Valid")
279        self.assertEqual(entry, (4, 2, 0, "")) # TODO: Update with type
280
281        # title 103
282        (c, entry) = self.db.smart_add("valid 501.torrent", valid_url)
283        self.assertEqual(c.get_name(), "Valid")
284        self.assertEqual(entry, (5, 1, 0, "")) # TODO: Update with type
285
286        # title 0103
287        (c, entry) = self.db.smart_add("valid 0502.torrent", valid_url)
288        self.assertEqual(c.get_name(), "Valid")
289        self.assertEqual(entry, (5, 2, 0, "")) # TODO: Update with type
290       
291        # title season X
292        (c, entry) = self.db.smart_add("valid Season 6 complete.torrent", valid_url)
293        self.assertEqual(c.get_name(), "Valid")
294        self.assertEqual(entry, (6, 0, 0, "")) # TODO: Update with type
295
296        # title season X
297        (c, entry) = self.db.smart_add("valid Season7", valid_url)
298        self.assertEqual(c.get_name(), "Valid")
299        self.assertEqual(entry, (7, 0, 0, "")) # TODO: Update with type
300
301        # Quality
302        (c, entry) = self.db.smart_add("valid s3E05 720i.torrent", valid_url)
303        self.assertEqual(c.get_name(), "Valid")
304        self.assertEqual(entry, (3, 5, 0, "720i")) # TODO: Update with type
305
306        (c, entry) = self.db.smart_add("valid s3E05 720p ROXOR.torrent", valid_url)
307        self.assertEqual(c.get_name(), "Valid")
308        self.assertEqual(entry, (3, 5, 0, "720p")) # TODO: Update with type
309
310        (c, entry) = self.db.smart_add("valid s3E05 1080i.torrent", valid_url)
311        self.assertEqual(c.get_name(), "Valid")
312        self.assertEqual(entry, (3, 5, 0, "1080i")) # TODO: Update with type
313
314        (c, entry) = self.db.smart_add("valid s3E05 1080p.torrent", valid_url)
315        self.assertEqual(c.get_name(), "Valid")
316        self.assertEqual(entry, (3, 5, 0, "1080p")) # TODO: Update with type
317
318
319class CollectionTest(unittest.TestCase):
320
321    testSeason1 = [("1", "s1entry1", "First entry, first season", "url1", 1, 1, 0, "", None, 123456),
322                   ("2", "s1entry2", "Second entry, first season", "url2", 1, 2, 0, "", None, 123456),
323                   ("3", "s1entry3", "Third entry, first season", "url3", 1, 3, 0, "", None, 123456)]
324
325    testSeason2 = [("4", "s2entry1", "First entry, second season", "url4", 2, 1, 0, "720p", None, 123456),
326                   ("5", "s2entry2", "Second entry, second season", "url5", 2, 2, 0, "720p", None, 123456),
327                   ("6", "s2entry3", "Third entry, second season", "url6", 2, 3, 0, "720p", None, 123456),
328                   ("7", "s2entry3 dupe", "Third entry, second season", "url7", 2, 3, 0, "720p", None, 123456)]
329
330    def setUp(self):
331        self.db = CollectionDB(":memory:")
332
333        self.coll_id = self.db.new_collection("testCollection",
334                                              "testDescription").get_id()
335        self.assert_(self.coll_id != 0, "Failed to set up test collection")
336
337    def testinvalidcreate(self):
338
339        coll = self.db.get_collection(id=self.coll_id)
340        self.assert_(coll, "Missing test collection")
341
342        try:
343            coll.add_entry(None, None, None, None)
344            self.fail("Allowed 'None' as all parameters")
345        except:
346            pass
347
348        try:
349            coll.add_entry(None, "elemName", "elemDescription", "elemUrl")
350            self.fail("Allowed NONE as id")
351        except:
352            pass
353       
354        try:
355            coll.add_entry("123", None, "elemDescription", "elemUrl")
356            self.fail("Allowed NONE as name")
357        except:
358            pass
359       
360        try:
361            coll.add_entry("123", "elemName", None, "elemUrl")
362            self.fail("Allowed NONE as description")
363        except:
364            pass
365
366        try:
367            coll.add_entry("123", "elemName", "elemDescription", None)
368            self.fail("Allowed NONE as URL")
369        except:
370            pass
371       
372        try:
373            coll.add_entry("123", "elemName", "elemDecription", "elemUrl", major="invalid")
374            self.fail("Allowed invalid major")
375        except:
376            pass
377       
378        try:
379            coll.add_entry("123", "elemName", "elemDecription", "elemUrl", minor="invalid")
380            self.fail("Allowed invalid minor")
381        except:
382            pass
383       
384        try:
385            coll.add_entry("123", "elemName", "elemDecription", "elemUrl", type="invalid")
386            self.fail("Allowed invalid type")
387        except:
388            pass
389
390    def testcreate(self):
391
392        coll = self.db.get_collection(id=self.coll_id)
393        self.assert_(coll, "Missing test collection")
394
395        # Add season 1
396        for (id, name, desc, url, major, minor, type, quality, ts, size) in self.testSeason1:
397            coll.add_entry(id, name, desc, url, major, minor, type, quality, ts, size)
398
399        # Find season 1
400        season1 = coll.get_entries()
401        self.assertEqual(self.testSeason1, season1)
402
403        for entry  in self.testSeason1:
404            (id, name, desc, url, major, minor, type, quality, ts, size) = entry
405            entry2 = coll.get_entry(id=id)
406            self.assert_(entry2, "Missing entry %s"%id)
407            self.assertEquals(entry, entry2)
408       
409        # Add season 2
410        for (id, name, desc, url, major, minor, type, quality, ts, size) in self.testSeason2:
411            coll.add_entry(id, name, desc, url, major, minor, type, quality, ts, size)
412
413        # Find season 1
414        season1 = coll.get_entries(major=1)
415        self.assertEqual(self.testSeason1, season1)
416
417        for entry  in self.testSeason1:
418            (id, name, desc, url, major, minor, type, quality, ts, size) = entry
419            entry2 = coll.get_entry(id=id)
420            self.assert_(entry2, "Missing entry %s"%id)
421            self.assertEquals(entry, entry2)
422
423        # Find season 2
424        season2 = coll.get_entries(major=2)
425        self.assertEqual(self.testSeason2, season2)
426
427        for entry in self.testSeason2:
428            (id, name, desc, url, major, minor, type, quality, ts, size) = entry
429            entry2 = coll.get_entry(id=id)
430            self.assert_(entry2, "Missing entry %s"%id)
431            self.assertEqual(entry, entry2)
432
433        # Find one and one
434        for entry in self.testSeason1 + self.testSeason2:
435            (id, name, desc, url, major, minor, type, quality, ts, size) = entry
436            list = coll.get_entries(major=major, minor=minor, dupes=False)
437            self.assertEqual(len(list), 1)
438            entry2 = list[0]
439            if name != "s2entry3 dupe":
440                self.assert_(entry2, "Missing entry %s"%id)
441                self.assertEqual(entry, entry2)
442
443        # Do not fetch duplicates
444        season2 = coll.get_entries(major=2, dupes=False)
445        self.assertEqual(len(season2), 3)
446
447        # Find based on quality
448        season2 = coll.get_entries(quality="720P")
449        self.assertEqual(self.testSeason2, season2)
450       
451        empty = coll.get_entries(quality="1080p")
452        self.assertEqual(empty, [])
453       
454        # Empty string means low quality...  TODO: Fix?
455        season1 = coll.get_entries(quality="")
456        self.assertEqual(self.testSeason1, season1)
457       
458        # Find reversed
459        season1 = coll.get_entries(major=1, reverse=True)
460        rev = self.testSeason1[:]
461        rev.reverse()
462        self.assertEqual(rev, season1)
463
464    def testsetget(self):
465
466        coll = self.db.get_collection(id=self.coll_id)
467        self.assert_(coll, "Missing test collection")
468
469        self.assertEqual(coll.get_name(), "Testcollection")
470        self.assertEqual(coll.get_description(), "testDescription")
471
472        # Invalid
473        try:
474            coll.set_name(None)
475            self.fail("Allow name to be set to None")
476        except:
477            pass
478       
479        try:
480            coll.set_description(None)
481            self.fail("Allow description to be set to None")
482        except:
483            pass
484       
485        coll.set_name("newName")
486        self.assertEqual(coll.get_name(), "newName")
487       
488        coll.set_description("newDescription")
489        self.assertEqual(coll.get_name(), "newName")
490        self.assertEqual(coll.get_description(), "newDescription")
491       
492
493
494
495if __name__ == "__main__":
496
497    print "Testing the Collection classes"
498
499    unittest.main()
500
501    print "All done"