blob: a931e047aa22acffce6b04feea34bc939b51ad52 [file] [log] [blame]
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +00001#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9Test imagepairset.py
10"""
11
12# System-level imports
13import unittest
14
15# Local imports
16import column
17import imagepair
18import imagepairset
19
20
21BASE_URL_1 = 'http://base/url/1'
22BASE_URL_2 = 'http://base/url/2'
commit-bot@chromium.org57994232014-03-20 17:27:46 +000023DIFF_BASE_URL = 'http://diff/base/url'
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000024IMAGEPAIR_1_AS_DICT = {
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000025 imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000026 'builder': 'MyBuilder',
27 'test': 'test1',
28 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000029 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.org536b15f2014-02-13 17:17:05 +000032}
33IMAGEPAIR_2_AS_DICT = {
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000034 imagepair.KEY__IMAGEPAIRS__DIFFERENCES: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000035 'maxDiffPerChannel': [1, 2, 3],
36 'numDifferingPixels': 111,
37 'percentDifferingPixels': 22.222,
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000038 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000039 imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000040 'builder': 'MyBuilder',
41 'test': 'test2',
42 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000043 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.org536b15f2014-02-13 17:17:05 +000046}
47IMAGEPAIR_3_AS_DICT = {
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000048 imagepair.KEY__IMAGEPAIRS__DIFFERENCES: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000049 'maxDiffPerChannel': [4, 5, 6],
50 'numDifferingPixels': 111,
51 'percentDifferingPixels': 44.444,
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000052 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000053 imagepair.KEY__IMAGEPAIRS__EXPECTATIONS: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000054 'bugs': [1001, 1002],
55 'ignoreFailure': True,
56 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000057 imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS: {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000058 'builder': 'MyBuilder',
59 'test': 'test3',
60 },
commit-bot@chromium.org68a38152014-05-12 20:40:29 +000061 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.org536b15f2014-02-13 17:17:05 +000064}
65SET_A_DESCRIPTION = 'expectations'
66SET_B_DESCRIPTION = 'actuals'
67
68
69class 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 = [
rmistry2529f2e2014-08-22 04:46:30 -070082 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.org536b15f2014-02-13 17:17:05 +000088 ]
89 expected_imageset_dict = {
commit-bot@chromium.org16f41802014-02-26 19:05:20 +000090 'extraColumnHeaders': {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000091 'builder': {
92 'headerText': 'builder',
93 'isFilterable': True,
94 'isSortable': True,
epogerb4edbff2014-07-09 06:19:20 -070095 'useFreeformFilter': False,
commit-bot@chromium.org97f0b082014-05-08 21:15:20 +000096 'valuesAndCounts': [('MyBuilder', 3)],
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000097 },
98 'test': {
99 'headerText': 'which GM test',
100 'headerUrl': 'http://learn/about/gm/tests',
101 'isFilterable': True,
102 'isSortable': False,
epogerb4edbff2014-07-09 06:19:20 -0700103 'useFreeformFilter': False,
104 'valuesAndCounts': [('test1', 1),
105 ('test2', 1),
106 ('test3', 1)],
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000107 },
108 },
epogerb4edbff2014-07-09 06:19:20 -0700109 'extraColumnOrder': ['builder', 'test'],
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000110 'imagePairs': [
111 IMAGEPAIR_1_AS_DICT,
112 IMAGEPAIR_2_AS_DICT,
113 IMAGEPAIR_3_AS_DICT,
114 ],
commit-bot@chromium.orgd1c85d22014-03-17 14:22:02 +0000115 'imageSets': {
116 'imageA': {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000117 'baseUrl': BASE_URL_1,
118 'description': SET_A_DESCRIPTION,
119 },
commit-bot@chromium.orgd1c85d22014-03-17 14:22:02 +0000120 'imageB': {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000121 'baseUrl': BASE_URL_1,
122 'description': SET_B_DESCRIPTION,
123 },
commit-bot@chromium.orgd1c85d22014-03-17 14:22:02 +0000124 'diffs': {
commit-bot@chromium.org57994232014-03-20 17:27:46 +0000125 'baseUrl': DIFF_BASE_URL + '/diffs',
commit-bot@chromium.orgd1c85d22014-03-17 14:22:02 +0000126 'description': 'color difference per channel',
127 },
128 'whiteDiffs': {
commit-bot@chromium.org57994232014-03-20 17:27:46 +0000129 'baseUrl': DIFF_BASE_URL + '/whitediffs',
commit-bot@chromium.orgd1c85d22014-03-17 14:22:02 +0000130 'description': 'differing pixels in white',
131 },
132 },
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000133 }
134
135 image_pair_set = imagepairset.ImagePairSet(
commit-bot@chromium.org57994232014-03-20 17:27:46 +0000136 descriptions=(SET_A_DESCRIPTION, SET_B_DESCRIPTION),
137 diff_base_url=DIFF_BASE_URL)
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000138 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,
epogerb4edbff2014-07-09 06:19:20 -0700148 is_sortable=False))
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000149 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.org57994232014-03-20 17:27:46 +0000153 image_pair_set = imagepairset.ImagePairSet(
154 diff_base_url=DIFF_BASE_URL)
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000155 image_pair_set.add_image_pair(
rmistry2529f2e2014-08-22 04:46:30 -0700156 MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1,
157 dict_to_return=IMAGEPAIR_1_AS_DICT))
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000158 image_pair_set.add_image_pair(
rmistry2529f2e2014-08-22 04:46:30 -0700159 MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1,
160 dict_to_return=IMAGEPAIR_2_AS_DICT))
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000161 with self.assertRaises(Exception):
162 image_pair_set.add_image_pair(
rmistry2529f2e2014-08-22 04:46:30 -0700163 MockImagePair(imageA_base_url=BASE_URL_2, imageB_base_url=BASE_URL_2,
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000164 dict_to_return=IMAGEPAIR_3_AS_DICT))
165
epogerb4edbff2014-07-09 06:19:20 -0700166 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(
rmistry2529f2e2014-08-22 04:46:30 -0700172 MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1,
173 dict_to_return=IMAGEPAIR_1_AS_DICT))
epogerb4edbff2014-07-09 06:19:20 -0700174 image_pair_set.add_image_pair(
rmistry2529f2e2014-08-22 04:46:30 -0700175 MockImagePair(imageA_base_url=BASE_URL_1, imageB_base_url=BASE_URL_1,
176 dict_to_return=IMAGEPAIR_2_AS_DICT))
epogerb4edbff2014-07-09 06:19:20 -0700177 # 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.org536b15f2014-02-13 17:17:05 +0000185
186class MockImagePair(object):
187 """Mock ImagePair object, which will return canned results."""
rmistry2529f2e2014-08-22 04:46:30 -0700188 def __init__(self, imageA_base_url, imageB_base_url, dict_to_return):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000189 """
190 Args:
191 base_url: base_url attribute for this object
192 dict_to_return: dictionary to return from as_dict()
193 """
rmistry2529f2e2014-08-22 04:46:30 -0700194 self.imageA_base_url = imageA_base_url
195 self.imageB_base_url = imageB_base_url
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000196 self.extra_columns_dict = dict_to_return.get(
commit-bot@chromium.org68a38152014-05-12 20:40:29 +0000197 imagepair.KEY__IMAGEPAIRS__EXTRACOLUMNS, None)
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000198 self._dict_to_return = dict_to_return
199
200 def as_dict(self):
201 return self._dict_to_return
202
203
204def main():
205 suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairSetTest)
206 unittest.TextTestRunner(verbosity=2).run(suite)
207
208
209if __name__ == '__main__':
210 main()