Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/tests/apis/v1/forms/form_partial_update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ Feature: Form Partial Update Test
"""
When I am sending a PATCH request to /v1/forms/test/ with data.
Then The response status code is 200.
And The response JSON should equal:
"""
{
"id": 101,
"slug": "test",
"start_date": "2023-12-01T00:00:00Z",
"end_date": "2023-12-31T00:00:00Z",
"title": "test2",
"components": []
}
"""
And The response JSON should contain the following key-value pairs:
"""
{
"slug": "test",
"title": "test2"
}
"""
And The id data in the response JSON is the same as 101.
And The title data in the response JSON is the same as test2.
And It is True that a record with an ID of 101 exists in the Form model from apps.forms.models.
Expand Down
36 changes: 33 additions & 3 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def client_staff():
}


def get_nested_value(data, key_path):
keys = key_path.split(":")
for key in keys:
if isinstance(data, dict) and key in data:
data = data[key]
else:
return None
return data


@given(parsers.parse("I am a {user_type} user."), target_fixture="user")
def i_am_a_user_type_user(user_type):
if user_type == "anonymous":
Expand Down Expand Up @@ -111,14 +121,32 @@ def the_response_status_code_is_status_code(response, status_code):
assert response.status_code == status_code


@then("The response JSON should equal:")
def the_response_json_should_equal(response, docstring):
expected_json = json.loads(docstring)
actual_json = response.json()

assert actual_json == expected_json


@then("The response JSON should contain the following key-value pairs:")
def the_response_json_should_equal(response, docstring):
expected_json = json.loads(docstring)
actual_json = response.json()

for key, value in expected_json.items():
assert key in actual_json
assert actual_json[key] == value


@then(parsers.parse("The number of results in the response JSON is {number:d}."))
def the_number_of_results_in_the_response_json_is_number(response, number):
assert len(response.json()) == number


@then(parsers.parse("The {field} data in the response JSON is the same as {value}."))
def the_field_data_in_the_response_json_is_the_same_as_value(response, field, value):
assert str(response.json()[field]) == value
assert str(get_nested_value(response.json(), field)) == value


@then(parsers.parse("The {field} data in the response JSON is of type {data_type} and the same as {value}."))
Expand All @@ -132,7 +160,8 @@ def the_field_data_in_the_response_json_is_of_type_data_type_and_is_the_same_as_

@then(parsers.parse("The {field} data in the {index:d}th entry of the response JSON list is the same as {value}."))
def the_field_data_in_the_index_th_entry_of_the_response_json_list_is_the_same_as_value(response, field, index, value):
assert str(response.json()[int(index) - 1][field]) == value
data = get_nested_value(response.json()[index - 1], field)
assert str(data) == value


@then(
Expand All @@ -143,7 +172,8 @@ def the_field_data_in_the_index_th_entry_of_the_response_json_list_is_the_same_a
def the_field_data_in_the_index_th_entry_of_the_response_json_results_list_is_of_type_data_type_and_the_same_as_value(
response, field, index, value
):
assert str(response.json()[int(index) - 1][field]) == value
data = get_nested_value(response.json()[index - 1], field)
assert str(data) == value


@then(parsers.parse("It is {existance} that a record with an ID of {pk:d} exists in the {model} model from {module}."))
Expand Down
Loading