aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/rebaseline_server/imagepairset_test.py
blob: c046ec7c7cf6e5e24587439ada4d0c7599fb7912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python

"""
Copyright 2014 Google Inc.

Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.

Test imagepairset.py
"""

# System-level imports
import unittest

# Local imports
import column
import imagepair
import imagepairset


BASE_URL_1 = 'http://base/url/1'
BASE_URL_2 = 'http://base/url/2'
DIFF_BASE_URL = 'http://diff/base/url'
IMAGEPAIR_1_AS_DICT = {
    imagepair.KEY__EXTRA_COLUMN_VALUES: {
        'builder': 'MyBuilder',
        'test': 'test1',
    },
    imagepair.KEY__IMAGE_A_URL: 'test1/1111.png',
    imagepair.KEY__IMAGE_B_URL: 'test1/1111.png',
    imagepair.KEY__IS_DIFFERENT: False,
}
IMAGEPAIR_2_AS_DICT = {
    imagepair.KEY__DIFFERENCE_DATA: {
        'maxDiffPerChannel': [1, 2, 3],
        'numDifferingPixels': 111,
        'percentDifferingPixels': 22.222,
        'weightedDiffMeasure': 33.333,
    },
    imagepair.KEY__EXTRA_COLUMN_VALUES: {
        'builder': 'MyBuilder',
        'test': 'test2',
    },
    imagepair.KEY__IMAGE_A_URL: 'test2/2222.png',
    imagepair.KEY__IMAGE_B_URL: 'test2/22223.png',
    imagepair.KEY__IS_DIFFERENT: True,
}
IMAGEPAIR_3_AS_DICT = {
    imagepair.KEY__DIFFERENCE_DATA: {
        'maxDiffPerChannel': [4, 5, 6],
        'numDifferingPixels': 111,
        'percentDifferingPixels': 44.444,
        'weightedDiffMeasure': 33.333,
    },
    imagepair.KEY__EXPECTATIONS_DATA: {
        'bugs': [1001, 1002],
        'ignoreFailure': True,
    },
    imagepair.KEY__EXTRA_COLUMN_VALUES: {
        'builder': 'MyBuilder',
        'test': 'test3',
    },
    imagepair.KEY__IMAGE_A_URL: 'test3/3333.png',
    imagepair.KEY__IMAGE_B_URL: 'test3/33334.png',
    imagepair.KEY__IS_DIFFERENT: True,
}
SET_A_DESCRIPTION = 'expectations'
SET_B_DESCRIPTION = 'actuals'


class ImagePairSetTest(unittest.TestCase):

  def setUp(self):
    self.maxDiff = None  # do not truncate diffs when tests fail

  def shortDescription(self):
    """Tells unittest framework to not print docstrings for test cases."""
    return None

  def test_success(self):
    """Assembles some ImagePairs into an ImagePairSet, and validates results.
    """
    image_pairs = [
        MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_1_AS_DICT),
        MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_2_AS_DICT),
        MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_3_AS_DICT),
    ]
    expected_imageset_dict = {
        'extraColumnHeaders': {
            'builder': {
                'headerText': 'builder',
                'isFilterable': True,
                'isSortable': True,
                'valuesAndCounts': {
                    'MyBuilder': 3
                },
            },
            'test': {
                'headerText': 'which GM test',
                'headerUrl': 'http://learn/about/gm/tests',
                'isFilterable': True,
                'isSortable': False,
            },
        },
        'imagePairs': [
            IMAGEPAIR_1_AS_DICT,
            IMAGEPAIR_2_AS_DICT,
            IMAGEPAIR_3_AS_DICT,
        ],
        'imageSets': {
            'imageA': {
                'baseUrl': BASE_URL_1,
                'description': SET_A_DESCRIPTION,
            },
            'imageB': {
                'baseUrl': BASE_URL_1,
                'description': SET_B_DESCRIPTION,
            },
            'diffs': {
                'baseUrl': DIFF_BASE_URL + '/diffs',
                'description': 'color difference per channel',
            },
            'whiteDiffs': {
                'baseUrl': DIFF_BASE_URL + '/whitediffs',
                'description': 'differing pixels in white',
            },
        },
    }

    image_pair_set = imagepairset.ImagePairSet(
        descriptions=(SET_A_DESCRIPTION, SET_B_DESCRIPTION),
        diff_base_url=DIFF_BASE_URL)
    for image_pair in image_pairs:
      image_pair_set.add_image_pair(image_pair)
    # The 'builder' column header uses the default settings,
    # but the 'test' column header has manual adjustments.
    image_pair_set.set_column_header_factory(
        'test',
        column.ColumnHeaderFactory(
            header_text='which GM test',
            header_url='http://learn/about/gm/tests',
            is_filterable=True,
            is_sortable=False,
            include_values_and_counts=False))
    self.assertEqual(image_pair_set.as_dict(), expected_imageset_dict)

  def test_mismatched_base_url(self):
    """Confirms that mismatched base_urls will cause an exception."""
    image_pair_set = imagepairset.ImagePairSet(
        diff_base_url=DIFF_BASE_URL)
    image_pair_set.add_image_pair(
        MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_1_AS_DICT))
    image_pair_set.add_image_pair(
        MockImagePair(base_url=BASE_URL_1, dict_to_return=IMAGEPAIR_2_AS_DICT))
    with self.assertRaises(Exception):
      image_pair_set.add_image_pair(
          MockImagePair(base_url=BASE_URL_2,
                        dict_to_return=IMAGEPAIR_3_AS_DICT))


class MockImagePair(object):
  """Mock ImagePair object, which will return canned results."""
  def __init__(self, base_url, dict_to_return):
    """
    Args:
      base_url: base_url attribute for this object
      dict_to_return: dictionary to return from as_dict()
    """
    self.base_url = base_url
    self.extra_columns_dict = dict_to_return.get(
        imagepair.KEY__EXTRA_COLUMN_VALUES, None)
    self._dict_to_return = dict_to_return

  def as_dict(self):
    return self._dict_to_return


def main():
  suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairSetTest)
  unittest.TextTestRunner(verbosity=2).run(suite)


if __name__ == '__main__':
  main()