This article demonstrates how to test product API endpoints using various HTTP methods for create, update, and delete operations.
In this article, we demonstrate how to test your product API endpoints—including create, update, and delete—using various HTTP methods. The sections below detail the implementation of the API routes and the corresponding test functions, ensuring a comprehensive and self-contained guide.
Before delving into individual test cases, note that each test function begins by clearing the database table and adding a sample product. This approach ensures that tests are isolated and reproducible.
The following test function outlines the deletion process. Initially, a product is added and verified via a GET request. The product is then deleted using a DELETE request, and a final GET request confirms the deletion.
This section details the procedure for testing the update functionality. The test retrieves the original product details, performs a PUT request to update the product, and finally compares the old and new values to confirm that only the desired changes were made.
Copy
Ask AI
func TestUpdateProduct(t *testing.T) { clearTable() addProduct("connector", 10, 10) // Retrieve the original product. req, _ := http.NewRequest("GET", "/product/1", nil) response := sendRequest(req) checkStatusCode(t, http.StatusOK, response.Code) var oldValue map[string]interface{} json.Unmarshal(response.Body.Bytes(), &oldValue) // Prepare updated product payload; here, the quantity is changed while keeping the name consistent. product := []byte(`{"name":"connector", "quantity":1, "price":10}`) req, _ = http.NewRequest("PUT", "/product/1", bytes.NewBuffer(product)) req.Header.Set("Content-Type", "application/json") response = sendRequest(req) var newValue map[string]interface{} json.Unmarshal(response.Body.Bytes(), &newValue) // Verify that the product ID remains unchanged. if oldValue["id"] != newValue["id"] { t.Errorf("Expected id: %v, Got: %v", oldValue["id"], newValue["id"]) } // Validate updated fields. if newValue["name"] != "connector" { t.Errorf("Expected name: connector, Got: %v", newValue["name"]) } if newValue["price"] != float64(10) { t.Errorf("Expected price: 10, Got: %v", newValue["price"]) } // Confirm that the quantity has been updated. if oldValue["quantity"] == newValue["quantity"] { t.Errorf("Expected quantity to change from %v, but got %v", oldValue["quantity"], newValue["quantity"]) }}
After updating the product, the test compares the original values with the updated ones. For instance, if the new quantity remains unchanged (i.e., still 10 instead of the expected 1), an error is triggered:
This test case demonstrates the product creation process. A POST request helps to create a new product, and the response is unmarshaled to verify that the product properties match the expected values.
A common helper function is used to send HTTP requests to the application’s router. This function is essential for abstracting the request process in the tests.
Error messages like the one above indicate potential issues in your update logic and should be addressed promptly to ensure your API functions as expected.
To ensure the robustness of your API, consider adding the following test cases:
Test Case
Description
Non-existent Product Retrieval
Test retrieving a product that does not exist.
Get All Products
Test the endpoint to retrieve all products after multiple additions.
Error Handling for Incorrect Data Types
Test posting data with wrong data types to check proper error responses.
Deleting a Non-existent Product
Test DELETE requests for a product that does not exist.
Expanding your test suite with these scenarios will help ensure your API reliably handles various edge cases.By following this guide and implementing comprehensive tests, you can confidently develop and maintain your product inventory application, ensuring that it behaves as expected across all CRUD operations.