import json
from unittest import TestCase

import requests
from PIL import Image
from httmock import urlmatch, HTTMock
from redis import Redis

from app import app

import os

@urlmatch(netloc=r'(.*\.)?test.server\.com$')
def get_image_mock(url, request):
    return open('tests/test_resources/heman.png', 'r').read()


class SuccessImageResizeTest(TestCase):
    def setUp(self):
        self.r = Redis()
        self.app = app.test_client()
        self.base_url = '/v1/resizer/{}'
        self.test_image_name = 'test_image.jpg'

    def tearDown(self):
        self.r.flushall()
        try:
            os.remove(self.test_image_name)
        except OSError:
            pass

class TestResizeImage(SuccessImageResizeTest):
    def make_request(self, width, height):
        with HTTMock(get_image_mock):
            response = self.app.get(
                self.base_url.format(
                    '?width={}&height={}&file={}'.format(
                        0, 100, 'http://test.server.com/heman.png'
                    )
                )
            )
            image = self.r.get('heman.png_0_100')

            with open(self.test_image_name, 'w') as f:
                f.write(image)

            width, height = Image.open(self.test_image_name).size

            assert response.status_code == 200
            assert 'image/jpeg' in response.content_type

            assert 'heman.png_0_100' in self.r.keys('heman.png_0_100')
            assert image is not None

            return width, height

    def test_height(self):
        width, height = self.make_request(0, 100)
        assert height is not None
        assert width is not None

    def test_width(self):
        width, height = self.make_request(100, 0)
        assert width is not None
        assert height is not None

    def test_bad_argument(self):
        with HTTMock(get_image_mock):
            response = self.app.get(
                self.base_url.format('?file=http://test.server.com/heman.png')
            )
            assert response.status_code == 400

@@ -319,23 +319,16 @@
 %0A%0Aclass 
-Success
 ImageRes
@@ -429,16 +429,64 @@
 lient()%0A
+        self.test_image_name = 'test_image.jpg'%0A
         
@@ -527,35 +527,34 @@
        self.
-tes
+ge
 t_image_
 name = 'test
@@ -541,39 +541,303 @@
 t_image_
-name = 'test_image.jp
+url = self.base_url.format(%0A                    '?width=%7B%7D&height=%7B%7D&file=%7B%7D'.format(%0A                        0, 100, 'http://test.server.com/heman.png'%0A                    )%0A                )%0A        self.bad_argument_url = self.base_url.format('?file=http://test.server.com/heman.pn
 g'
+)
 %0A%0A    de
@@ -873,17 +873,32 @@
 f.r.
-flushall(
+delete('heman.png_0_100'
 )%0A  
@@ -1020,15 +1020,8 @@
 age(
-Success
 Imag
@@ -1155,224 +1155,26 @@
 get(
-%0A                self.base_url.format(%0A                    '?width=%7B%7D&height=%7B%7D&file=%7B%7D'.format(%0A                        0, 100, 'http://test.server.com/heman.png'%0A                    )%0A                )%0A            
+self.get_image_url
 )%0A  
@@ -1624,16 +1624,308 @@
 height%0A%0A
+    def test_file_exists_already(self):%0A        with HTTMock(get_image_mock):%0A            response = self.app.get(self.get_image_url)%0A            with HTTMock(get_image_mock):%0A                response_2 = self.app.get(self.get_image_url)%0A                assert response_2.status_code == 200%0A%0A
     def 
@@ -2316,106 +2316,240 @@
 get(
-%0A                self.base_url.format('?file=http://test.server.com/heman.png')%0A            )%0A    
+self.bad_argument_url)%0A            assert response.status_code == 400%0A%0Aclass TestUtils(TestCase):%0A    def setUp(self):%0A        self.app = app.test_client()%0A%0A    def test_ping(self):%0A        response = self.app.get('/v1/utils/ping')%0A
     
@@ -2567,28 +2567,73 @@
 response.status_code == 
-4
+2
 00%0A
+        assert response.get_data() == 'pong'%0A

Make tests more readable