def test_figure(self): # http://stackoverflow.com/a/2473445/1694979 imgfile = StringIO( 'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,' '\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;') imgfile.name = 'test_img_file.gif' self.q.figure.save('image', ContentFile(imgfile.read())) self.assertIsInstance(self.q.figure, ImageFieldFile)
def test_image_in_question(self): imgfile = StringIO( 'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,' '\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;') imgfile.name = 'test_img_file.gif' self.question1.figure.save('image', ContentFile(imgfile.read())) response = self.client.get('/tq1/take/') self.assertContains(response, '<img src=') self.assertContains(response, 'alt="' + str(self.question1.content))
def dummy_file(content, name='dummy_file', mode='rb'): """ Create an in-memory, file-like object. :param str content: the contents of the file :param str name: a name for the file :param str mode: the mode to open the file (ignored) :return: an object that looks like an open file handle. """ data_file = StringIO(content) data_file.name = name data_file.__enter__ = lambda: None data_file.__exit__ = lambda extype, value, traceback: None return data_file