Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Testing Docs #56

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading