-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b087de4
commit 0930342
Showing
4 changed files
with
217 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,125 @@ | ||
package tests | ||
|
||
import ( | ||
"io" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/GenerateNU/sac/backend/src/models" | ||
"github.com/GenerateNU/sac/backend/src/transactions" | ||
"github.com/huandu/go-assert" | ||
|
||
"github.com/goccy/go-json" | ||
) | ||
|
||
func TestCreateCategoryWorks(t *testing.T) { | ||
app, assert, resp := RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{ | ||
"category_name": "Science", | ||
}, nil, nil, nil) | ||
defer app.DropDB() | ||
func CreateSampleCategory(t *testing.T, categoryName string) ExistingAppAssert { | ||
return TestRequest{ | ||
Method: "POST", | ||
Path: "/api/v1/categories/", | ||
Body: &map[string]interface{}{ | ||
"category_name": categoryName, | ||
}, | ||
}.TestOnStatusAndDBKeepDB(t, nil, | ||
DBTesterWithStatus{ | ||
Status: 201, | ||
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
|
||
assert.Equal(201, resp.StatusCode) | ||
var respCategory models.Category | ||
|
||
var respCategory models.Category | ||
err := json.NewDecoder(resp.Body).Decode(&respCategory) | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&respCategory) | ||
assert.NilError(err) | ||
|
||
assert.NilError(err) | ||
dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID) | ||
|
||
dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID) | ||
assert.NilError(err) | ||
|
||
assert.NilError(err) | ||
assert.Equal(dbCategory, &respCategory) | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
assert.Equal(dbCategory, &respCategory) | ||
func TestCreateCategoryWorks(t *testing.T) { | ||
appAssert := CreateSampleCategory(t, "Science") | ||
appAssert.App.DropDB() | ||
} | ||
|
||
func TestCreateCategoryIgnoresid(t *testing.T) { | ||
app, assert, resp := RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{ | ||
"id": 12, | ||
"category_name": "Science", | ||
}, nil, nil, nil) | ||
defer app.DropDB() | ||
|
||
var respCategory models.Category | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&respCategory) | ||
|
||
assert.NilError(err) | ||
|
||
dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID) | ||
|
||
assert.NilError(err) | ||
|
||
assert.NotEqual(12, dbCategory.ID) | ||
TestRequest{ | ||
Method: "POST", | ||
Path: "/api/v1/categories/", | ||
Body: &map[string]interface{}{ | ||
"id": 12, | ||
"category_name": "Science", | ||
}, | ||
}.TestOnStatusAndDB(t, nil, | ||
DBTesterWithStatus{ | ||
Status: 201, | ||
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
var respCategory models.Category | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&respCategory) | ||
|
||
assert.NilError(err) | ||
|
||
dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID) | ||
|
||
assert.NilError(err) | ||
|
||
assert.NotEqual(12, dbCategory.ID) | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func TestCreateCategoryFailsIfNameIsNotString(t *testing.T) { | ||
app, assert, resp := RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{ | ||
"category_name": 1231, | ||
}, nil, nil, nil) | ||
defer app.DropDB() | ||
|
||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
|
||
assert.NilError(err) | ||
|
||
msg := string(bodyBytes) | ||
|
||
assert.Equal("failed to process the request", msg) | ||
|
||
assert.Equal(400, resp.StatusCode) | ||
TestRequest{ | ||
Method: "POST", | ||
Path: "/api/v1/categories/", | ||
Body: &map[string]interface{}{ | ||
"category_name": 1231, | ||
}, | ||
}.TestOnStatusAndMessage(t, nil, | ||
MessageWithStatus{ | ||
Status: 400, | ||
Message: "failed to process the request", | ||
}) | ||
} | ||
|
||
func TestCreateCategoryFailsIfNameIsMissing(t *testing.T) { | ||
app, assert, resp := RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{}, nil, nil, nil) | ||
defer app.DropDB() | ||
|
||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
|
||
assert.NilError(err) | ||
|
||
msg := string(bodyBytes) | ||
|
||
assert.Equal("failed to validate the data", msg) | ||
|
||
assert.Equal(400, resp.StatusCode) | ||
TestRequest{ | ||
Method: "POST", | ||
Path: "/api/v1/categories/", | ||
Body: &map[string]interface{}{}, | ||
}.TestOnStatusAndMessage(t, nil, | ||
MessageWithStatus{ | ||
Status: 400, | ||
Message: "failed to validate the data", | ||
}, | ||
) | ||
} | ||
|
||
func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) { | ||
categoryName := "Science" | ||
app, assert, resp := RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{ | ||
"category_name": categoryName, | ||
}, nil, nil, nil) | ||
|
||
assert.Equal(201, resp.StatusCode) | ||
|
||
var respCategory models.Category | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&respCategory) | ||
|
||
assert.NilError(err) | ||
|
||
dbCategory, err := transactions.GetCategory(app.Conn, respCategory.ID) | ||
|
||
assert.NilError(err) | ||
|
||
assert.Equal(dbCategory, &respCategory) | ||
existingAppAssert := CreateSampleCategory(t, categoryName) | ||
|
||
for _, permutation := range AllCasingPermutations(categoryName) { | ||
_, _, resp = RequestTesterWithJSONBody(t, "POST", "/api/v1/categories/", &map[string]interface{}{ | ||
"category_name": permutation, | ||
}, nil, &app, assert) | ||
|
||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
|
||
assert.NilError(err) | ||
|
||
msg := string(bodyBytes) | ||
|
||
assert.Equal("category with that name already exists", msg) | ||
|
||
assert.Equal(400, resp.StatusCode) | ||
fmt.Println(permutation) | ||
TestRequest{ | ||
Method: "POST", | ||
Path: "/api/v1/categories/", | ||
Body: &map[string]interface{}{ | ||
"category_name": permutation, | ||
}, | ||
}.TestOnStatusAndMessageKeepDB(t, &existingAppAssert, | ||
MessageWithStatus{ | ||
Status: 400, | ||
Message: "category with that name already exists", | ||
}, | ||
) | ||
} | ||
|
||
existingAppAssert.App.DropDB() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.