def test_assert_dicts_nested_dicts(): expected = {"foo": "bar", "fizz": {"foo": "bar"}} actual = {"foo": "bar", "fizz": {"foo": "bar", "fizz": "buzz"}} passed, _ = asserts.assert_dicts(expected, actual) assert passed
def test_assert_dicts_not_contains(): expected = {"foo": "bar", "fizz": "buzz"} actual = {"foo": "bar"} passed, _ = asserts.assert_dicts(expected, actual) assert not passed
def test_assert_dicts_equal(): expected = {"foo": "bar"} actual = {"foo": "bar"} passed, _ = asserts.assert_dicts(expected, actual, all_required=True) assert passed
def run_assert(assert_type: str, params: AssertParams) -> AssertResult: params_problems = assert_params_problems(params) if params_problems: raise ValueError(f"Params invalid: {', '.join(params_problems)}") action_response = run_action(params["method"], params["actionParams"]) expected = params["expected"] if assert_type == "Headers": actual = action_response["headers"] passed, description = assert_dicts( expected=expected, actual=actual, all_required=params.get("allRequired", False), ) elif assert_type == "JSON": actual = action_response["body"] passed, description = assert_dicts( expected=expected, actual=actual, all_required=params.get("allRequired", False), ) elif assert_type == "StatusCode": actual = action_response["status_code"] passed = expected == actual if not passed: description = f"expected status code {expected}, got {actual}" else: description = "passed" else: raise ValueError(f"Assert type {assert_type} is invalid") return AssertResult( passed=passed, expected=str(expected), actual=str(actual), description=description, )
def equals_rows(expected_rows: List[dict], actual_rows: List[dict]) -> Tuple[bool, str]: if len(expected_rows) != len(actual_rows): description = f"Expected {len(expected_rows)} rows, got {len(actual_rows)}" return False, description for expected_row, actual_row in zip(expected_rows, actual_rows): row_matches, description = assert_dicts(expected_row, actual_row, all_required=True) if not row_matches: return False, description return True, "passed"
def run_assert(assert_type: str, params: AssertParams) -> AssertResult: if assert_type == "FindMessage": messages = run_action("Receive", params["actionParams"]) for message in messages["messages_received"]: if assert_dicts(params["expected"], message): return AssertResult( actual=str(message), expected=str(params["expected"]), passed=True, description="passed", ) return AssertResult( actual=None, expected=str(params["expected"]), passed=False, description=f"No message found matching {params['expected']}", ) raise ValueError(f"Assert type {assert_type} is invalid")
def contains_rows(expected_rows: List[dict], actual_rows: List[dict]) -> Tuple[bool, str]: remaining_rows = expected_rows[:] for actual_row in actual_rows: for i, expected_row in enumerate(remaining_rows): row_matches, _ = assert_dicts(expected_row, actual_row, all_required=False) if row_matches: del remaining_rows[i] if not remaining_rows: passed = True description = "passed" else: passed = False # NOTE: may need better formatting description = f"The following rows did not match any returned: {remaining_rows}" return passed, description