Skip to main content
In this article, we set up and run tests for the voting functionality in your application. Our tests cover various scenarios including successful voting, duplicate vote attempts, vote deletions, and edge cases such as voting on non-existent posts or by unauthorized users. Before running these tests, ensure you have an authorized client and properly configured test posts in your test environment.

Retrieving and Validating Posts

The initial test function retrieves all posts and validates them using the defined schema. This ensures that the posts returned by the API match the expected structure.
Console Output:
Save this file as test_votes.py.

Testing a Successful Vote

For the successful vote test, we simulate voting on a post. Initially, the function prototype is set as follows:
After setting up the endpoint URL and required data (post ID and vote direction), we update the test to include the correct post ID:
Since a vote direction is needed (with 1 representing a like/upvote), the function is modified accordingly:
In our final version, we simulate voting on a post owned by another user by selecting the fourth post from the list:
Console Output after running:
Running the command:
yields:

Testing Duplicate Voting

Duplicate voting is an important scenario where a user attempts to vote on a post twice. First, a fixture is used to set up the initial vote:
The test case for duplicate voting validates that the second vote attempt returns a 409 status code:

Testing Vote Deletion

Vote deletion is triggered by setting the vote direction to 0. This function sends the deletion request and asserts a successful deletion:
To ensure proper error handling, the following test verifies that deleting a non-existent vote returns a 404 status code. Here, the test_vote fixture is not used so no vote exists for the given post:

Testing Voting on a Non-Existent Post

When a user attempts to vote on a post that does not exist (using an ID that isn’t in the database), the API should return a 404 status code. The following test case ensures this behavior:

Testing Unauthorized Voting

To verify security measures, the final voting test checks that an unauthorized user (i.e., using an unauthenticated client) cannot vote. This test should return a 401 status code:
Console output when running these tests might display warnings such as:

Additional Tests for Bank Account Operations

Although not directly related to voting tests, the repository also includes tests for bank account operations. An example from the bank account test file is given below:
The output confirms that the tests pass:

Final Notes

Before deleting the database.py file, ensure that all tests pass by running your complete test suite. The majority of functionality has now been moved to conftest.py.
An example of the test_posts fixture used throughout the tests is shown below:
When writing tests, consider additional scenarios that could potentially break the application by creating individual test cases for each situation. Comprehensive testing is essential for ensuring the stability and reliability of your application.

By following this guide, you have now set up a robust test suite covering various edge cases and scenarios for voting and other functionality. This comprehensive approach not only helps in maintaining functionality but also enhances the resilience of your application.

Watch Video