class QuoteImageFieldTestCase(TestCase): def setUp(self): user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote(user=user, book=book, quotation="봐, 나는 살아있어!") def tearDown(self): try: os.remove(self.quote.photo.path) except: pass def test_saving_photo_field_text_file(self): self.quote.photo = SimpleUploadedFile('best_file_eva.txt', b'these are the file contents!') self.quote.save() #print('file saved:', self.quote.photo.path) exists = os.path.exists(self.quote.photo.path) self.assertTrue(exists) def test_saving_photo_field_image_file(self): image_content = open(IMAGE_FILEPATH, 'rb').read() self.quote.photo = SimpleUploadedFile(name='IMG_6114.jpg', content=image_content, content_type='image/jpeg') self.quote.save() # open saved image im = Image.open(self.quote.photo.path) self.assertEqual( im.filename, os.path.realpath('bookshot/tests/media/bookshot/IMG_6114.jpg')) self.assertEqual(im.size, (3264, 2448))
class QuoteImageFieldTestCase(TestCase): def setUp(self): user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote( user=user, book=book, quotation="봐, 나는 살아있어!" ) def tearDown(self): try: os.remove(self.quote.photo.path) except: pass def test_saving_photo_field_text_file(self): self.quote.photo = SimpleUploadedFile('best_file_eva.txt', b'these are the file contents!') self.quote.save() #print('file saved:', self.quote.photo.path) exists = os.path.exists(self.quote.photo.path) self.assertTrue(exists) def test_saving_photo_field_image_file(self): image_content = open(IMAGE_FILEPATH, 'rb').read() self.quote.photo = SimpleUploadedFile(name='IMG_6114.jpg', content=image_content, content_type='image/jpeg') self.quote.save() # open saved image im = Image.open(self.quote.photo.path) self.assertEqual(im.filename, os.path.realpath('bookshot/tests/media/bookshot/IMG_6114.jpg')) self.assertEqual(im.size, (3264,2448))
class ResizeImageTestCase(TestCase): def setUp(self): self.IMAGE_FILEPATH = os.path.join(TEST_ROOT, 'fixtures/media/IMG_6114.jpg') user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote(user=user, book=book, quotation="봐, 나는 살아있어!") self.image_content = open(self.IMAGE_FILEPATH, 'rb').read() def tearDown(self): try: os.remove(self.quote.photo.path) except: pass def test_save_large_image_file_to_smaller_image(self): self.quote.photo = SimpleUploadedFile(name='IMG_6114.jpg', content=self.image_content, content_type='image/jpeg') self.quote.resize_image(max_size=(640, 640)) self.quote.save() image = Image.open(self.IMAGE_FILEPATH) self.assertTrue(image.width > 640) self.assertEqual(self.quote.photo.width, 640) self.assertTrue(self.quote.photo.height < 640)
def setUp(self): self.IMAGE_FILEPATH = os.path.join(TEST_ROOT, 'fixtures/media/IMG_6114.jpg') user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote(user=user, book=book, quotation="봐, 나는 살아있어!") self.image_content = open(self.IMAGE_FILEPATH, 'rb').read()
def test_returns_cropped_part_from_original_image(self): width = 200 height = 200 cropped_image = Quote.crop_image(self.image_filename, (0, 0, width, height), self.cropped_image_filename) self.assertIsNotNone(cropped_image) self.assertEqual(cropped_image.size, (width, height))
def setUp(self): user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote( user=user, book=book, quotation="봐, 나는 살아있어!" )
def setUp(self): user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote( user=user, book=book, quotation="봐, 나는 살아있어!" ) self.image_content = open(IMAGE_FILEPATH, 'rb').read()
class QuotePhotoResizeTestCase(TestCase): def setUp(self): user = User.objects.create_user('jhk', '*****@*****.**', 'jhpassword') book = Book.objects.create(title="스토너") self.quote = Quote( user=user, book=book, quotation="봐, 나는 살아있어!" ) self.image_content = open(IMAGE_FILEPATH, 'rb').read() def tearDown(self): try: os.remove(self.quote.photo.path) except: pass def test_resize_huge_image(self): self.quote.photo = SimpleUploadedFile(name='IMG_6114.jpg', content=self.image_content, content_type='image/jpeg') # resize self.quote.resize_image(max_size=(640,640)) self.quote.save() # verify prev state from django.core.files.base import ContentFile big_im = Image.open(ContentFile(self.image_content)) self.assertTrue(big_im.width > 640) self.assertTrue(big_im.height > 640) # assert: open saved image small_im = Image.open(self.quote.photo.path) self.assertTrue(small_im.width == 640) self.assertTrue(small_im.height <= 640)
def test_pass_max_size_as_param(self): width, height = Quote.calculate_image_dimension(850, 567, max_size=(850, 850)) self.assertEqual(width, 850) self.assertEqual(height, 567)
def test_keeps_ratio_of_original_size_when_portrait(self): width, height = Quote.calculate_image_dimension(567, 850) self.assertEqual(width, 426) self.assertEqual(height, 640) self.assertAlmostEqual(567 / 850., width / float(height), delta=0.002)
def test_keeps_ratio_of_original_size_when_landscape(self): width, height = Quote.calculate_image_dimension(850, 567) self.assertEqual(width, 640) self.assertEqual(height, 426) self.assertAlmostEqual(850 / 567., width / float(height), delta=0.004)
def test_returns_max_size_if_larger(self): width, height = Quote.calculate_image_dimension(1280, 1280) self.assertEqual(width, 640) self.assertEqual(height, 640)
def test_returns_itself_if_same(self): width, height = Quote.calculate_image_dimension(640, 640) self.assertEqual(width, 640) self.assertEqual(height, 640)
def test_returns_itself_if_smaller(self): width, height = Quote.calculate_image_dimension(320, 320) self.assertEqual(width, 320) self.assertEqual(height, 320)