Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #71

Closed
EduardSchwarzkopf opened this issue Feb 24, 2024 · 2 comments
Closed

Improve tests #71

EduardSchwarzkopf opened this issue Feb 24, 2024 · 2 comments
Assignees
Labels

Comments

@EduardSchwarzkopf
Copy link
Owner

Consider the async with blocks that are present in each test function. You're performing the same POST request in these blocks within a number of functions (test_create_user, test_invalid_create_user, test_login, test_invalid_login, etc).

First, you might create a helper function that handles these POST requests:

async def make_post_request(session, client, url, json_data):
    async with session:
        response = await client.post(url, json=json_data)
    return response

Now you can use make_post_request in your tests:

async def test_create_user(
    client_session_wrapper_fixture: ClientSessionWrapper,
    username,
    displayname,
    password,
):
    data = {
        "email": username,
        "password": password,
        "displayname": displayname,
    }
    response = await make_post_request(
        client_session_wrapper_fixture.session, 
        client_session_wrapper_fixture.client, 
        f"{endpoint}/register",
        data,
    )
    assert res.status_code == 201

    ...

The creation of a new user is a good candidate for a helper function because it's a multi-step process that you're likely doing in multiple places within your tests. Here's what a helper function for that could look like:

async def create_test_user(session, client, username, displayname, password):
    data = {
        "email": username,
        "password": password,
        "displayname": displayname,
    }
    return await make_post_request(session, client, f"{endpoint}/register", data)

Now, the test_create_user function becomes:

async def test_create_user(
    client_session_wrapper_fixture: ClientSessionWrapper,
    username,
    displayname,
    password,
):
    response = await create_test_user(
        client_session_wrapper_fixture.session, 
        client_session_wrapper_fixture.client, 
        username, 
        displayname, 
        password,
    )
    assert response.status_code == 201

    ...

This kind of refactoring helps to keep your test code DRY and makes your tests less complex and more readable.

@EduardSchwarzkopf
Copy link
Owner Author

Another problem is the current setup of the session. On every test, the database is being created and thrown out. This needs to be improved with a session scope. Most likely I will need to update the fixtures to handle existing data in the database

@EduardSchwarzkopf
Copy link
Owner Author

this is done with #76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant