commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | Test imagepairset.py |
| 10 | """ |
| 11 | |
| 12 | # System-level imports |
| 13 | import unittest |
| 14 | |
| 15 | # Local imports |
| 16 | import column |
| 17 | import imagepair |
| 18 | import imagepairset |
| 19 | |
| 20 | |
| 21 | BASE_URL_1 = 'http://base/url/1' |
| 22 | BASE_URL_2 = 'http://base/url/2' |
commit-bot@chromium.org | 5799423 | 2014-03-20 17:27:46 +0000 | [diff] [blame] | 23 | DIFF_BASE_URL = 'http://diff/base/url' |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 24 | IMAGEPAIR_1_AS_DICT = { |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 25 | imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 26 | 'builder': 'MyBuilder', |
| 27 | 'test': 'test1', |
| 28 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 29 | imagepair.KEY__IMAGEPAIRS__IMAGE_A_URL: 'test1/1111.png', |
| 30 | imagepair.KEY__IMAGEPAIRS__IMAGE_B_URL: 'test1/1111.png', |
| 31 | imagepair.KEY__IMAGEPAIRS__IS_DIFFERENT: False, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 32 | } |
| 33 | IMAGEPAIR_2_AS_DICT = { |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 34 | imagepair.KEY__IMAGEPAIRS__DIFFERENCES: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 35 | 'maxDiffPerChannel': [1, 2, 3], |
| 36 | 'numDifferingPixels': 111, |
| 37 | 'percentDifferingPixels': 22.222, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 38 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 39 | imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 40 | 'builder': 'MyBuilder', |
| 41 | 'test': 'test2', |
| 42 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 43 | imagepair.KEY__IMAGEPAIRS__IMAGE_A_URL: 'test2/2222.png', |
| 44 | imagepair.KEY__IMAGEPAIRS__IMAGE_B_URL: 'test2/22223.png', |
| 45 | imagepair.KEY__IMAGEPAIRS__IS_DIFFERENT: True, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 46 | } |
| 47 | IMAGEPAIR_3_AS_DICT = { |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 48 | imagepair.KEY__IMAGEPAIRS__DIFFERENCES: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 49 | 'maxDiffPerChannel': [4, 5, 6], |
| 50 | 'numDifferingPixels': 111, |
| 51 | 'percentDifferingPixels': 44.444, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 52 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 53 | imagepair.KEY__IMAGEPAIRS__EXPECTATIONS: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 54 | 'bugs': [1001, 1002], |
| 55 | 'ignoreFailure': True, |
| 56 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 57 | imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 58 | 'builder': 'MyBuilder', |
| 59 | 'test': 'test3', |
| 60 | }, |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 61 | imagepair.KEY__IMAGEPAIRS__IMAGE_A_URL: 'test3/3333.png', |
| 62 | imagepair.KEY__IMAGEPAIRS__IMAGE_B_URL: 'test3/33334.png', |
| 63 | imagepair.KEY__IMAGEPAIRS__IS_DIFFERENT: True, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 64 | } |
| 65 | SET_A_DESCRIPTION = 'expectations' |
| 66 | SET_B_DESCRIPTION = 'actuals' |
| 67 | |
| 68 | |
| 69 | class ImagePairSetTest(unittest.TestCase): |
| 70 | |
| 71 | def setUp(self): |
| 72 | self.maxDiff = None # do not truncate diffs when tests fail |
| 73 | |
| 74 | def shortDescription(self): |
| 75 | """Tells unittest framework to not print docstrings for test cases.""" |
| 76 | return None |
| 77 | |
| 78 | def test_success(self): |
| 79 | """Assembles some ImagePairs into an ImagePairSet, and validates results. |
| 80 | """ |
| 81 | image_pairs = [ |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 82 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 83 | dict_to_return=IMAGEPAIR_1_AS_DICT), |
| 84 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 85 | dict_to_return=IMAGEPAIR_2_AS_DICT), |
| 86 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 87 | dict_to_return=IMAGEPAIR_3_AS_DICT), |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 88 | ] |
| 89 | expected_imageset_dict = { |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 90 | 'extraColumnHeaders': { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 91 | 'builder': { |
| 92 | 'headerText': 'builder', |
| 93 | 'isFilterable': True, |
| 94 | 'isSortable': True, |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 95 | 'useFreeformFilter': False, |
commit-bot@chromium.org | 97f0b08 | 2014-05-08 21:15:20 +0000 | [diff] [blame] | 96 | 'valuesAndCounts': [('MyBuilder', 3)], |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 97 | }, |
| 98 | 'test': { |
| 99 | 'headerText': 'which GM test', |
| 100 | 'headerUrl': 'http://learn/about/gm/tests', |
| 101 | 'isFilterable': True, |
| 102 | 'isSortable': False, |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 103 | 'useFreeformFilter': False, |
| 104 | 'valuesAndCounts': [('test1', 1), |
| 105 | ('test2', 1), |
| 106 | ('test3', 1)], |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 107 | }, |
| 108 | }, |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 109 | 'extraColumnOrder': ['builder', 'test'], |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 110 | 'imagePairs': [ |
| 111 | IMAGEPAIR_1_AS_DICT, |
| 112 | IMAGEPAIR_2_AS_DICT, |
| 113 | IMAGEPAIR_3_AS_DICT, |
| 114 | ], |
commit-bot@chromium.org | d1c85d2 | 2014-03-17 14:22:02 +0000 | [diff] [blame] | 115 | 'imageSets': { |
| 116 | 'imageA': { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 117 | 'baseUrl': BASE_URL_1, |
| 118 | 'description': SET_A_DESCRIPTION, |
| 119 | }, |
commit-bot@chromium.org | d1c85d2 | 2014-03-17 14:22:02 +0000 | [diff] [blame] | 120 | 'imageB': { |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 121 | 'baseUrl': BASE_URL_1, |
| 122 | 'description': SET_B_DESCRIPTION, |
| 123 | }, |
commit-bot@chromium.org | d1c85d2 | 2014-03-17 14:22:02 +0000 | [diff] [blame] | 124 | 'diffs': { |
commit-bot@chromium.org | 5799423 | 2014-03-20 17:27:46 +0000 | [diff] [blame] | 125 | 'baseUrl': DIFF_BASE_URL + '/diffs', |
commit-bot@chromium.org | d1c85d2 | 2014-03-17 14:22:02 +0000 | [diff] [blame] | 126 | 'description': 'color difference per channel', |
| 127 | }, |
| 128 | 'whiteDiffs': { |
commit-bot@chromium.org | 5799423 | 2014-03-20 17:27:46 +0000 | [diff] [blame] | 129 | 'baseUrl': DIFF_BASE_URL + '/whitediffs', |
commit-bot@chromium.org | d1c85d2 | 2014-03-17 14:22:02 +0000 | [diff] [blame] | 130 | 'description': 'differing pixels in white', |
| 131 | }, |
| 132 | }, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | image_pair_set = imagepairset.ImagePairSet( |
commit-bot@chromium.org | 5799423 | 2014-03-20 17:27:46 +0000 | [diff] [blame] | 136 | descriptions=(SET_A_DESCRIPTION, SET_B_DESCRIPTION), |
| 137 | diff_base_url=DIFF_BASE_URL) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 138 | for image_pair in image_pairs: |
| 139 | image_pair_set.add_image_pair(image_pair) |
| 140 | # The 'builder' column header uses the default settings, |
| 141 | # but the 'test' column header has manual adjustments. |
| 142 | image_pair_set.set_column_header_factory( |
| 143 | 'test', |
| 144 | column.ColumnHeaderFactory( |
| 145 | header_text='which GM test', |
| 146 | header_url='http://learn/about/gm/tests', |
| 147 | is_filterable=True, |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 148 | is_sortable=False)) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 149 | self.assertEqual(image_pair_set.as_dict(), expected_imageset_dict) |
| 150 | |
| 151 | def test_mismatched_base_url(self): |
| 152 | """Confirms that mismatched base_urls will cause an exception.""" |
commit-bot@chromium.org | 5799423 | 2014-03-20 17:27:46 +0000 | [diff] [blame] | 153 | image_pair_set = imagepairset.ImagePairSet( |
| 154 | diff_base_url=DIFF_BASE_URL) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 155 | image_pair_set.add_image_pair( |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 156 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 157 | dict_to_return=IMAGEPAIR_1_AS_DICT)) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 158 | image_pair_set.add_image_pair( |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 159 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 160 | dict_to_return=IMAGEPAIR_2_AS_DICT)) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 161 | with self.assertRaises(Exception): |
| 162 | image_pair_set.add_image_pair( |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 163 | MockImagePair(imageA_base_url=BASE_URL_2, imageB_base_url=BASE_URL_2, |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 164 | dict_to_return=IMAGEPAIR_3_AS_DICT)) |
| 165 | |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 166 | def test_missing_column_ids(self): |
| 167 | """Confirms that passing truncated column_ids_in_order to as_dict() |
| 168 | will cause an exception.""" |
| 169 | image_pair_set = imagepairset.ImagePairSet( |
| 170 | diff_base_url=DIFF_BASE_URL) |
| 171 | image_pair_set.add_image_pair( |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 172 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 173 | dict_to_return=IMAGEPAIR_1_AS_DICT)) |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 174 | image_pair_set.add_image_pair( |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 175 | MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1, |
| 176 | dict_to_return=IMAGEPAIR_2_AS_DICT)) |
epoger | b4edbff | 2014-07-09 06:19:20 -0700 | [diff] [blame] | 177 | # Call as_dict() with default or reasonable column_ids_in_order. |
| 178 | image_pair_set.as_dict() |
| 179 | image_pair_set.as_dict(column_ids_in_order=['test', 'builder']) |
| 180 | image_pair_set.as_dict(column_ids_in_order=['test', 'builder', 'extra']) |
| 181 | # Call as_dict() with not enough column_ids. |
| 182 | with self.assertRaises(Exception): |
| 183 | image_pair_set.as_dict(column_ids_in_order=['builder']) |
| 184 | |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 185 | |
| 186 | class MockImagePair(object): |
| 187 | """Mock ImagePair object, which will return canned results.""" |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 188 | def __init__(self, imageA_base_url, imageB_base_url, dict_to_return): |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 189 | """ |
| 190 | Args: |
| 191 | base_url: base_url attribute for this object |
| 192 | dict_to_return: dictionary to return from as_dict() |
| 193 | """ |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 194 | self.imageA_base_url = imageA_base_url |
| 195 | self.imageB_base_url = imageB_base_url |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 196 | self.extra_columns_dict = dict_to_return.get( |
commit-bot@chromium.org | 68a3815 | 2014-05-12 20:40:29 +0000 | [diff] [blame] | 197 | imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS, None) |
commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 198 | self._dict_to_return = dict_to_return |
| 199 | |
| 200 | def as_dict(self): |
| 201 | return self._dict_to_return |
| 202 | |
| 203 | |
| 204 | def main(): |
| 205 | suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairSetTest) |
| 206 | unittest.TextTestRunner(verbosity=2).run(suite) |
| 207 | |
| 208 | |
| 209 | if __name__ == '__main__': |
| 210 | main() |