1
|
|
|
import unittest |
|
|
|
|
2
|
|
|
import os |
3
|
|
|
from faker import Faker |
4
|
|
|
from pyrandimg.randimg_provider import RandImgProvider |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class TestRandImgProvider(unittest.TestCase): |
|
|
|
|
8
|
|
|
def setUp(self): |
9
|
|
|
""" Initialize each unit test """ |
10
|
|
|
self.faker = Faker() |
11
|
|
|
self.faker.add_provider(RandImgProvider) |
12
|
|
|
|
13
|
|
|
def test_generate_default_image_url(self): |
|
|
|
|
14
|
|
|
self.assertRegex(self.faker.image_url(), r'^(https?\:\/\/)?www\.rand\-img\.com\/720\/480$') |
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_generate_image_url_with_custom_width(self): |
|
|
|
|
17
|
|
|
self.assertRegex(self.faker.image_url(200), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/480$') |
|
|
|
|
18
|
|
|
self.assertRegex(self.faker.image_url(980), r'^(https?\:\/\/)?www\.rand\-img\.com\/980\/480$') |
|
|
|
|
19
|
|
|
|
20
|
|
|
def test_generate_image_url_with_custom_width_and_height(self): |
|
|
|
|
21
|
|
|
self.assertRegex(self.faker.image_url(200, 500), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/500$') |
|
|
|
|
22
|
|
|
self.assertRegex(self.faker.image_url(320, 280), r'^(https?\:\/\/)?www\.rand\-img\.com\/320\/280$') |
|
|
|
|
23
|
|
|
self.assertRegex(self.faker.image_url(1920, 1080), r'(https?\:\/\/)?www\.rand\-img\.com\/1920\/1080') |
|
|
|
|
24
|
|
|
|
25
|
|
|
def test_generate_image_url_with_category(self): |
|
|
|
|
26
|
|
|
self.assertRegex(self.faker.image_url(200, 500, 'food'), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/500\/food$') |
|
|
|
|
27
|
|
|
self.assertRegex(self.faker.image_url(500, 653, 'sky'), r'^(https?\:\/\/)?www\.rand\-img\.com\/500\/653\/sky$') |
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_generate_rand_image_url(self): |
|
|
|
|
30
|
|
|
self.assertRegex( |
31
|
|
|
self.faker.squared_image_url(320, rand=True), |
|
|
|
|
32
|
|
|
r'^(https?\:\/\/)?www\.rand\-img\.com\/320\/320\?rand=\d+$' |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
def test_generate_default_squared_image_url(self): |
|
|
|
|
36
|
|
|
self.assertRegex(self.faker.squared_image_url(200), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/200$') |
|
|
|
|
37
|
|
|
|
38
|
|
|
def test_generate_squared_image_url_with_category(self): |
|
|
|
|
39
|
|
|
self.assertRegex( |
40
|
|
|
self.faker.squared_image_url(200, 'food'), |
|
|
|
|
41
|
|
|
r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/200\/food$' |
42
|
|
|
) |
43
|
|
|
self.assertRegex( |
44
|
|
|
self.faker.squared_image_url(1024, 'sky'), |
|
|
|
|
45
|
|
|
r'^(https?\:\/\/)?www\.rand\-img\.com\/1024\/1024\/sky$' |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
def test_image_url_with_parameters(self): |
|
|
|
|
49
|
|
|
self.assertRegex( |
50
|
|
|
self.faker.image_url(720, 480, 'food', rand=True, blur=4, gray=1), |
|
|
|
|
51
|
|
|
r'https?:\/\/www\.rand\-img\.com\/720\/480/food\?([(\&?rand\=\d+)(\&?blur\=4)(\&?gray\=1)])' |
|
|
|
|
52
|
|
|
) |
53
|
|
|
self.assertRegex( |
54
|
|
|
self.faker.image_url(250, 120, 'fashion', rand=False, blur=4, gray=1), |
|
|
|
|
55
|
|
|
r'https?:\/\/www\.rand\-img\.com\/250\/120/fashion\?([(\&?blur\=4)(\&?gray\=1)])' |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
def test_get_gif(self): |
|
|
|
|
59
|
|
|
self.assertRegex(self.faker.gif_url(), r'^(https?\:\/\/)?www\.rand\-img\.com\/gif$') |
|
|
|
|
60
|
|
|
|
61
|
|
|
def test_get_gif_with_rand(self): |
|
|
|
|
62
|
|
|
self.assertRegex(self.faker.gif_url(True), r'^(https?\:\/\/)?www\.rand\-img\.com\/gif\?rand\=\d+$') |
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_download_gif(self): |
|
|
|
|
65
|
|
|
gif = self.faker.gif() |
|
|
|
|
66
|
|
|
gif_dirname = os.path.dirname(gif) |
|
|
|
|
67
|
|
|
_, file_ext = os.path.splitext(gif) |
68
|
|
|
|
69
|
|
|
self.assertEqual('.gif', file_ext) |
70
|
|
|
self.assertTrue(os.path.exists(gif)) |
71
|
|
|
os.remove(gif) |
72
|
|
|
|
73
|
|
|
def test_download_gif_with_dirname(self): |
|
|
|
|
74
|
|
|
os.mkdir('output') |
75
|
|
|
output_dir = os.path.join(os.getcwd(), 'output') |
76
|
|
|
gif = self.faker.gif(dir=output_dir) |
|
|
|
|
77
|
|
|
gif_dirname = os.path.dirname(gif) |
78
|
|
|
_, file_ext = os.path.splitext(gif) |
79
|
|
|
|
80
|
|
|
self.assertEqual(output_dir, gif_dirname) |
81
|
|
|
self.assertEqual('.gif', file_ext) |
82
|
|
|
self.assertTrue(os.path.exists(gif)) |
83
|
|
|
os.remove(gif) |
84
|
|
|
os.rmdir(output_dir) |
85
|
|
|
|
86
|
|
|
def test_download_gif_with_invalid_argument_exception(self): |
|
|
|
|
87
|
|
|
pass |
88
|
|
|
|
89
|
|
|
def test_download_image(self): |
|
|
|
|
90
|
|
|
gif = self.faker.gif() |
|
|
|
|
91
|
|
|
gif_dirname = os.path.dirname(gif) |
|
|
|
|
92
|
|
|
_, file_ext = os.path.splitext(gif) |
93
|
|
|
|
94
|
|
|
self.assertEqual('.gif', file_ext) |
95
|
|
|
self.assertTrue(os.path.exists(gif)) |
96
|
|
|
os.remove(gif) |
97
|
|
|
|
98
|
|
View Code Duplication |
def test_download_image_with_dirname(self): |
|
|
|
|
99
|
|
|
os.mkdir('output') |
100
|
|
|
output_dir = os.path.join(os.getcwd(), 'output') |
101
|
|
|
image = self.faker.image(dir=output_dir) |
|
|
|
|
102
|
|
|
image_dirname = os.path.dirname(image) |
103
|
|
|
_, file_ext = os.path.splitext(image) |
104
|
|
|
|
105
|
|
|
self.assertEqual(output_dir, image_dirname) |
106
|
|
|
self.assertEqual('.jpg', file_ext) |
107
|
|
|
self.assertTrue(os.path.exists(image)) |
108
|
|
|
os.remove(image) |
109
|
|
|
os.rmdir(output_dir) |
110
|
|
|
|
111
|
|
View Code Duplication |
def test_download_image_with_dirname_and_filename(self): |
|
|
|
|
112
|
|
|
os.mkdir('output') |
113
|
|
|
output_dir = os.path.join(os.getcwd(), 'output') |
114
|
|
|
image = self.faker.image(dir=output_dir, filename='testing.png') |
|
|
|
|
115
|
|
|
image_dirname = os.path.dirname(image) |
116
|
|
|
_, file_ext = os.path.splitext(image) |
117
|
|
|
|
118
|
|
|
self.assertEqual(output_dir, image_dirname) |
119
|
|
|
self.assertEqual('.png', file_ext) |
120
|
|
|
self.assertTrue(os.path.exists(image)) |
121
|
|
|
os.remove(image) |
122
|
|
|
os.rmdir(output_dir) |
123
|
|
|
|
124
|
|
|
def test_download_image_with_exception(self): |
|
|
|
|
125
|
|
|
pass |
126
|
|
|
|
127
|
|
|
def test_download_image_with_invalid_argument_exception(self): |
|
|
|
|
128
|
|
|
pass |
129
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.