1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
import io |
4
|
|
|
import os |
5
|
|
|
import shutil |
6
|
|
|
import unittest |
7
|
|
|
from uuid import uuid4 |
8
|
|
|
|
9
|
|
|
from PIL import Image |
10
|
|
|
|
11
|
|
|
from saucenao import SauceNao |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class TestSauceNao(unittest.TestCase): |
15
|
|
|
SAUCENAO_MIN_WIDTH = 3 |
16
|
|
|
SAUCENAO_MIN_HEIGHT = 3 |
17
|
|
|
|
18
|
|
|
def setUp(self): |
19
|
|
|
"""Constructor for unittest classes |
20
|
|
|
|
21
|
|
|
:return: |
22
|
|
|
""" |
23
|
|
|
directory = str(uuid4()) |
24
|
|
|
os.mkdir(directory) |
25
|
|
|
self.directory = os.path.abspath(directory) |
26
|
|
|
self.saucenao = SauceNao() |
27
|
|
|
|
28
|
|
|
def tearDown(self): |
29
|
|
|
"""Destructor for unittest classes |
30
|
|
|
|
31
|
|
|
:return: |
32
|
|
|
""" |
33
|
|
|
shutil.rmtree(self.directory) |
34
|
|
|
|
35
|
|
|
def generate_small_jpg(self): |
36
|
|
|
"""Generate a rather small jpg file to upload faster(631 bytes) |
37
|
|
|
|
38
|
|
|
:return: |
39
|
|
|
""" |
40
|
|
|
file_path = os.path.join(self.directory, str(uuid4()) + ".jpg") |
41
|
|
|
im = Image.new("RGB", (self.SAUCENAO_MIN_WIDTH, self.SAUCENAO_MIN_HEIGHT)) |
42
|
|
|
im.save(file_path, "JPEG") |
43
|
|
|
return file_path |
44
|
|
|
|
45
|
|
|
def test_check_file_object(self): |
46
|
|
|
"""Test the run_application function |
47
|
|
|
|
48
|
|
|
:return: |
49
|
|
|
""" |
50
|
|
|
# check returned object from file open |
51
|
|
|
with open(self.generate_small_jpg(), "rb") as test_file: |
52
|
|
|
results = self.saucenao.check_file_object(test_file) |
53
|
|
|
self.assertIsInstance(results, list) |
54
|
|
|
print(results) |
55
|
|
|
|
56
|
|
|
# check io.BytesIO object |
57
|
|
|
results = self.saucenao.check_file_object( |
58
|
|
|
io.BytesIO( |
59
|
|
|
b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A\x00\x00\x00\x0D\x49\x48\x44\x52\x00\x00\x01\x00\x00\x00\x01\x00" |
60
|
|
|
b"\x01\x03\x00\x00\x00\x66\xBC\x3A\x25\x00\x00\x00\x03\x50\x4C\x54\x45\xB5\xD0\xD0\x63\x04\x16\xEA" |
61
|
|
|
b"\x00\x00\x00\x1F\x49\x44\x41\x54\x68\x81\xED\xC1\x01\x0D\x00\x00\x00\xC2\xA0\xF7\x4F\x6D\x0E\x37" |
62
|
|
|
b"\xA0\x00\x00\x00\x00\x00\x00\x00\x00\xBE\x0D\x21\x00\x00\x01\x9A\x60\xE1\xD5\x00\x00\x00\x00\x49" |
63
|
|
|
b"\x45\x4E\x44\xAE\x42\x60\x82" |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
self.assertIsInstance(results, list) |
67
|
|
|
print(results) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
if __name__ == '__main__': |
71
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestSauceNao) |
72
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite) |
73
|
|
|
|