Skip to content

Commit

Permalink
fix(matchers): Don't sort failed matches when printing error message
Browse files Browse the repository at this point in the history
Fixes GH-704
  • Loading branch information
mgaligniana committed Apr 26, 2024
1 parent 3478dcf commit 46b0e11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions responses/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def list_to_str(input_list: List[str]) -> str:
Function is called recursively for nested lists
"""
converted_list = []
for item in sorted(input_list, key=lambda x: str(x)):
for item in input_list:
if isinstance(item, dict):
item = _create_key_val_str(item)
elif isinstance(item, list):
Expand All @@ -44,7 +44,7 @@ def list_to_str(input_list: List[str]) -> str:
return "[" + list_str + "]"

items_list = []
for key in sorted(input_dict.keys(), key=lambda x: str(x)):
for key in input_dict.keys():
val = input_dict[key]
if isinstance(val, dict):
val = _create_key_val_str(val)
Expand Down
17 changes: 14 additions & 3 deletions responses/tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,24 @@ def run():
)
assert (
"- POST http://example.com/ request.body doesn't match: "
"{page: {type: json}} doesn't match {page: {diff: value, type: json}}"
"{page: {type: json}} doesn't match {page: {type: json, diff: value}}"
) in str(exc.value)

run()
assert_reset()


def test_failed_matchers_dont_sort_inputs_in_error_message():
json_a = {"array": ["C", "B", "A"]}
json_b = '{"array" : ["B", "A", "C"]}'
mock_request = Mock(body=json_b)
result = matchers.json_params_matcher(json_a)(mock_request)
assert result == (
False,
"request.body doesn't match: {array: [B, A, C]} doesn't match {array: [C, B, A]}",
)


def test_json_params_matcher_json_list():
json_a = [{"a": "b"}]
json_b = '[{"a": "b", "c": "d"}]'
Expand Down Expand Up @@ -811,8 +822,8 @@ def test_matchers_create_key_val_str():
}
conv_str = matchers._create_key_val_str(data)
reference = (
"{1: 4, high: {nested: nested_dict}, my_list: [!, 1, 2, [[list, nested], {nested: dict}], "
"a, {3: test, key1: val1, key2: 2}], test: val}"
"{my_list: [1, 2, a, {key1: val1, key2: 2, 3: test}, !, [[list, nested], {nested: dict}]], "
"1: 4, test: val, high: {nested: nested_dict}}"
)
assert conv_str == reference

Expand Down

0 comments on commit 46b0e11

Please sign in to comment.