-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
96 lines (69 loc) · 3.01 KB
/
tests.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
import os, math
from app import app
from unittest import TestCase
from converter import get_image_pixels, get_image_reduction_factors, reduce_image_size
from PIL import Image
def get_image_dimensions(image_path):
"""Gets the width and height of an image and
returns them as a new dict"""
img = Image.open(image_path)
width, height = img.size
img.close()
return {"width": width, "height": height}
#################################################
# Tests
#################################################
class AuthTests(TestCase):
def setUp(self):
"""Executed prior to each test."""
app.config["TESTING"] = True
app.config["DEBUG"] = False
def test_get_image_pixels(self):
"""Tests the get_image_pixels method"""
testImage = "./test_images/converter-test-2.jpg"
image_dimensions = get_image_dimensions(testImage)
pixels = get_image_pixels(testImage)
# Test Width and Height
self.assertEqual(len(pixels[0]), image_dimensions.get("width"))
self.assertEqual(len(pixels), image_dimensions.get("height"))
def test_get_image_reduction_factors(self):
"""Tests the get_image_reduction_factors method"""
testImage = "./test_images/converter-test-4.jpg"
image_dimensions = get_image_dimensions(testImage)
maxwidth = 200
maxheight = 100
pixels = get_image_pixels(testImage)
reduction_factors = get_image_reduction_factors(pixels, maxwidth, maxheight)
actualWidth = image_dimensions.get("width") / reduction_factors[0]
differenceFromWidthMax = abs(maxwidth - actualWidth)
actualHeight = image_dimensions.get("height") / reduction_factors[1]
differenceFromHeightMax = abs(maxheight - actualHeight)
# Test width and height should not be more than
# (reduction factor) different than max width/height
self.assertLess(
differenceFromWidthMax,
reduction_factors[0],
)
self.assertLess(
differenceFromHeightMax,
reduction_factors[1],
)
def test_reduce_image_size(self):
testImage = "./test_images/converter-test-4.jpg"
image_dimensions = get_image_dimensions(testImage)
maxwidth = 200
maxheight = 100
pixels = get_image_pixels(testImage)
reduction_factors = get_image_reduction_factors(pixels, maxwidth, maxheight)
reduced_pixels = reduce_image_size(
pixels, reduction_factors[0], reduction_factors[1]
)
expectedWidth = image_dimensions.get("width") / reduction_factors[0]
realWidth = len(reduced_pixels[0])
widthdiff = abs(expectedWidth - realWidth)
expectedHeight = image_dimensions.get("height") / reduction_factors[1]
realHeight = len(reduced_pixels)
heightdiff = abs(expectedHeight - realHeight)
# difference between real and expected size should be less than 1
self.assertLess(widthdiff, 1)
self.assertLess(heightdiff, 1)