class TempImageTest(TestCase): def setUp(self): self.image = SimpleUploadedFile( 'test.png', open( os.path.join( os.path.dirname(__file__), 'testing/test.png')).read()) self.temp_image = TempImage( original=self.image, content_type=self.image.content_type) self.temp_image.save() def tearDown(self): self.temp_image.delete() def test_absolute_url(self): self.assertEquals( '%s%s' % ( settings.MEDIA_URL, self.temp_image.original.name), self.temp_image.get_absolute_url()) def test_uuid(self): self.assertEquals( len(self.temp_image.uuid), 32) def test_filename(self): self.assertEquals( self.temp_image.original.name, '%s%s_%s' % ( UPLOAD_PATH, self.temp_image.uuid.hex, self.image.name))
def setUp(self): self.image = SimpleUploadedFile( 'test.png', open( os.path.join( os.path.dirname(__file__), 'testing/test.png')).read()) self.temp_image = TempImage( original=self.image, content_type=self.image.content_type) self.temp_image.save()
class FileUploadsImageWidgetTest(TestCase): widget_name = 'test-widget' widget_id = 'id_%s' % widget_name expected = u'<div class="image">' \ + '<a class="add" data-target="#modal-upload" href="#" ' \ + 'data-source="%(uploader_url)s" data-toggle="modal" ' \ + 'data-upload="%(uploader_upload_url)s" >' \ + '<img src="%(img_src)s" /></a>' \ + '<input type="hidden" id="%(widget_id)s" name="%(widget_id)s" ' \ + 'value="%(value)s" /></div>' \ + '<noscript><input type="file" name="%(widget_name)s" ' \ + 'id="%(widget_id)s" /></noscript>' defaults = { 'uploader_upload_url': reverse('file-uploader-upload'), 'uploader_url': reverse('file-uploader'), 'widget_name': widget_name, 'widget_id': widget_id} def setUp(self): self.image_path = ( os.path.join( os.path.dirname(__file__), 'testing/test.png')) self.image = SimpleUploadedFile( 'test.png', open(self.image_path).read(), 'image/png') self.temp_image = TempImage( original=self.image, content_type=self.image.content_type) self.temp_image.save() self.widget = FileUploadsImageWidget() def tearDown(self): self.temp_image.delete() def test_value_from_datadict(self): """ when the form is submitted the submitted filename should be transformed into the associated temp image """ data = {'id_%s' % self.widget_name: self.temp_image.original.name} files = [] result = self.widget.value_from_datadict(data, files, self.widget_name) self.assertEquals(result.size, self.image.size) self.assertEquals(result.content_type, self.image.content_type) def test_render_empty(self): """ this test renders the widget without any associated images temporary or otherwise """ value = '' result = self.widget.render( self.widget_name, value, {'id': self.widget_id}) values = self.defaults.copy() values.update( {'value': '/static/file_uploads/images/question.small.gif', 'img_src': '/static/file_uploads/images/question.small.gif'}) self.assertEquals(result, self.expected % values) def test_render_uploaded(self): """ this test simulates the situation where the form is re-rendered, perhaps due to error and the widget must show the *temp* image that has already been uploaded """ data = {'id_%s' % self.widget_name: self.temp_image.original.name} files = [] value = self.widget.value_from_datadict(data, files, self.widget_name) result = self.widget.render( self.widget_name, value, {'id': self.widget_id}) values = self.defaults.copy() values.update( {'value': self.temp_image.original.name, 'img_src': self.temp_image.original.name}) self.assertEquals(result, self.expected % values) def test_render_existing(self): """ In this case there is an image associated with the dummy obj. When the widget is rendered it should show the *saved* image """ dummy = TestDummy() dummy.save() dummy_image = TestDummyImage(dummy=dummy, image=self.image) dummy_image.save() result = self.widget.render( self.widget_name, dummy_image.image, {'id': 'id_%s' % self.widget_name}) values = self.defaults.copy() values.update( {'value': dummy_image.pk, 'img_src': dummy_image.image.name}) self.assertEquals(result, self.expected % values)