In this lesson, we build and test a RESTful API endpoint for retrieving a product from the database. The walkthrough covers creating the products table, implementing helper functions to clear the table and add products, and writing a test to ensure the GET API functions as expected.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Creating the Products Table
ThecreateTable method defines a SQL query to create the table if it does not already exist. The table includes an auto-incrementing id, a name, a quantity, and a price, with the id set as the primary key.
Implementing Test Helpers
To prepare our test environment, we include helper functions for clearing data from the table and inserting products.Clear Table
Before testing the GET API, we clear the content of the products table. As theid field is auto-incremented, we reset the counter to ensure that a new product receives an ID of 1.
Adding a Product
TheaddProduct helper method inserts a new product into the database. It accepts parameters for the product’s name, quantity, and price.
Testing the GET API
The following test function,TestGetProduct, demonstrates the end-to-end process of verifying the GET API:
- Clear the products table.
- Add a new product to the database.
- Create an HTTP GET request to retrieve the product with ID 1.
- Use the helper function
sendRequestto dispatch the request to our router. - Confirm that the API returns a status code of 200 (HTTP OK).
Helper: Sending a Request
ThesendRequest function leverages the HTTP test package to record the response, invoking the router’s ServeHTTP method with the test request.
Helper: Checking the Response Status Code
ThecheckStatusCode function validates that the actual HTTP status code matches the expected one. If they differ, an error is logged through the testing framework.
Routing Configuration
Our router is configured to associate URL endpoints with their corresponding handler functions. This mapping ensures that the correct handler processes each request.ServeHTTP method dispatches incoming requests to the appropriate handlers. Route variables can be accessed using mux.Vars(request) when needed.
Test Suite Setup
Before running the tests, we initialise the application and create the products table in the test’s main function. Ensure that the specified database (“test”) exists. If the database is missing, use the following instructions:If you encounter an error such as
Error 1049: Unknown database 'test', log into MySQL and create the database manually.Verifying the Test
After runninggo test, verify that the test passes with an HTTP status code of 200. Checking the database table should reveal that the auto-increment counter resets correctly, assigning an ID of 1 to a newly inserted product after clearing the table.
A successful test run confirms that the GET API functions as intended and that the database setup and routing configurations are correct.