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)
| 210 | |
| 211 | |
| 212 | def assert_array_result(object_array, to_match, expected, should_not_find=False): |
| 213 | """ |
| 214 | Pass in array of JSON objects, a dictionary with key/value pairs |
| 215 | to match against, and another dictionary with expected key/value |
| 216 | pairs. |
| 217 | If the should_not_find flag is true, to_match should not be found |
| 218 | in object_array |
| 219 | """ |
| 220 | if should_not_find: |
| 221 | assert_equal(expected, {}) |
| 222 | num_matched = 0 |
| 223 | for item in object_array: |
| 224 | all_match = True |
| 225 | for key, value in to_match.items(): |
| 226 | if item[key] != value: |
| 227 | all_match = False |
| 228 | if not all_match: |
| 229 | continue |
| 230 | elif should_not_find: |
| 231 | num_matched = num_matched + 1 |
| 232 | for key, value in expected.items(): |
| 233 | if item[key] != value: |
| 234 | raise AssertionError("%s : expected %s=%s" % (str(item), str(key), str(value))) |
| 235 | num_matched = num_matched + 1 |
| 236 | if num_matched == 0 and not should_not_find: |
| 237 | raise AssertionError("No objects matched %s" % (str(to_match))) |
| 238 | if num_matched > 0 and should_not_find: |
| 239 | raise AssertionError("Objects were found %s" % (str(to_match))) |
| 240 | |
| 241 | |
| 242 | # Utility functions |