Skip to main content
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.

Creating the Products Table

The createTable 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.
This function is invoked from our test’s main method to guarantee that the table exists before any tests are executed.

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 the id field is auto-incremented, we reset the counter to ensure that a new product receives an ID of 1.

Adding a Product

The addProduct 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:
  1. Clear the products table.
  2. Add a new product to the database.
  3. Create an HTTP GET request to retrieve the product with ID 1.
  4. Use the helper function sendRequest to dispatch the request to our router.
  5. Confirm that the API returns a status code of 200 (HTTP OK).

Helper: Sending a Request

The sendRequest 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

The checkStatusCode 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.
The Gorilla Mux package’s 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.
Execute in the terminal:
And within the MySQL prompt:
Below is the test main function demonstrating the initialisation:
Run the test suite with:

Verifying the Test

After running go 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.

Next Steps

With the GET API successfully working and tested, proceed to implement and test the POST API and other endpoints using a similar approach. Happy coding!

Watch Video