def messages_published(self, exchange: str, num_expected: int = 1, routing_key: str = '') -> bool: if not self.__rmq_context.exchange_is_tracked(exchange, routing_key): return False num_messages = self.__rmq_context.messages_published(exchange, routing_key) if num_expected is None and num_messages != 0: return True return validation.matches(num_expected, num_messages)
def file_matches_json(self, path: str, expected: dict) -> bool: path = self.get_file_matching(path) if not path: print(f'Expected exactly 1 matching file at: "{path}".') return False with open(path, 'rb') as data: actual = json.loads(data.read().decode('utf-8')) return validation.matches(expected, actual)
def test_matches_MatchingANYWithNestedList_ReturnsTrue(self): # Given expected = [{'bar': [{'bazz': validation.ANY}]}] actual = [{'bar': [{'bazz': 'buzz'}]}] # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_MatchingListOfLists_ReturnsTrue(self): # Given expected = [['foo']] actual = [['foo']] # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_MatchingListOfStringsOutOfOrder_ReturnsTrue(self): # Given expected = ['foo', 'bar'] actual = ['bar', 'foo'] # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_MatchingListOfDictsOutOfOrder_ReturnsTrue(self): # Given expected = [{'foo': 'bar'}, {'bazz': 'buzz'}] actual = [{'bazz': 'buzz'}, {'foo': 'bar'}] # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_MatchingListWithMultipleANY_ReturnsTrue(self): # Given expected = ['foo', validation.ANY, validation.ANY] actual = ['foo', 'bazz', 'bar'] # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_NonMatchingListWithANY_ReturnsFalse(self): # Given expected = ['foo', validation.ANY, 'bar'] actual = ['foo', 'bazz', 'buzz'] # When result = validation.matches(expected, actual) # Then self.assertFalse(result)
def test_matches_MatchingStrings_ReturnsTrue(self): # Given expected = 'foo' actual = 'foo' # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def then(self, given, result) -> bool: expected_response = { 'id': validation.ANY, 'firstName': given['firstName'], 'lastName': given['lastName'], 'email': given['email'] } return validation.matches( expected_response, result) and self.mocks.mysql.verify().row_exists( creds.MYSQL_DATABASE, creds.MYSQL_TABLE, given)
def payload_published_json(self, exchange: str, expected_payload: dict, routing_key: str = '') -> bool: if not self.__rmq_context.exchange_is_tracked(exchange, routing_key): return False payloads = self.__rmq_context.payloads_published(exchange, routing_key) for payload in payloads: try: payload = json.loads(payload) if validation.matches(expected_payload, payload, quiet=True): return True except JSONDecodeError: pass print(f'Expected:\n{expected_payload}\nwas not found in actual:\n{payloads}') return False
def __contained_json_verification(self, endpoint, http_verb, expected_body, url_pattern): bodies = self.__get_request_bodies(endpoint, http_verb, url_pattern) for body in bodies: try: body = json.loads(body) if validation.matches(expected_body, body, quiet=True): return True except JSONDecodeError: pass print( f'Expected:\n{expected_body}\nwas not found in actual:\n{bodies}') return False
def test_matches_NonMatchingDict_ReturnsFalse(self): # Given expected = { 'foo': 'bar' } actual = { 'foo': 'foo' } # When result = validation.matches(expected, actual) # Then self.assertFalse(result)
def test_matches_NonMatchingDictValueTypesExpectDictActualList_ReturnsFalse(self): # Given expected = { 'foo': {'bar': 'bar'} } actual = { 'foo': ['bar'] } # When result = validation.matches(expected, actual) # Then self.assertFalse(result)
def __count_verification(self, endpoint, http_verb, times, url_pattern): payload = { 'method': http_verb, self.__get_url_type(url_pattern): endpoint } data = json.dumps(payload).encode('utf-8') request = urllib.request.Request( f'{self.__url}/__admin/requests/count', data=data, headers={'Content-Type': 'application/json'}) response = urllib.request.urlopen(request).read() call_count = json.loads(response)['count'] if times is None: return call_count > 0 return validation.matches(times, call_count)
def test_matches_ActualHasExtraNestedField_ReturnsFalse(self): # Given expected = { 'foo': 'foo', 'bar': { } } actual = { 'foo': 'foo', 'bar': { 'buzz': 'baz' } } # When result = validation.matches(expected, actual) # Then self.assertFalse(result)
def test_matches_ExpectedHasNestedANY_ReturnsTrue(self): # Given expected = { 'foo': 'foo', 'bar': { 'buzz': validation.ANY } } actual = { 'foo': 'foo', 'bar': { 'buzz': 'baz' } } # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def test_matches_MatchingNestedDict_ReturnsTrue(self): # Given expected = { 'foo': { 'nested': 'buzz' }, 'bar': { 'buzz': 'baz' } } actual = { 'foo': { 'nested': 'buzz' }, 'bar': { 'buzz': 'baz' } } # When result = validation.matches(expected, actual) # Then self.assertTrue(result)
def then(self, given, result) -> bool: return validation.matches(given, result)
def value_matches_json(self, key: str, value: dict) -> bool: value_json = json.loads(self.__redis_client.get(key).decode('utf-8')) return validation.matches(value, value_json)
def object_matches_json(self, bucket_name: str, object_name: str, expected_json: dict) -> bool: data = self.__s3_client.get_object(bucket_name, object_name).data actual = json.loads(data.decode('utf-8')) return validation.matches(expected_json, actual)