-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
executable file
·115 lines (90 loc) · 4.43 KB
/
tests.py
File metadata and controls
executable file
·115 lines (90 loc) · 4.43 KB
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
#!flask/bin/python
"""Image Resizer application unit tests."""
__author__ = "Carlos Gomez (carlosg24@gmail.com)"
import cStringIO
import app
import image_resizer
import unittest
from PIL import Image
class FlaskAppTests(unittest.TestCase):
def setUp(self):
app.app.config['TESTING'] = True
self.app = app.app.test_client()
def testResizeRouteNoURL(self):
response = self.app.get('/resize')
self.assertEqual(response.data, app.NO_URL_ERROR_MESSAGE)
def testResizeRouteNoWidthOrHeight(self):
response = self.app.get('/resize', data=dict(
url='http://localhost/image.jpg'))
self.assertEqual(response.data, app.NO_URL_ERROR_MESSAGE)
class ImageResizerTests(unittest.TestCase):
def setUp(self):
self.test_image_resizer = image_resizer.ImageResizer()
color = (0, 0, 255, 0)
size = (400, 200)
self.test_landscape_image = Image.new("RGBA", size, color)
size = (200, 400)
self.test_portrait_image = Image.new("RGBA", size, color)
def testResizeImageFromURL(self):
"""Tests the resize_image_from_url method."""
test_file = cStringIO.StringIO()
self.test_landscape_image.save(test_file, format='jpeg')
test_file.seek(0)
real_StringIO = cStringIO.StringIO
# Quick monkey-patch to mock out StringIO creation when an argument
# is passed to constructor. Would use mocking library if needed
# more mocking than this.
cStringIO.StringIO = (lambda x=False:
test_file if x else real_StringIO())
file, format = self.test_image_resizer.resize_image_from_url(
'http://127.0.0.1/image.jpeg', 0, 0, False)
cStringIO.StringIO = real_StringIO
self.assertTrue(file)
self.assertEqual('JPEG', format)
def testResizeImageDontKeepAspect(self):
"""Tests the resize_image method with keep_aspect_ratio=False."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 100, 150, False)
self.assertEqual((100, 150), result_image.size)
def testResizeImageNoWidth(self):
"""Tests resize_image method with width set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 0, 150, False)
self.assertEqual((400, 150), result_image.size)
def testResizeImageNoHeight(self):
"""Tests resize_image method with height set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 100, 0, False)
self.assertEqual((100, 200), result_image.size)
def testResizeLandscapeImageKeepAspect(self):
"""Tests a landscape image aspect ratio resize."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 200, 150, True)
self.assertEqual((200, 100), result_image.size)
def testResizeLandscapeImageKeepAspectNoWidth(self):
"""Tests a landscape image aspect ratio resize with width set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 0, 150, True)
self.assertEqual((300, 150), result_image.size)
def testResizeLandscapeImageKeepAspectNoHeight(self):
"""Tests a landscape image aspect ratio resize with height set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_landscape_image, 300, 0, True)
self.assertEqual((300, 150), result_image.size)
def testResizePortraitImageKeepAspect(self):
"""Tests a portrait image aspect ratio resize."""
result_image = self.test_image_resizer.resize_image(
self.test_portrait_image, 200, 150, True)
self.assertEqual((75, 150), result_image.size)
def testResizePortraitImageKeepAspectNoWidth(self):
"""Tests a portrait image aspect ratio resize with width set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_portrait_image, 0, 150, True)
self.assertEqual((75, 150), result_image.size)
def testResizePortraitImageKeepAspectNoHeight(self):
"""Tests a portrait image aspect ratio resize with height set to 0."""
result_image = self.test_image_resizer.resize_image(
self.test_portrait_image, 300, 0, True)
self.assertEqual((300, 600), result_image.size)
if __name__ == '__main__':
unittest.main()