Skip to content

Commit

Permalink
fix height to string, remove favicon png, add tests, make app importable
Browse files Browse the repository at this point in the history
  • Loading branch information
jinpark committed Mar 28, 2015
1 parent fe599da commit 0fdbfbc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
Empty file added __init__.py
Empty file.
6 changes: 4 additions & 2 deletions app.py
Expand Up @@ -39,6 +39,9 @@ def home():

@app.route('/favicon.ico/')
def favicon():
"""
I hate favicons. ugh
"""
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/png')

Expand Down Expand Up @@ -70,7 +73,6 @@ def convert(url):
try:
resize_height = int(query_string['rheight'])
except:

bad_request('rheight')
else:
resize_height = None
Expand All @@ -79,7 +81,7 @@ def convert(url):
if resize_width and not resize_height:
img.transform(resize=str(resize_width))
if resize_height and not resize_width:
img.transform(resize='x' + resize_height)
img.transform(resize='x' + str(resize_height))

temp_file = NamedTemporaryFile(mode='w+b',suffix=img.format)
img.save(file=temp_file)
Expand Down
Binary file removed static/favicon.png
Binary file not shown.
Binary file added tests/images/50x200.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/50x200_pre.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions tests/test_app.py
@@ -0,0 +1,75 @@
import os
import sys
import unittest
import requests
from StringIO import StringIO
from wand.image import Image
from mock import MagicMock

topdir = os.path.join(os.path.dirname(__file__), "..")
sys.path.append(topdir)
import app


def get_stub(*args, **kwargs):
response = requests.get.return_value
with open('tests/images/50x200_pre.jpeg', 'r') as f:
response.content = f.read()
response.headers = {'content-type': 'image/png'}
return response

requests.get = MagicMock(side_effect=get_stub)


class TestImageResizer(unittest.TestCase):
def setUp(self):
self.app = app.app.test_client()

def test_entry(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/', follow_redirects=True)
rv_image_string = rv.data
self.assertEqual(rv.data, expected_image)

def test_resize_width(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 400)

def test_resize_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 25)
self.assertEqual(img.height, 100)

def test_resize_width_and_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100&rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 100)

def test_resize_width_and_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100&rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 100)

def test_convert_type(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?type=png', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.mimetype, "image/png")

if __name__ == '__main__':
unittest.main()

0 comments on commit 0fdbfbc

Please sign in to comment.