Skip to content

Commit

Permalink
fix(tests): extract data providing to fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamhexi committed Oct 16, 2024
1 parent 8b5b1a2 commit 340e788
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions tests/software/test_materials_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def server(mock_args, database_directory):
process.kill()


@pytest.fixture
def material() -> dict[str, str | list]:
"""Fixture to provide required fields to create a learning material."""
return {
'title': '123',
'paragraphs': ['123'],
'tags': ['123'],
}


def send_request(
endpoint: str,
timeout: int = 15,
Expand Down Expand Up @@ -102,7 +112,7 @@ def send_request(
return (content, response.status_code)


def test_getting_empty_database():
def test_getting_empty_database(material):
"""Test if the empty database returns no materials when requested."""
response, _ = send_request(
endpoint='materials',
Expand All @@ -112,35 +122,25 @@ def test_getting_empty_database():
assert response['message'] == ''


def test_adding_material():
def test_adding_material(material):
"""Test if a material"""
data = {
'title': '123',
'paragraphs': ['123'],
'tags': ['123'],
}

response, _ = send_request(
endpoint='materials',
method='post',
request_body=data,
request_body=material,
)

assert response['data']['material_id'], 'Material id is empty.'


def test_adding_and_removing_material():
def test_adding_and_removing_material(material):
"""Test if a material may be added then removed."""
data = {
'title': '123',
'paragraphs': ['123'],
'tags': ['123'],
}

response, _ = send_request(
endpoint='materials',
method='post',
request_body=data,
request_body=material,
)

assert response['data']['material_id'], 'Material id is empty.'
Expand All @@ -154,19 +154,13 @@ def test_adding_and_removing_material():
assert response['message']


def test_updating_material():
def test_updating_material(material):
"Test if updating an existing material succeeds."

data = {
'title': '1123',
'paragraphs': ['123'],
'tags': ['123'],
}

response, status_code = send_request(
endpoint='materials',
method='post',
request_body=data,
request_body=material,
)

assert status_code == 200, 'Adding a new material to the database failed.'
Expand All @@ -177,33 +171,34 @@ def test_updating_material():
], 'Material id is missing in the response to adding a new material to the database.'

material_id = response['data']['material_id']
data = {
new_material = {
'id': material_id,
'title': 'Totally different title!',
'paragraphs': [],
}

_, status_code = send_request(
response, status_code = send_request(
endpoint='materials',
method='PUT',
request_body=data,
request_body=new_material,
)

assert status_code == 200, 'Updating an existing material failed.'
assert (
'Updated the material with id' in response['message']
), 'Failed to update the material.'
assert (
new_material['title'] == response['data']['title']
), 'Title of the material has not been updated.'


def test_updating_non_existent_material_fails():
def test_updating_non_existent_material_fails(material):
"""Test if updating non-existent material fails."""
data = {
'title': '1123',
'paragraphs': ['123'],
'tags': ['123'],
}

_, status_code = send_request(
endpoint='materials',
method='post',
request_body=data,
request_body=material,
)

assert status_code == 200, 'Adding a new material to the database failed.'
Expand Down

0 comments on commit 340e788

Please sign in to comment.