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)
| 141 | "String %r contains invalid characters for a hash." % string) |
| 142 | |
| 143 | def assert_array_result(object_array, to_match, expected, should_not_find=False): |
| 144 | """ |
| 145 | Pass in array of JSON objects, a dictionary with key/value pairs |
| 146 | to match against, and another dictionary with expected key/value |
| 147 | pairs. |
| 148 | If the should_not_find flag is true, to_match should not be found |
| 149 | in object_array |
| 150 | """ |
| 151 | if should_not_find: |
| 152 | assert_equal(expected, {}) |
| 153 | num_matched = 0 |
| 154 | for item in object_array: |
| 155 | all_match = True |
| 156 | for key, value in to_match.items(): |
| 157 | if item[key] != value: |
| 158 | all_match = False |
| 159 | if not all_match: |
| 160 | continue |
| 161 | elif should_not_find: |
| 162 | num_matched = num_matched + 1 |
| 163 | for key, value in expected.items(): |
| 164 | if item[key] != value: |
| 165 | raise AssertionError("%s : expected %s=%s" % (str(item), str(key), str(value))) |
| 166 | num_matched = num_matched + 1 |
| 167 | if num_matched == 0 and not should_not_find: |
| 168 | raise AssertionError("No objects matched %s" % (str(to_match))) |
| 169 | if num_matched > 0 and should_not_find: |
| 170 | raise AssertionError("Objects were found %s" % (str(to_match))) |
| 171 | |
| 172 | # Utility functions |
| 173 | ################### |