def test_save__save_successful(self): test_image = create_test_image() with patch('models.boto3.client') as MockClient: MockClient.return_value = BotocoreClientMock() # Just check if it runs. image = Image(test_image.read(), 'image.png') image.save()
def test_save__unable_to_upload_to_server(self): test_image = create_test_image() with patch('models.boto3.client') as MockClient: MockClient.return_value = BotocoreClientMockError('upload_fileobj') with self.assertRaises(ImageUploadException): image = Image(test_image.read(), 'image.png') image.save()
def test_get_image__cache(self): test_image = create_test_image() cache = LocalMemoryCache() cache.add('image.png', test_image) image_binary = get_image(self.image_name) self.assertEqual(test_image, image_binary) cache.delete(self.image_name) # Clean up
def test_upload__image_not_valid(self): test_image = create_test_image(width=30000) responses.add(responses.GET, 'http://www.google.com/image.png', body=test_image.read(), status=200) response = self.client.post( '/image/upload', data=json.dumps({'image_url': 'http://www.google.com/image.png'}), content_type='application/json', ) self.assertEqual(400, response.status_code) expected_response_data = { 'message': 'Image validation error: Make sure image width is between 1 and 3000', 'data': {}, 'code': 400, } self.assertEqual(expected_response_data, json.loads(response.data))
def test_upload__success(self): test_image = create_test_image() responses.add(responses.GET, 'http://www.google.com/image.png', body=test_image.read(), status=200) response = self.client.post( '/image/upload', data=json.dumps({'image_url': 'http://www.google.com/image.png'}), content_type='application/json', ) self.assertEqual(201, response.status_code) expected_response_data = { 'message': 'Image uploaded successfully', 'data': { 'image_url': 'http://localhost/image/view/image.png' }, 'code': 201, } self.assertEqual(expected_response_data, json.loads(response.data))
def test_upload__image_upload_error(self): test_image = create_test_image() responses.add(responses.GET, 'http://www.google.com/image.png', body=test_image.read(), status=200) with patch('models.boto3.client') as MockClient: MockClient.return_value = BotocoreClientMockError('upload_fileobj') response = self.client.post( '/image/upload', data=json.dumps( {'image_url': 'http://www.google.com/image.png'}), content_type='application/json', ) self.assertEqual(500, response.status_code) expected_response_data = { 'message': 'Unable to upload image to server', 'data': {}, 'code': 500 } self.assertEqual(expected_response_data, json.loads(response.data))
def test_validate__valid_image(self): test_image = create_test_image(1280, 800) self.assertTrue(self.validator.validate(test_image)) self.assertEqual('', self.validator.error)
def test_validate__image_file__invalid_height(self): test_image = create_test_image(1280, 4000) self.assertFalse(self.validator.validate(test_image)) self.assertEqual( f'Make sure image height is between 1 and {ImageValidator.VALID_MAX_HEIGHT}', self.validator.error)
def test_validate__image_file__invalid_width(self): test_image = create_test_image(4000, 4000) self.assertFalse(self.validator.validate(test_image)) self.assertEqual( f'Make sure image width is between 1 and {ImageValidator.VALID_MAX_WIDTH}', self.validator.error)
def test_save__invalid_image(self): test_image = create_test_image(4000, 4000) with self.assertRaises(ImageNotValidException): image = Image(test_image.read(), 'image.png') image.save()