Skip to content

Commit

Permalink
Update Testing Docs (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Jan 24, 2024
1 parent 70751aa commit 7e7b4e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
71 changes: 35 additions & 36 deletions backend/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Here is an example of creating a `TestRequest`, notice how instead of saying `He

```go
TestRequest{
Method: "POST",
Method: fiber.MethodPost,
Path: "/api/v1/tags/",
Body: &map[string]interface{}{
"name": tagName,
Expand All @@ -38,9 +38,9 @@ Say you want to test hitting the `[APP_ADDRESS]/health` endpoint with a GET requ

```go
TestRequest{
Method: "GET",
Method: fiber.MethodGet,
Path: "/health",
}.TestOnStatus(t, nil, 200).Close()
}.TestOnStatus(t, nil, fiber.StatusOK).Close()
```

## Testing that a Request Returns a XXX Status Code and Assert Something About the Database
Expand All @@ -49,15 +49,13 @@ Say you want to test that a creating a catgory with POST `[APP_ADDRESS]/api/v1/c

```go
TestRequest{
Method: "POST",
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Body: &map[string]interface{}{
"category_name": categoryName,
},
Body: SampleCategoryFactory(),
}.TestOnStatusAndDB(t, nil,
DBTesterWithStatus{
Status: 201,
DBTester: AssertRespCategorySameAsDBCategory,
Status: fiber.StatusCreated,
DBTester: AssertSampleCategoryBodyRespDB,
},
).Close()
```
Expand All @@ -67,18 +65,27 @@ TestRequest{
Often times there are common assertions you want to make about the database, for example, if the object in the response is the same as the object in the database. We can create a lambda function that takes in the `TestApp`, `*assert.A`, and `*http.Response` and makes the assertions we want. We can then pass this function to the `DBTesterWithStatus` struct.

```go
var AssertRespCategorySameAsDBCategory = func(app TestApp, assert *assert.A, resp *http.Response) {
func AssertSampleCategoryBodyRespDB(app TestApp, assert *assert.A, resp *http.Response) {
AssertCategoryWithIDBodyRespDB(app, assert, resp, 1, SampleCategoryFactory())
}

func AssertCategoryWithIDBodyRespDB(app TestApp, assert *assert.A, resp *http.Response, id uint, body *map[string]interface{}) {
var respCategory models.Category

err := json.NewDecoder(resp.Body).Decode(&respCategory)

assert.NilError(err)

dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID)
var dbCategory models.Category

err = app.Conn.First(&dbCategory, id).Error

assert.NilError(err)

assert.Equal(dbCategory, &respCategory)
assert.Equal(dbCategory.ID, respCategory.ID)
assert.Equal(dbCategory.Name, respCategory.Name)

assert.Equal((*body)["name"].(string), dbCategory.Name)
}
```

Expand All @@ -89,30 +96,25 @@ Since the test suite creates a new database for each test, we can have a determi
Consider this example, to create a tag, we need to create a category first. This is a multi step test, so we need to use `ExistingAppAssert` to keep the database state from the previous step.

```go
appAssert := TestRequest{
Method: "POST",
TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Body: &map[string]interface{}{
"category_name": categoryName,
},
Body: SampleCategoryFactory(),
}.TestOnStatusAndDB(t, nil,
DBTesterWithStatus{
Status: 201,
DBTester: AssertRespCategorySameAsDBCategory,
Status: fiber.StatusCreated,
DBTester: AssertSampleCategoryBodyRespDB,
},
)

TestRequest{
Method: "POST",
Method: fiber.MethodPost,
Path: "/api/v1/tags/",
Body: &map[string]interface{}{
"name": tagName,
"category_id": 1,
},
Body: SampleTagFactory(),
}.TestOnStatusAndDB(t, &appAssert,
DBTesterWithStatus{
Status: 201,
DBTester: AssertRespTagSameAsDBTag,
Status: fiber.StatusCreated,
DBTester: AssertSampleTagBodyRespDB,
},
).Close()
```
Expand All @@ -121,23 +123,20 @@ TestRequest{

This closes the connection to the database. This is important because if you don't close the connection, we will run out of available connections and the tests will fail. **Call this on the last test request of a test**

## Testing that a Request Returns a XXX Status Code, Assert Something About the Message, and Assert Something About the Database
## Testing that a Request Returns the Correct Error (Status Code and Message), and Assert Something About the Database

Say you want to test a bad request to POST `[APP_ADDRESS]/api/v1/categories/` endpoint returns a `400` status code, the message is `failed to process the request`, and that a category was not created.
Say you want to test a bad request to POST `[APP_ADDRESS]/api/v1/categories/` endpoint returns a `400` status code, the message is `failed to process the request`, and that a category was not created. We can leverage our errors defined in the error package to do this!

```go
TestRequest{
Method: "POST",
TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Body: &map[string]interface{}{
"category_name": 1231,
"name": 1231,
},
}.TestOnStatusMessageAndDB(t, nil,
StatusMessageDBTester{
MessageWithStatus: MessageWithStatus{
Status: 400,
Message: "failed to process the request",
},
ErrorWithDBTester{
Error: errors.FailedToParseRequestBody,
DBTester: AssertNoCategories,
},
).Close()
Expand Down
5 changes: 1 addition & 4 deletions backend/tests/api/category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"fmt"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -34,13 +33,11 @@ func AssertCategoryWithIDBodyRespDB(app TestApp, assert *assert.A, resp *http.Re
err = app.Conn.First(&dbCategory, id).Error

assert.NilError(err)
x, _ := io.ReadAll(resp.Body)
fmt.Println(string(x))

assert.Equal(dbCategory.ID, respCategory.ID)
assert.Equal(dbCategory.Name, respCategory.Name)

assert.Equal((*body)["name"].(string), dbCategory.Name)

}

func AssertSampleCategoryBodyRespDB(app TestApp, assert *assert.A, resp *http.Response) {
Expand Down

0 comments on commit 7e7b4e3

Please sign in to comment.