def check_events(self, test, listener, session_ids): res = listener.results if not len(test['expectations']): return cmd_names = [event.command for event in res['started']] self.assertEqual(len(res['started']), len(test['expectations']), cmd_names) for i, expectation in enumerate(test['expectations']): event_type = next(iter(expectation)) event = res['started'][i] # The tests substitute 42 for any number other than 0. if (event.command_name == 'getMore' and event.command['getMore']): event.command['getMore'] = 42 elif event.command_name == 'killCursors': event.command['cursors'] = [42] # Replace afterClusterTime: 42 with actual afterClusterTime. expected_cmd = expectation[event_type]['command'] expected_read_concern = expected_cmd.get('readConcern') if expected_read_concern is not None: time = expected_read_concern.get('afterClusterTime') if time == 42: actual_time = event.command.get('readConcern', {}).get('afterClusterTime') if actual_time is not None: expected_read_concern['afterClusterTime'] = actual_time recovery_token = expected_cmd.get('recoveryToken') if recovery_token == 42: expected_cmd['recoveryToken'] = CompareType(dict) # Replace lsid with a name like "session0" to match test. if 'lsid' in event.command: for name, lsid in session_ids.items(): if event.command['lsid'] == lsid: event.command['lsid'] = name break for attr, expected in expectation[event_type].items(): actual = getattr(event, attr) if isinstance(expected, dict): for key, val in expected.items(): if val is None: if key in actual: self.fail("Unexpected key [%s] in %r" % (key, actual)) elif key not in actual: self.fail("Expected key [%s] in %r" % (key, actual)) else: self.assertEqual(val, actual[key], "Key [%s] in %s" % (key, actual)) else: self.assertEqual(actual, expected)
def wrap_types(val): """Support $$type assertion in command results.""" if isinstance(val, list): return [wrap_types(v) for v in val] if isinstance(val, abc.Mapping): typ = val.get('$$type') if typ: return CompareType(TYPES[typ]) d = {} for key in val: d[key] = wrap_types(val[key]) return d return val
def check_events(self, test, listener, session_ids): res = listener.results if not len(test['expectations']): return # Give a nicer message when there are missing or extra events cmds = decode_raw([event.command for event in res['started']]) self.assertEqual(len(res['started']), len(test['expectations']), cmds) for i, expectation in enumerate(test['expectations']): event_type = next(iter(expectation)) event = res['started'][i] # The tests substitute 42 for any number other than 0. if (event.command_name == 'getMore' and event.command['getMore']): event.command['getMore'] = Int64(42) elif event.command_name == 'killCursors': event.command['cursors'] = [Int64(42)] elif event.command_name == 'update': # TODO: remove this once PYTHON-1744 is done. # Add upsert and multi fields back into expectations. updates = expectation[event_type]['command']['updates'] for update in updates: update.setdefault('upsert', False) update.setdefault('multi', False) # Replace afterClusterTime: 42 with actual afterClusterTime. expected_cmd = expectation[event_type]['command'] expected_read_concern = expected_cmd.get('readConcern') if expected_read_concern is not None: time = expected_read_concern.get('afterClusterTime') if time == 42: actual_time = event.command.get('readConcern', {}).get('afterClusterTime') if actual_time is not None: expected_read_concern['afterClusterTime'] = actual_time recovery_token = expected_cmd.get('recoveryToken') if recovery_token == 42: expected_cmd['recoveryToken'] = CompareType(dict) # Replace lsid with a name like "session0" to match test. if 'lsid' in event.command: for name, lsid in session_ids.items(): if event.command['lsid'] == lsid: event.command['lsid'] = name break for attr, expected in expectation[event_type].items(): actual = getattr(event, attr) expected = wrap_types(expected) if isinstance(expected, dict): for key, val in expected.items(): if val is None: if key in actual: self.fail("Unexpected key [%s] in %r" % (key, actual)) elif key not in actual: self.fail("Expected key [%s] in %r" % (key, actual)) else: self.assertEqual(val, decode_raw(actual[key]), "Key [%s] in %s" % (key, actual)) else: self.assertEqual(actual, expected)