forked from prestidigitation/image_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
214 lines (144 loc) · 5.46 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
__author__ = "Antony Cherepanov"
import image_downloader
import os
import shutil
def test_is_image_valid_url():
print("test_is_image_valid_url():")
valid_url = "http://upload.wikimedia.org/wikipedia/commons/f/ff/Picea_likiangensis.jpg"
assert(image_downloader.is_image(valid_url))
def test_is_image_invalid_url():
print("test_is_image_invalid_url():")
invalid_url = "http://upload.wikimedia.org/wikipedia/commons/f/ff/Picea_likiangensis.txt"
assert(not image_downloader.is_image(invalid_url))
def test_check_dir_valid_path():
print("test_check_dir_valid_path():")
# Check with existent folder
path = get_path_to_folder_for_images()
assert(image_downloader.check_dir(path))
# Check with non existent folder
remove_folder(path)
assert(image_downloader.check_dir(path))
remove_folder(path)
def test_check_dir_invalid_path():
print("test_check_dir_invalid_path():")
fake_folder_path = "/fake/folder"
assert(not image_downloader.check_dir(fake_folder_path))
fake_folder = "../fake/folder"
assert(not image_downloader.check_dir(fake_folder))
def test_open_file_valid():
print("test_open_file_valid():")
file_path = create_empty_file()
assert(image_downloader.open_text_file(file_path))
remove_file(file_path)
def test_open_file_invalid():
print("test_open_file_invalid():")
fake_file = "/fake/file.txt"
assert(not image_downloader.open_text_file(fake_file))
def test_file_parsing():
print("test_file_parsing():")
file_path = create_file_with_urls()
file = open(file_path, 'r')
num_of_urls = sum(1 for line in file)
file.seek(0, 0)
valid_urls = image_downloader.file_parser(file)
file.close()
remove_file(file_path)
if len(valid_urls) != num_of_urls:
raise Exception("Fail. Not all URLs validated")
def test_img_downloader_valid_urls():
print("test_img_downloader_valid_urls():")
urls = get_test_urls()
folder_path = get_path_to_folder_for_images()
image_downloader.img_downloader(urls, folder_path)
files = os.listdir(folder_path)
remove_folder(folder_path)
if len(files) != len(urls):
raise Exception("Fail. Not all images was downloaded")
def test_img_downloader_invalid_urls():
print("test_img_downloader_invalid_urls():")
urls = get_invalid_urls()
folder_path = get_path_to_folder_for_images()
image_downloader.img_downloader(urls, folder_path)
files = os.listdir(folder_path)
remove_folder(folder_path)
if len(files) != 0:
raise Exception("Fail. Image downloaded by invalid link")
def test_start_download():
print("test_start_download():")
urls = get_test_urls()
folder_path = get_path_to_folder_for_images()
image_downloader.start_download(urls, folder_path)
files = os.listdir(folder_path)
remove_folder(folder_path)
if len(files) != len(urls):
raise Exception("Fail. Not all images was downloaded")
def test_start():
print("test_start():")
file_path = create_file_with_urls()
file = open(file_path, 'r')
num_of_urls = sum(1 for line in file)
file.close()
folder_path = get_path_to_folder_for_images()
image_downloader.start(file_path, folder_path)
files = os.listdir(folder_path)
remove_folder(folder_path)
remove_file(file_path)
if len(files) != num_of_urls:
raise Exception("Fail. Not all images was downloaded")
def create_empty_file():
path = get_test_file_path()
open(path, 'a').close()
return path
def get_test_file_path():
return os.getcwd() + "/test_file.txt"
def create_file_with_urls():
path = get_test_file_path()
urls = get_test_urls()
with open(path, 'a') as file:
for url in urls:
file.write(url + "\n")
file.close()
return path
def get_test_urls():
urls = list()
urls.append("http://upload.wikimedia.org/wikipedia/commons/f/ff/Picea_likiangensis.jpg")
urls.append("http://farm1.static.flickr.com/67/175175908_9d19e087c7.jpg")
urls.append("http://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png")
return urls
def get_invalid_urls():
urls = list()
urls.append("http://www.bountiful-farms.com/photos/plants/piceafastigiata.jpg")
return urls
def get_path_to_folder_for_images():
path = os.getcwd() + "/test_folder"
if not os.path.exists(path):
os.mkdir(path)
return path
def remove_file(t_path):
if os.path.exists(t_path):
try:
os.remove(t_path)
except Exception as err:
print("Error during file remove: {0}".format(err))
return
def remove_folder(t_path):
if os.path.exists(t_path):
try:
shutil.rmtree(t_path, True)
except Exception as err:
print("Error during folder remove: {0}".format(err))
return
if __name__ == '__main__':
print("Start tests")
test_is_image_valid_url()
test_is_image_invalid_url()
test_check_dir_valid_path()
test_check_dir_invalid_path()
test_open_file_valid()
test_open_file_invalid()
test_file_parsing()
test_img_downloader_valid_urls()
test_img_downloader_invalid_urls()
test_start_download()
test_start()
print("Tests passed!")