Skip to content

Commit

Permalink
test(restapi): Added description field to tests for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhand committed May 17, 2024
1 parent d6b7e43 commit 415ae6c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
8 changes: 5 additions & 3 deletions tests/unit/restapi/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
API endpoints.
"""

from typing import Any
from flask.testing import FlaskClient
from werkzeug.test import TestResponse

Expand Down Expand Up @@ -108,6 +109,7 @@ def register_tag(
client: FlaskClient,
name: str,
group_id: int,
description: str | None = None,
) -> TestResponse:
"""Register a tag using the API.
Expand All @@ -121,10 +123,10 @@ def register_tag(
The response from the API.
"""

payload: dict[str, Any] = {"name": name}
payload = {"name": name, "group_id": group_id}

if group_id:
payload["group_id"] = group_id
if description is not None:
payload["description"] = description

return client.post(
f"/{V1_ROOT}/{V1_TAGS_ROUTE}/",
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/restapi/v1/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,20 @@ def registered_tags(
) -> dict[str, Any]:
tag1_response = actions.register_tag(
client,
name="ML: Object Recognition",
name="tag_one"
description="The first tag.",
group_id=auth_account["default_group_id"],
).get_json()
tag2_response = actions.register_tag(
client,
name="ML: Patch Attack",
name="tag_two"
description="The second tag.",
group_id=auth_account["default_group_id"],
).get_json()
tag3_response = actions.register_tag(
client,
name="AI",
name="tag_three"
description="Not retrieved.",
group_id=auth_account["default_group_id"],
).get_json()
return {
Expand Down
53 changes: 34 additions & 19 deletions tests/unit/restapi/v1/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
# -- Actions ---------------------------------------------------------------------------


def rename_tag(
def modify_tag(
client: FlaskClient,
tag_id: int,
new_name: str,
new_description: str,
) -> TestResponse:
"""Rename a tag using the API.
Expand All @@ -49,7 +50,7 @@ def rename_tag(
Returns:
The response from the API.
"""
payload = {"name": new_name}
payload: dict[str, Any] = {"name": new_name, "description": new_description}

return client.put(
f"/{V1_ROOT}/{V1_TAGS_ROUTE}/{tag_id}",
Expand Down Expand Up @@ -91,16 +92,19 @@ def assert_tag_response_contents_matches_expectations(
"createdOn",
"lastModifiedOn",
"name",
"description",
}
assert set(response.keys()) == expected_keys

# Validate the non-Ref fields
assert isinstance(response["id"], int)
assert isinstance(response["name"], str)
assert isinstance(response["description"], str)
assert isinstance(response["createdOn"], str)
assert isinstance(response["lastModifiedOn"], str)

assert response["name"] == expected_contents["name"]
assert response["description"] == expected_contents["description"]

assert helpers.is_iso_format(response["createdOn"])
assert helpers.is_iso_format(response["lastModifiedOn"])
Expand Down Expand Up @@ -240,6 +244,7 @@ def assert_cannot_rename_tag_with_existing_name(
client: FlaskClient,
tag_id: int,
existing_name: str,
existing_description: str,
) -> None:
"""Assert that renaming a tag with an existing name fails.
Expand All @@ -255,6 +260,7 @@ def assert_cannot_rename_tag_with_existing_name(
client=client,
tag_id=tag_id,
new_name=existing_name,
new_description=existing_description,
)
assert response.status_code == 400

Expand All @@ -279,23 +285,26 @@ def test_create_tag(
during registration.
"""
name = "tag"

description = "The first tag."
user_id = auth_account["default_group_id"]
group_id = auth_account["default_group_id"]
tag_response = actions.register_tag(
client,
name=name,
group_id=auth_account["default_group_id"],
description=description,
group_id=group_id,
)
tag_expected = tag_response.get_json()

assert_tag_response_contents_matches_expectations(
response=tag_expected,
expected_contents={
"name": name,
"user_id": auth_account["user_id"],
"group_id": auth_account["default_group_id"],
"description": description,
"user_id": user_id,
"group_id": group_id,
},
)

assert_retrieving_tag_by_id_works(
client, tag_id=tag_expected["id"], expected=tag_expected
)
Expand All @@ -313,8 +322,8 @@ def test_tag_search_query(
This test validates the following sequence of actions:
- A user registers three tags: "tag1", "tag2", "tag3".
- The user is able to retrieve a list of all registered tags with the string "ML"
contained within the name.
- The user is able to retrieve a list of all registered tags with the string "tag"
contained within the description.
- In all cases, the returned information matches the information that was provided
during registration.
"""
Expand All @@ -325,7 +334,7 @@ def test_tag_search_query(
assert_retrieving_all_tags_works(
client,
expected=tag_expected_list,
search="name:*ML*",
search="description:*tag*",
)


Expand Down Expand Up @@ -368,18 +377,24 @@ def test_rename_tag(
- The user retrieves information about the same tag and it reflects the name
change.
"""
tag1 = registered_tags["tag1"]
new_name = "new_name"

rename_tag(client, tag_id=tag1["id"], new_name=new_name)
updated_tag_name = "new_name"
tag_to_rename = registered_tags["tag1"]
existing_tag = registered_tags["tag2"]

modify_tag(
client,
tag_id=tag_to_rename["id"],
new_name=updated_tag_name,
new_description=tag_to_rename["description"],
)
assert_tag_name_matches_expected_name(
client, tag_id=tag1["id"], expected_name=new_name
client, tag_id=tag_to_rename["id"], expected_name=updated_tag_name
)

tag2 = registered_tags["tag2"]

assert_cannot_rename_tag_with_existing_name(
client, tag_id=tag1["id"], existing_name=tag2["name"]
client,
tag_id=tag_to_rename["id"],
existing_name=existing_tag["name"],
existing_description=tag_to_rename["description"],
)


Expand Down

0 comments on commit 415ae6c

Please sign in to comment.