class ResizingImageFieldTestCase(TestCase): """ Test the behavior of the definition of our custom field in the context of a model instance. """ def setUp(self): super(ResizingImageFieldTestCase, self).setUp() self.model_instance = mock.Mock(attr='test-attr') self.field = ResizingImageField('testing/{attr}/path', TEST_SIZES) def test_generate_filename(self): """ Ensure that the path_template is used to generate filenames correctly. """ self.assertEqual( self.field.generate_filename(self.model_instance, 'test-filename'), 'testing/test-attr/path/test-filename' ) @mock.patch(PATCH_MODULE + '.validate_image_type') @mock.patch(PATCH_MODULE + '.validate_image_size') @ddt.data( (None, False), ('test-filename', False), ('test-filename', True), ) @ddt.unpack def test_pre_save(self, filename, is_existing_file, mock_validate_size, mock_validate_type): """ Ensure that image validation and resizing take place only when a new file is being stored. """ # pylint: disable=too-many-function-args field_value = ResizingImageFieldFile(self.model_instance, self.field, filename) self.model_instance.resized_image = field_value self.field.attname = 'resized_image' self.field.name = 'resized_image' with mock.patch(PATCH_MODULE + '.ResizingImageFieldFile.create_resized_copies') as mock_resize: # actual file data is needed for this test to work with make_uploaded_file('image/jpeg', (1000, 1000)) as image_file: if filename: field_value.file = image_file field_value._committed = is_existing_file # pylint: disable=protected-access self.field.pre_save(self.model_instance, False) expected_called = bool(filename) and not is_existing_file for actual_called in (mock_validate_size.called, mock_validate_type.called, mock_resize.called): self.assertEqual(actual_called, expected_called) def test_upload_to(self): """ Ensure that the field cannot be initialized with a callable `upload_to` as this will break the filename-generation template logic. """ def dummy_upload_to(instance, filename): # pylint: disable=missing-docstring, unused-argument return 'foo' with self.assertRaises(Exception) as exc_context: self.field = ResizingImageField('testing/{attr}/path', TEST_SIZES, upload_to=dummy_upload_to) self.assertEquals( exc_context.exception.message, 'ResizingImageField does not support passing a custom callable for the `upload_to` keyword arg.', )
class ResizingImageFieldTestCase(TestCase): """ Test the behavior of the definition of our custom field in the context of a model instance. """ def setUp(self): super(ResizingImageFieldTestCase, self).setUp() self.model_instance = mock.Mock(attr='test-attr') self.field = ResizingImageField('testing/{attr}/path', TEST_SIZES) def test_generate_filename(self): """ Ensure that the path_template is used to generate filenames correctly. """ self.assertEqual( self.field.generate_filename(self.model_instance, 'test-filename'), 'testing/test-attr/path/test-filename') @mock.patch(PATCH_MODULE + '.validate_image_type') @mock.patch(PATCH_MODULE + '.validate_image_size') @ddt.data( (None, False), ('test-filename', False), ('test-filename', True), ) @ddt.unpack def test_pre_save(self, filename, is_existing_file, mock_validate_size, mock_validate_type): """ Ensure that image validation and resizing take place only when a new file is being stored. """ # pylint: disable=too-many-function-args field_value = ResizingImageFieldFile(self.model_instance, self.field, filename) self.model_instance.resized_image = field_value self.field.attname = 'resized_image' self.field.name = 'resized_image' with mock.patch(PATCH_MODULE + '.ResizingImageFieldFile.create_resized_copies' ) as mock_resize: # actual file data is needed for this test to work with make_uploaded_file('image/jpeg', (1000, 1000)) as image_file: if filename: field_value.file = image_file field_value._committed = is_existing_file # pylint: disable=protected-access self.field.pre_save(self.model_instance, False) expected_called = bool(filename) and not is_existing_file for actual_called in (mock_validate_size.called, mock_validate_type.called, mock_resize.called): self.assertEqual(actual_called, expected_called) def test_upload_to(self): """ Ensure that the field cannot be initialized with a callable `upload_to` as this will break the filename-generation template logic. """ def dummy_upload_to(instance, filename): # pylint: disable=missing-docstring, unused-argument return 'foo' with self.assertRaises(Exception) as exc_context: self.field = ResizingImageField('testing/{attr}/path', TEST_SIZES, upload_to=dummy_upload_to) self.assertEquals( exc_context.exception.message, 'ResizingImageField does not support passing a custom callable for the `upload_to` keyword arg.', )