def lists_are_equal(list_one: Union[Tuple[Any], List[Any]], list_two: Union[Tuple[Any], List[Any]], message: str='') -> bool: """lists_are_equal(list_one, list_two, message='') Assert two collections contain tbe exact same values. .. note:: The order of the values in each collection will be asserted. :param list_one: The first value. (Can be any collection type) :type list_one: Union[Tuple[Any], List[Any]] :param list_two: The second value. (Can be any collection type) :type list_two: Union[Tuple[Any], List[Any]] :param message: A custom message to include when the assertion fails. Default is Empty String. :type message: str, optional :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(list_one): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if not CommonCollectionUtils.is_collection(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if len(list_one) != len(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if isinstance(list_one, set) or isinstance(list_two, set): return list_one == list_two current_idx = 0 while current_idx < len(list_one): item_one = list_one[current_idx] item_two = list_two[current_idx] if item_one != item_two: raise AssertionError('{}: expected\n {}\n to be equal to\n {}\n Difference:\n {}\n should be\n {}\n at index {}'.format(message, list_one, list_two, item_one, item_two, current_idx)) current_idx += 1 return True
def list_contents_are_same(list_one: Union[Tuple[Any], List[Any]], list_two: Union[Tuple[Any], List[Any]], message: str='') -> bool: """list_contents_are_same(list_one, list_two, message='') Assert the values contained within two collections are the same. .. note:: The order of the values in each collection is ignored. :param list_one: The first value. (Can be any collection type) :type list_one: Union[Tuple[Any], List[Any]] :param list_two: The second value. (Can be any collection type) :type list_two: Union[Tuple[Any], List[Any]] :param message: A custom message to include when the assertion fails. Default is Empty String. :type message: str, optional :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(list_one): raise AssertionError('{}: {} is not a collection'.format(message, list_one)) if not CommonCollectionUtils.is_collection(list_two): raise AssertionError('{}: {} is not a collection'.format(message, list_two)) if len(list_one) != len(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) for item_one in list_one: if item_one not in list_two: raise AssertionError('{}: expected\n {}\n contents to be equal to\n {}\n {} not found in\n {}'.format(message, list_one, list_two, item_one, list_two)) for item_one in list_two: if item_one not in list_one: raise AssertionError('{}: expected\n {}\n contents to be equal to\n {}\n {} not found in\n {}'.format(message, list_one, list_two, item_one, list_one)) return True
def are_similar(value_one: Any, value_two: Any, message: str = '') -> bool: """are_similar(value_one, value_two, message='') Assert two values are similar. If the values are both collections, then the values contained within will be asserted to be similar. .. note:: The order of the values in each collection is ignored. :param value_one: The first value. :type value_one: Any :param value_two: The second value. :type value_two: Any :param message: A custom message to include when the assertion fails. :type message: str :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if CommonCollectionUtils.is_collection( value_one) or CommonCollectionUtils.is_collection(value_two): return CommonAssertionUtils.list_contents_are_same(value_one, value_two, message=message) if value_one != value_two: raise AssertionError( '{}: expected\n {}\n to be similar to\n {}'.format( message, value_one, value_two)) return True
def _should_merge_dictionaries_allow_duplicates_false(dictionary_one: Dict[str, Any], dictionary_two: Dict[str, Any]) -> None: result_dict = CommonCollectionUtils.merge_dict(dictionary_one, dictionary_two, allow_duplicates_in_collections=False) CommonAssertionUtils.contains(result_dict, 'a', message=pformat(result_dict)) CommonAssertionUtils.contains(result_dict, 'b', message=pformat(result_dict)) CommonAssertionUtils.contains(result_dict, 'c', message=pformat(result_dict)) CommonAssertionUtils.contains(result_dict, 'test_coll', message=pformat(result_dict)) CommonAssertionUtils.contains(result_dict, 'test_other_coll', message=pformat(result_dict)) a_val = result_dict['a'] CommonAssertionUtils.are_equal(a_val, 5, message=pformat(result_dict)) b_val = result_dict['b'] CommonAssertionUtils.are_equal(b_val, 2, message=pformat(result_dict)) c_val = result_dict['c'] CommonAssertionUtils.are_equal(c_val, 6, message=pformat(result_dict)) test_coll_val = result_dict['test_coll'] CommonAssertionUtils.contains(test_coll_val, 1, message=pformat(result_dict)) CommonAssertionUtils.contains(test_coll_val, 2, message=pformat(result_dict)) CommonAssertionUtils.contains(test_coll_val, 3, message=pformat(result_dict)) CommonAssertionUtils.contains(test_coll_val, 4, message=pformat(result_dict)) CommonAssertionUtils.contains(test_coll_val, 5, message=pformat(result_dict)) CommonAssertionUtils.contains(test_coll_val, 6, message=pformat(result_dict)) count_of_test_val = 0 for val in test_coll_val: if val == 3: count_of_test_val += 1 CommonAssertionUtils.are_equal(count_of_test_val, 1, message='The number of 3s were not correct! {}'.format(pformat(result_dict))) test_other_coll_val = result_dict['test_other_coll'] CommonAssertionUtils.contains(test_other_coll_val, 24, message=pformat(result_dict)) CommonAssertionUtils.contains(test_other_coll_val, 25, message=pformat(result_dict))
def has_length(value: Union[Tuple[Any], List[Any]], expected_length: int, message: str = '') -> bool: """has_length(value, expected_length, message='') Assert a collection has the specified length. :param value: The collection being asserted. (Any collection that works with `len()` can be used) :type value: Union[Tuple[Any], List[Any]] :param expected_length: The length expected of the collection. :type expected_length: int :param message: A custom message to include when the assertion fails. :type message: str :return: True if the length matches. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(value): raise AssertionError( '{}: expected collection {} to have length {}, but was not a collection' .format(message, value, expected_length)) if len(value) != expected_length: raise AssertionError( '{}: expected collection {} to have length {}, but was {}'. format(message, value, expected_length, len(value))) return True
def get_full_stack_trace() -> List[str]: """ Retrieve the full stacktrace from the current stack. :return: A collection of stack trace strings. """ exception_info = CommonStacktraceUtil.full_exception_info() if CommonCollectionUtils.is_collection(exception_info): exceptions = traceback.format_exception(*exception_info) else: exceptions = traceback.format_stack() return exceptions
def are_equal(value_one: Any, value_two: Any, message: str = '') -> bool: """ Assert the two values are equal to each other. If the values are both collections, then the values contained within will be asserted to be equal. Note: The order of the values in each collection is asserted. :param value_one: The first value. :param value_two: The second value. :param message: A custom message to include when the assertion fails. :return: True if the assertion succeeds. :exception AssertionError when the assertion fails. """ if CommonCollectionUtils.is_collection( value_one) or CommonCollectionUtils.is_collection(value_two): return CommonAssertionUtils.lists_are_equal(value_one, value_two, message=message) if value_one != value_two: raise AssertionError( '{}: expected\n {}\n to be equal to\n {}'.format( message, value_one, value_two)) return True
def has_length(value, expected_length: int, message: str = '') -> bool: """ Assert a collection has the specified length. :param expected_length: The length expected of the collection. :param value: The collection being asserted. :param message: A custom message to include when the assertion fails. :return: True if the length matches. :exception AssertionError when the assertion fails. """ if not CommonCollectionUtils.is_collection(value): raise AssertionError( '{}: expected collection {} to have length {}, but was not a collection' .format(message, value, expected_length)) if len(value) != expected_length: raise AssertionError( '{}: expected collection {} to have length {}, but was {}'. format(message, value, expected_length, len(value))) return True
def should_combine(items: List[int], combination_length: int, expected_outcome: Set[Tuple[int]]): result = CommonCollectionUtils.create_possible_combinations( items, combination_length) CommonAssertionUtils.are_equal(result, expected_outcome)
def should_intersect_false(list_one: List[int], *list_items: int): result = CommonCollectionUtils.intersects(list_one, *list_items) CommonAssertionUtils.is_false(result)
def should_intersect_true(list_one, *list_items): result = CommonCollectionUtils.intersects(list_one, *list_items) CommonAssertionUtils.is_true(result)