Create test for make_passport.
[toast/gimp_passport.git] / test_gimp_passport.py
1 import os
2 import unittest
3 from PIL import Image, ImageDraw
4 from gimp_passport import mm_to_pixel, make_passport, tile, main
5
6
7 class TestMakePassport(unittest.TestCase):
8
9     def test_mm_to_pixel(self):
10         self.assertEqual(0, mm_to_pixel(0., 100))
11         self.assertEqual(200, mm_to_pixel(25.4, 200))
12         self.assertEqual(600, mm_to_pixel(2*25.4, 300))
13
14     def test_make_passport(self):
15         dpi = 90
16         img = Image.new('RGBA', (mm_to_pixel(60, dpi), mm_to_pixel(80, dpi)), '#bbbbbb00')
17         img.info['dpi'] = (dpi, dpi)
18         draw = ImageDraw.ImageDraw(img)
19         draw.rectangle((50, 60, 150, 170), fill='#00bbbbff')
20         pic = make_passport(img, 0.75, 35., 45.)
21         self.assertIsInstance(pic, Image.Image)
22
23     def test_tile(self):
24         dpi = 90
25         img = Image.new('RGB', (mm_to_pixel(35, dpi), mm_to_pixel(45, dpi)), 'lightgray')
26         img.info['dpi'] = (dpi, dpi)
27         draw = ImageDraw.ImageDraw(img)
28         draw.rectangle((0, 0, img.size[0]-1, img.size[1]-1), outline='black')
29         paper = tile(img, 150, 100, 4)
30         self.assertIsInstance(paper, Image.Image)
31
32     def test_main(self):
33         source = 'test_image.png'
34         dest = 'test_image_result.png'
35         if os.path.exists(dest):
36             os.remove(dest)
37         main(35., 45., 4., 100., 150., source, dest)
38         self.assertTrue(os.path.exists(dest))