Pass in array of JSON objects, a dictionary with key/value pairs to match against, and another dictionary with expected key/value pairs. If the should_not_find flag is true, to_match should not be found in object_array
(object_array, to_match, expected, should_not_find=False)
| 176 | |
| 177 | |
| 178 | def assert_array_result(object_array, to_match, expected, should_not_find=False): |
| 179 | """ |
| 180 | Pass in array of JSON objects, a dictionary with key/value pairs |
| 181 | to match against, and another dictionary with expected key/value |
| 182 | pairs. |
| 183 | If the should_not_find flag is true, to_match should not be found |
| 184 | in object_array |
| 185 | """ |
| 186 | if should_not_find: |
| 187 | assert_equal(expected, {}) |
| 188 | num_matched = 0 |
| 189 | for item in object_array: |
| 190 | all_match = True |
| 191 | for key, value in to_match.items(): |
| 192 | if item[key] != value: |
| 193 | all_match = False |
| 194 | if not all_match: |
| 195 | continue |
| 196 | elif should_not_find: |
| 197 | num_matched = num_matched + 1 |
| 198 | for key, value in expected.items(): |
| 199 | if item[key] != value: |
| 200 | raise AssertionError("%s : expected %s=%s" % (str(item), str(key), str(value))) |
| 201 | num_matched = num_matched + 1 |
| 202 | if num_matched == 0 and not should_not_find: |
| 203 | raise AssertionError("No objects matched %s" % (str(to_match))) |
| 204 | if num_matched > 0 and should_not_find: |
| 205 | raise AssertionError("Objects were found %s" % (str(to_match))) |
| 206 | |
| 207 | |
| 208 | # Utility functions |