def test_calls_jpeg_when_args_length_bigger_than_3_crop_no_proportion(self): mox = Mox() mox.StubOutWithMock(controllers, 'jpeg') ret = 'should_be_a_pil_img' controllers.jpeg(path='crop/arg2/arg3/arg4').AndReturn(ret) mox.ReplayAll() got = self.handler('crop', 'arg2', 'arg3', 'arg4') mox.VerifyAll() msg = 'Expected "%s", got %r' % (ret, got) assert got == ret, msg
def test_calls_jpeg_when_args_length_bigger_than_3_no_crop(self): mox = Mox() mox.StubOutWithMock(controllers, 'jpeg') ret = 'should_be_a_pil_img' controllers.jpeg(path='arg1/arg2/arg3/arg4').AndReturn(ret) mox.ReplayAll() got = self.handler('arg1', 'arg2', 'arg3', 'arg4') mox.VerifyAll() msg = 'Expected "%s", got %r' % (ret, got) assert got == ret, msg
def test_calls_jpeg_when_args_length_equals_3(self): mox = Mox() mox.StubOutWithMock(controllers, 'jpeg') ret = 'should_be_a_pil_img' controllers.jpeg(path='arg1/arg2/arg3').AndReturn(ret) mox.ReplayAll() got = self.handler('arg1', 'arg2', 'arg3') mox.VerifyAll() msg = 'Expected "%s", got %r' % (ret, got) assert got == ret, msg
def test_caching_opens_if_does_not_exist(self): mox = Mox() old_jpeg = controllers.jpeg old_picture = controllers.picture controllers.jpeg = mox.CreateMockAnything() controllers.picture = mox.CreateMockAnything() cache_at = '/full/path/to/cache' class ImageHandlerStub(controllers.ImageHandler): fs = mox.CreateMockAnything() ImageHandlerStub.fs.exists(cache_at).AndReturn(True) controllers.jpeg(path='imgs/image.jpg').AndReturn('fake-img') ImageHandlerStub.fs.join(cache_at, 'imgs/image.jpg'). \ AndReturn('/should/be/cache/full/path.jpg') ImageHandlerStub.fs.exists('/should/be/cache/full/path.jpg'). \ AndReturn(False) ImageHandlerStub.fs.dirname('/should/be/cache/full/path.jpg'). \ AndReturn('dir-name') ImageHandlerStub.fs.mkdir('dir-name') file_mock = mox.CreateMockAnything() ImageHandlerStub.fs.open_raw('/should/be/cache/full/path.jpg', 'w'). \ AndReturn(file_mock) file_mock.write('fake-img') file_mock.close() mox.ReplayAll() try: img = ImageHandlerStub(cache_at) assert img.should_cache assert_equal(img.cache_path, cache_at) got = img('imgs', 'image.jpg') assert_equal(got, 'fake-img') mox.VerifyAll() finally: controllers.jpeg = old_jpeg controllers.picture = old_picture
def test_calls_jpeg_when_args_length_equals_3_and_crop(self): mox = Mox() mox.StubOutWithMock(controllers, 'jpeg') mox.StubOutWithMock(controllers, 'picture') ret = 'should_be_a_pil_img' controllers.jpeg(path='crop/200x100/image.jpg') controllers.picture(path='image.jpg', width=200, height=100). \ AndReturn(ret) mox.ReplayAll() got = self.handler('crop', '200x100', 'image.jpg') mox.VerifyAll() msg = 'Expected "%s", got %r' % (ret, got) assert got == ret, msg
def test_caching_return_if_already_exists(self): mox = Mox() old_jpeg = controllers.jpeg old_picture = controllers.picture old_static = controllers.static mox.StubOutWithMock(controllers, 'static') controllers.jpeg = mox.CreateMockAnything() controllers.picture = mox.CreateMockAnything() cache_at = '/full/path/to/cache' class ImageHandlerStub(controllers.ImageHandler): fs = mox.CreateMockAnything() ImageHandlerStub.fs.exists(cache_at).AndReturn(True) controllers.jpeg(path='imgs/image.jpg') ImageHandlerStub.fs.join(cache_at, 'imgs/image.jpg'). \ AndReturn('/should/be/cache/full/path.jpg') ImageHandlerStub.fs.exists('/should/be/cache/full/path.jpg'). \ AndReturn(True) controllers.static.serve_file('/should/be/cache/full/path.jpg', 'image/jpeg'). \ AndReturn('should-be-image-data') mox.ReplayAll() try: img = ImageHandlerStub(cache_at) assert img.should_cache assert_equal(img.cache_path, cache_at) got = img('imgs', 'image.jpg') assert_equal(got, 'should-be-image-data') mox.VerifyAll() finally: controllers.jpeg = old_jpeg controllers.picture = old_picture mox.UnsetStubs()