test_thumbnails.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from thumbnailer import _resizer
  2. from unittest import TestCase, main
  3. import os
  4. from PIL import Image
  5. class ThumbnailerTests(TestCase):
  6. def path(self, filename):
  7. return os.path.join(self.img_path, filename)
  8. def setUp(self):
  9. self.img_path = os.path.join(os.path.dirname(__file__), "test_data")
  10. self.img = Image.open(self.path("sample_image.jpg"))
  11. def testSquare(self):
  12. r = _resizer('square', '100', self.img_path)
  13. output = r.resize(self.img)
  14. self.assertEqual((100, 100), output.size)
  15. def testExact(self):
  16. r = _resizer('exact', '250x100', self.img_path)
  17. output = r.resize(self.img)
  18. self.assertEqual((250, 100), output.size)
  19. def testWidth(self):
  20. r = _resizer('aspect', '250x?', self.img_path)
  21. output = r.resize(self.img)
  22. self.assertEqual((250, 166), output.size)
  23. def testHeight(self):
  24. r = _resizer('aspect', '?x250', self.img_path)
  25. output = r.resize(self.img)
  26. self.assertEqual((375, 250), output.size)
  27. class ThumbnailerFilenameTest(TestCase):
  28. def path(self, *parts):
  29. return os.path.join(self.img_path, *parts)
  30. def setUp(self):
  31. self.img_path = os.path.join(os.path.dirname(__file__), "test_data")
  32. def testRoot(self):
  33. """Test a file that is in the root of img_path."""
  34. r = _resizer('square', '100', self.img_path)
  35. new_name = r.get_thumbnail_name(self.path('sample_image.jpg'))
  36. self.assertEqual('sample_image_square.jpg', new_name)
  37. def testRootWithSlash(self):
  38. r = _resizer('square', '100', self.img_path + '/')
  39. new_name = r.get_thumbnail_name(self.path('sample_image.jpg'))
  40. self.assertEqual('sample_image_square.jpg', new_name)
  41. def testSubdir(self):
  42. """Test a file that is in a sub-directory of img_path."""
  43. r = _resizer('square', '100', self.img_path)
  44. new_name = r.get_thumbnail_name(self.path('subdir', 'sample_image.jpg'))
  45. self.assertEqual('subdir/sample_image_square.jpg', new_name)
  46. if __name__=="__main__":
  47. main()