OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2013 Google Inc. | 4 Copyright 2013 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 Test imagediffdb.py | 9 Test imagediffdb.py |
10 | |
11 TODO(epoger): Modify to use Python's unittest framework. | |
12 """ | 10 """ |
13 | 11 |
14 # System-level imports | 12 # System-level imports |
15 import logging | 13 import logging |
| 14 import shutil |
| 15 import tempfile |
| 16 import unittest |
16 | 17 |
17 # Local imports | 18 # Local imports |
18 import imagediffdb | 19 import imagediffdb |
19 | 20 |
20 | 21 |
21 IMAGE_URL_BASE = 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm/bi
tmap-64bitMD5/' | 22 IMG_URL_BASE = 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm/bitm
ap-64bitMD5/' |
| 23 |
| 24 |
| 25 class ImageDiffDbTest(unittest.TestCase): |
| 26 |
| 27 def setUp(self): |
| 28 self._temp_dir = tempfile.mkdtemp() |
| 29 |
| 30 def tearDown(self): |
| 31 shutil.rmtree(self._temp_dir) |
| 32 |
| 33 def shortDescription(self): |
| 34 """Tell unittest framework to not print docstrings for test cases.""" |
| 35 return None |
| 36 |
| 37 def test_simple(self): |
| 38 # params for each self-test: |
| 39 # 0. expected image locator |
| 40 # 1. expected image URL |
| 41 # 2. actual image locator |
| 42 # 3. actual image URL |
| 43 # 4. expected percent_pixels_differing (as a string, to 4 decimal places) |
| 44 # 5. expected weighted_diff_measure (as a string, to 4 decimal places) |
| 45 # 6. expected max_diff_per_channel |
| 46 selftests = [ |
| 47 [ |
| 48 '16206093933823793653', |
| 49 IMG_URL_BASE + 'arcofzorro/16206093933823793653.png', |
| 50 '13786535001616823825', |
| 51 IMG_URL_BASE + 'arcofzorro/13786535001616823825.png', |
| 52 '0.0662', '0.0113', [255, 255, 247], |
| 53 ], |
| 54 [ |
| 55 '10552995703607727960', |
| 56 IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png', |
| 57 '11198253335583713230', |
| 58 IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png', |
| 59 '100.0000', '66.6667', [255, 0, 255], |
| 60 ], |
| 61 ] |
| 62 |
| 63 # Add all image pairs to the database |
| 64 db = imagediffdb.ImageDiffDB(self._temp_dir) |
| 65 for selftest in selftests: |
| 66 retval = db.add_image_pair( |
| 67 expected_image_locator=selftest[0], expected_image_url=selftest[1], |
| 68 actual_image_locator=selftest[2], actual_image_url=selftest[3]) |
| 69 |
| 70 # Fetch each image pair from the database |
| 71 for selftest in selftests: |
| 72 record = db.get_diff_record(expected_image_locator=selftest[0], |
| 73 actual_image_locator=selftest[2]) |
| 74 self.assertEqual('%.4f' % record.get_percent_pixels_differing(), |
| 75 selftest[4]) |
| 76 self.assertEqual('%.4f' % record.get_weighted_diff_measure(), selftest[5]) |
| 77 self.assertEqual(record.get_max_diff_per_channel(), selftest[6]) |
| 78 |
22 | 79 |
23 def main(): | 80 def main(): |
24 logging.basicConfig(level=logging.INFO) | 81 suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest) |
25 | 82 unittest.TextTestRunner(verbosity=2).run(suite) |
26 # params for each self-test: | |
27 # 0. expected image locator | |
28 # 1. expected image URL | |
29 # 2. actual image locator | |
30 # 3. actual image URL | |
31 # 4. expected percent_pixels_differing (as a string, to 4 decimal places) | |
32 # 5. expected weighted_diff_measure (as a string, to 4 decimal places) | |
33 # 6. expected max_diff_per_channel | |
34 selftests = [ | |
35 [ | |
36 '16206093933823793653', | |
37 IMAGE_URL_BASE + 'arcofzorro/16206093933823793653.png', | |
38 '13786535001616823825', | |
39 IMAGE_URL_BASE + 'arcofzorro/13786535001616823825.png', | |
40 '0.0662', '0.0113', [255, 255, 247], | |
41 ], | |
42 [ | |
43 '10552995703607727960', | |
44 IMAGE_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png', | |
45 '11198253335583713230', | |
46 IMAGE_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png', | |
47 '100.0000', '66.6667', [255, 0, 255], | |
48 ], | |
49 ] | |
50 | |
51 # Add all image pairs to the database | |
52 db = imagediffdb.ImageDiffDB('/tmp/ImageDiffDB') | |
53 for selftest in selftests: | |
54 retval = db.add_image_pair( | |
55 expected_image_locator=selftest[0], expected_image_url=selftest[1], | |
56 actual_image_locator=selftest[2], actual_image_url=selftest[3]) | |
57 | |
58 # Fetch each image pair from the database | |
59 for selftest in selftests: | |
60 record = db.get_diff_record(expected_image_locator=selftest[0], | |
61 actual_image_locator=selftest[2]) | |
62 assert (('%.4f' % record.get_percent_pixels_differing()) == selftest[4]) | |
63 assert (('%.4f' % record.get_weighted_diff_measure()) == selftest[5]) | |
64 assert (record.get_max_diff_per_channel() == selftest[6]) | |
65 logging.info("Self-test completed successfully!") | |
66 | 83 |
67 | 84 |
68 if __name__ == '__main__': | 85 if __name__ == '__main__': |
69 main() | 86 main() |
OLD | NEW |