OLD | NEW |
(Empty) | |
| 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' |
| 23 IMAGEPAIR_1_AS_DICT = { |
| 24 imagepair.KEY__EXTRA_COLUMN_VALUES: { |
| 25 'builder': 'MyBuilder', |
| 26 'test': 'test1', |
| 27 }, |
| 28 imagepair.KEY__IMAGE_A_URL: 'test1/1111.png', |
| 29 imagepair.KEY__IMAGE_B_URL: 'test1/1111.png', |
| 30 imagepair.KEY__IS_DIFFERENT: False, |
| 31 } |
| 32 IMAGEPAIR_2_AS_DICT = { |
| 33 imagepair.KEY__DIFFERENCE_DATA: { |
| 34 'maxDiffPerChannel': [1, 2, 3], |
| 35 'numDifferingPixels': 111, |
| 36 'percentDifferingPixels': 22.222, |
| 37 'weightedDiffMeasure': 33.333, |
| 38 }, |
| 39 imagepair.KEY__EXTRA_COLUMN_VALUES: { |
| 40 'builder': 'MyBuilder', |
| 41 'test': 'test2', |
| 42 }, |
| 43 imagepair.KEY__IMAGE_A_URL: 'test2/2222.png', |
| 44 imagepair.KEY__IMAGE_B_URL: 'test2/22223.png', |
| 45 imagepair.KEY__IS_DIFFERENT: True, |
| 46 } |
| 47 IMAGEPAIR_3_AS_DICT = { |
| 48 imagepair.KEY__DIFFERENCE_DATA: { |
| 49 'maxDiffPerChannel': [4, 5, 6], |
| 50 'numDifferingPixels': 111, |
| 51 'percentDifferingPixels': 44.444, |
| 52 'weightedDiffMeasure': 33.333, |
| 53 }, |
| 54 imagepair.KEY__EXPECTATIONS_DATA: { |
| 55 'bugs': [1001, 1002], |
| 56 'ignoreFailure': True, |
| 57 }, |
| 58 imagepair.KEY__EXTRA_COLUMN_VALUES: { |
| 59 'builder': 'MyBuilder', |
| 60 'test': 'test3', |
| 61 }, |
| 62 imagepair.KEY__IMAGE_A_URL: 'test3/3333.png', |
| 63 imagepair.KEY__IMAGE_B_URL: 'test3/33334.png', |
| 64 imagepair.KEY__IS_DIFFERENT: True, |
| 65 } |
| 66 SET_A_DESCRIPTION = 'expectations' |
| 67 SET_B_DESCRIPTION = 'actuals' |
| 68 |
| 69 |
| 70 class ImagePairSetTest(unittest.TestCase): |
| 71 |
| 72 def setUp(self): |
| 73 self.maxDiff = None # do not truncate diffs when tests fail |
| 74 |
| 75 def shortDescription(self): |
| 76 """Tells unittest framework to not print docstrings for test cases.""" |
| 77 return None |
| 78 |
| 79 def test_success(self): |
| 80 """Assembles some ImagePairs into an ImagePairSet, and validates results. |
| 81 """ |
| 82 image_pairs = [ |
| 83 MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_1_AS_DICT), |
| 84 MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_2_AS_DICT), |
| 85 MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_3_AS_DICT), |
| 86 ] |
| 87 expected_imageset_dict = { |
| 88 'columnHeaders': { |
| 89 'builder': { |
| 90 'headerText': 'builder', |
| 91 'isFilterable': True, |
| 92 'isSortable': True, |
| 93 'valuesAndCounts': { |
| 94 'MyBuilder': 3 |
| 95 }, |
| 96 }, |
| 97 'test': { |
| 98 'headerText': 'which GM test', |
| 99 'headerUrl': 'http://learn/about/gm/tests', |
| 100 'isFilterable': True, |
| 101 'isSortable': False, |
| 102 }, |
| 103 }, |
| 104 'imagePairs': [ |
| 105 IMAGEPAIR_1_AS_DICT, |
| 106 IMAGEPAIR_2_AS_DICT, |
| 107 IMAGEPAIR_3_AS_DICT, |
| 108 ], |
| 109 'imageSets': [ |
| 110 { |
| 111 'baseUrl': BASE_URL_1, |
| 112 'description': SET_A_DESCRIPTION, |
| 113 }, |
| 114 { |
| 115 'baseUrl': BASE_URL_1, |
| 116 'description': SET_B_DESCRIPTION, |
| 117 }, |
| 118 ], |
| 119 } |
| 120 |
| 121 image_pair_set = imagepairset.ImagePairSet( |
| 122 descriptions=(SET_A_DESCRIPTION, SET_B_DESCRIPTION)) |
| 123 for image_pair in image_pairs: |
| 124 image_pair_set.add_image_pair(image_pair) |
| 125 # The 'builder' column header uses the default settings, |
| 126 # but the 'test' column header has manual adjustments. |
| 127 image_pair_set.set_column_header_factory( |
| 128 'test', |
| 129 column.ColumnHeaderFactory( |
| 130 header_text='which GM test', |
| 131 header_url='http://learn/about/gm/tests', |
| 132 is_filterable=True, |
| 133 is_sortable=False, |
| 134 include_values_and_counts=False)) |
| 135 self.assertEqual(image_pair_set.as_dict(), expected_imageset_dict) |
| 136 |
| 137 def test_mismatched_base_url(self): |
| 138 """Confirms that mismatched base_urls will cause an exception.""" |
| 139 image_pair_set = imagepairset.ImagePairSet() |
| 140 image_pair_set.add_image_pair( |
| 141 MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_1_AS_DICT)) |
| 142 image_pair_set.add_image_pair( |
| 143 MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_2_AS_DICT)) |
| 144 with self.assertRaises(Exception): |
| 145 image_pair_set.add_image_pair( |
| 146 MockImagePair(base_url=BASE_URL_2, |
| 147 dict_to_return=IMAGEPAIR_3_AS_DICT)) |
| 148 |
| 149 |
| 150 class MockImagePair(object): |
| 151 """Mock ImagePair object, which will return canned results.""" |
| 152 def __init__(self, base_url, dict_to_return): |
| 153 """ |
| 154 Args: |
| 155 base_url: base_url attribute for this object |
| 156 dict_to_return: dictionary to return from as_dict() |
| 157 """ |
| 158 self.base_url = base_url |
| 159 self.extra_columns_dict = dict_to_return.get( |
| 160 imagepair.KEY__EXTRA_COLUMN_VALUES, None) |
| 161 self._dict_to_return = dict_to_return |
| 162 |
| 163 def as_dict(self): |
| 164 return self._dict_to_return |
| 165 |
| 166 |
| 167 def main(): |
| 168 suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairSetTest) |
| 169 unittest.TextTestRunner(verbosity=2).run(suite) |
| 170 |
| 171 |
| 172 if __name__ == '__main__': |
| 173 main() |
OLD | NEW |