This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
test_unit.py
105 lines (78 loc) · 3.24 KB
/
test_unit.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
# Python library imports
import os, io
from unittest.mock import patch
from unittest import TestCase
# Dependencies imports
from requests.exceptions import MissingSchema
from PIL import Image
# Local project imports
import make_art as ma
# Helper functions
def create_pillow_object():
"""Creates and returns a temporary Pillow object"""
return Image.new('RGB', (100, 100), color = 'red')
# Test cases
class TestFilePath(TestCase):
"""Test if file paths"""
TEST_PATH = 'test_file'
@classmethod
def teardown_class(cls):
"""Remove temporary file"""
os.remove(cls.TEST_PATH)
def test_image_does_not_exists(self):
"""Test if a failed image path throws an error"""
# By default get_image_conversion throws a MissingSchema
# exception when the image path and/or url does not exists
with self.assertRaises(MissingSchema):
ma.get_image_conversion(None, 1, False, "")
def test_saved_text_to_file(self):
"""Test if the ascii convertion was saved into a file"""
img = create_pillow_object()
ascii_string = ma.convert_image_to_ascii(img, 1)
ma.save_text_to_file(ascii_string, self.TEST_PATH)
self.assertTrue(os.path.exists(self.TEST_PATH))
class TestPillowObject(TestCase):
"""Test if pillow objects are returned"""
def create_pillow_object(self):
"""Creates and returns a temporary Pillow object"""
return Image.new('RGB', (100, 100), color = 'red')
def test_scale_image(self):
"""Test if a pillow object is returned"""
img = create_pillow_object()
test_img = ma.scale_image(img, 1)
self.assertEqual(type(img), type(test_img))
def test_convert_to_grayscale(self):
"""Test if a pillow object is returned"""
img = create_pillow_object()
test_img = ma.convert_to_grayscale(img)
self.assertEqual(type(img), type(test_img))
class TestConvertions(TestCase):
"""Test cases for image convertion to strings"""
TEST_PATH = 'test_img.png'
@classmethod
def setup_class(cls):
"""Create temporary file"""
img = create_pillow_object()
img.save(cls.TEST_PATH)
@classmethod
def teardown_class(cls):
"""Remove temporary file"""
os.remove(cls.TEST_PATH)
def test_image_conversion(self):
"""Test the image path exists and is converted to ascii"""
# If the image exists, get_image_conversion returns an string
converted_img = ma.get_image_conversion(self.TEST_PATH, 1, False, "")
self.assertEqual(type(converted_img), type('string'))
def test_image_to_ascii(self):
"""Test the image is converted to ascii based string"""
img = create_pillow_object()
ascii_string = ma.convert_image_to_ascii(img, 1)
self.assertEqual(type(ascii_string), type('string'))
def test_image_to_ascii_chars(self):
"""Test the image is converted to ascii character string"""
img = create_pillow_object()
new_width = int(100)
img = ma.scale_image(img, 1)
img = ma.convert_to_grayscale(img)
ascii_string = ma.map_pixels_to_ascii_chars(img)
self.assertEqual(type(ascii_string), type('string'))