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

Testing Refactor/Rename #167

Merged
merged 1 commit into from
Feb 7, 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
2 changes: 1 addition & 1 deletion backend/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Body: SampleCategoryFactory(),
}.TestOnStatusAndDB(t, nil,
}.TestOnStatusAndTester(t, nil,
TesterWithStatus{
Status: fiber.StatusCreated,
DBTester: AssertSampleCategoryBodyRespDB,
Expand Down
35 changes: 17 additions & 18 deletions backend/tests/api/category_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@ import (
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/huandu/go-assert"
)

func AssertTagsWithBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response, body *[]map[string]interface{}) []uuid.UUID {
func AssertTagsWithBodyRespDB(eaa h.ExistingAppAssert, resp *http.Response, body *[]map[string]interface{}) []uuid.UUID {
var respTags []models.Tag

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

assert.NilError(err)
eaa.Assert.NilError(err)

var dbTags []models.Tag

err = app.Conn.Find(&dbTags).Error
err = eaa.App.Conn.Find(&dbTags).Error

assert.NilError(err)
eaa.Assert.NilError(err)

assert.Equal(len(dbTags), len(respTags))
eaa.Assert.Equal(len(dbTags), len(respTags))

for i, dbTag := range dbTags {
assert.Equal(dbTag.ID, respTags[i].ID)
assert.Equal(dbTag.Name, respTags[i].Name)
assert.Equal(dbTag.CategoryID, respTags[i].CategoryID)
eaa.Assert.Equal(dbTag.ID, respTags[i].ID)
eaa.Assert.Equal(dbTag.Name, respTags[i].Name)
eaa.Assert.Equal(dbTag.CategoryID, respTags[i].CategoryID)
}

tagIDs := make([]uuid.UUID, len(dbTags))
Expand All @@ -50,16 +49,16 @@ func TestGetCategoryTagsWorks(t *testing.T) {
body := SampleTagFactory(categoryUUID)
(*body)["id"] = tagID

appAssert.TestOnStatusAndDB(
appAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/categories/%s/tags", categoryUUID),
Role: &models.Super,
},
h.TesterWithStatus{
Status: fiber.StatusOK,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertTagsWithBodyRespDB(app, assert, resp, &[]map[string]interface{}{*body})
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertTagsWithBodyRespDB(eaa, resp, &[]map[string]interface{}{*body})
},
},
).Close()
Expand Down Expand Up @@ -102,10 +101,10 @@ func TestGetCategoryTagsFailsCategoryNotFound(t *testing.T) {
Role: &models.Super,
}, h.ErrorWithTester{
Error: errors.CategoryNotFound,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var category models.Category
err := app.Conn.Where("id = ?", uuid).First(&category).Error
assert.Assert(err != nil)
err := eaa.App.Conn.Where("id = ?", uuid).First(&category).Error
eaa.Assert.Assert(err != nil)
},
},
).Close()
Expand All @@ -114,16 +113,16 @@ func TestGetCategoryTagsFailsCategoryNotFound(t *testing.T) {
func TestGetCategoryTagWorks(t *testing.T) {
existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, tagUUID),
Role: &models.Super,
},
h.TesterWithStatus{
Status: fiber.StatusOK,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertTagWithBodyRespDB(app, assert, resp, SampleTagFactory(categoryUUID))
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertTagWithBodyRespDB(eaa, resp, SampleTagFactory(categoryUUID))
},
},
).Close()
Expand Down
125 changes: 62 additions & 63 deletions backend/tests/api/category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
h "github.com/GenerateNU/sac/backend/tests/api/helpers"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/huandu/go-assert"

"github.com/goccy/go-json"
)
Expand All @@ -21,60 +20,60 @@ func SampleCategoryFactory() *map[string]interface{} {
}
}

func AssertCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response, body *map[string]interface{}) uuid.UUID {
func AssertCategoryBodyRespDB(eaa h.ExistingAppAssert, resp *http.Response, body *map[string]interface{}) uuid.UUID {
var respCategory models.Category

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

assert.NilError(err)
eaa.Assert.NilError(err)

var dbCategories []models.Category

err = app.Conn.Find(&dbCategories).Error
err = eaa.App.Conn.Find(&dbCategories).Error

assert.NilError(err)
eaa.Assert.NilError(err)

assert.Equal(1, len(dbCategories))
eaa.Assert.Equal(1, len(dbCategories))

dbCategory := dbCategories[0]

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

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

return dbCategory.ID
}

func AssertCategoryWithBodyRespDBMostRecent(app h.TestApp, assert *assert.A, resp *http.Response, body *map[string]interface{}) uuid.UUID {
func AssertCategoryWithBodyRespDBMostRecent(eaa h.ExistingAppAssert, resp *http.Response, body *map[string]interface{}) uuid.UUID {
var respCategory models.Category

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

assert.NilError(err)
eaa.Assert.NilError(err)

var dbCategory models.Category

err = app.Conn.Order("created_at desc").First(&dbCategory).Error
err = eaa.App.Conn.Order("created_at desc").First(&dbCategory).Error

assert.NilError(err)
eaa.Assert.NilError(err)

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

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

return dbCategory.ID
}

func AssertSampleCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response) uuid.UUID {
return AssertCategoryBodyRespDB(app, assert, resp, SampleCategoryFactory())
func AssertSampleCategoryBodyRespDB(eaa h.ExistingAppAssert, resp *http.Response) uuid.UUID {
return AssertCategoryBodyRespDB(eaa, resp, SampleCategoryFactory())
}

func CreateSampleCategory(existingAppAssert h.ExistingAppAssert) (h.ExistingAppAssert, uuid.UUID) {
var sampleCategoryUUID uuid.UUID

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Expand All @@ -83,8 +82,8 @@ func CreateSampleCategory(existingAppAssert h.ExistingAppAssert) (h.ExistingAppA
},
h.TesterWithStatus{
Status: fiber.StatusCreated,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
sampleCategoryUUID = AssertSampleCategoryBodyRespDB(app, assert, resp)
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
sampleCategoryUUID = AssertSampleCategoryBodyRespDB(eaa, resp)
},
},
)
Expand All @@ -98,7 +97,7 @@ func TestCreateCategoryWorks(t *testing.T) {
}

func TestCreateCategoryIgnoresid(t *testing.T) {
h.InitTest(t).TestOnStatusAndDB(
h.InitTest(t).TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/categories/",
Expand All @@ -110,29 +109,29 @@ func TestCreateCategoryIgnoresid(t *testing.T) {
},
h.TesterWithStatus{
Status: fiber.StatusCreated,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertSampleCategoryBodyRespDB(app, assert, resp)
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertSampleCategoryBodyRespDB(eaa, resp)
},
},
).Close()
}

func Assert1Category(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertNumCategoriesRemainsAtN(app, assert, resp, 1)
func Assert1Category(eaa h.ExistingAppAssert, resp *http.Response) {
AssertNumCategoriesRemainsAtN(eaa, resp, 1)
}

func AssertNoCategories(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertNumCategoriesRemainsAtN(app, assert, resp, 0)
func AssertNoCategories(eaa h.ExistingAppAssert, resp *http.Response) {
AssertNumCategoriesRemainsAtN(eaa, resp, 0)
}

func AssertNumCategoriesRemainsAtN(app h.TestApp, assert *assert.A, resp *http.Response, n int) {
func AssertNumCategoriesRemainsAtN(eaa h.ExistingAppAssert, resp *http.Response, n int) {
var categories []models.Category

err := app.Conn.Find(&categories).Error
err := eaa.App.Conn.Find(&categories).Error

assert.NilError(err)
eaa.Assert.NilError(err)

assert.Equal(n, len(categories))
eaa.Assert.Equal(n, len(categories))
}

func TestCreateCategoryFailsIfNameIsNotString(t *testing.T) {
Expand Down Expand Up @@ -170,8 +169,8 @@ func TestCreateCategoryFailsIfNameIsMissing(t *testing.T) {
func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

TestNumCategoriesRemainsAt1 := func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertNumCategoriesRemainsAtN(app, assert, resp, 1)
TestNumCategoriesRemainsAt1 := func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertNumCategoriesRemainsAtN(eaa, resp, 1)
}

for _, permutation := range h.AllCasingPermutations((*SampleCategoryFactory())["name"].(string)) {
Expand All @@ -198,16 +197,16 @@ func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) {
func TestGetCategoryWorks(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/categories/%s", uuid),
Role: &models.Super,
},
h.TesterWithStatus{
Status: fiber.StatusOK,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertSampleCategoryBodyRespDB(app, assert, resp)
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertSampleCategoryBodyRespDB(eaa, resp)
},
},
).Close()
Expand Down Expand Up @@ -251,62 +250,62 @@ func TestGetCategoryFailsNotFound(t *testing.T) {
func TestGetCategoriesWorks(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodGet,
Path: "/api/v1/categories/",
Role: &models.Super,
},
h.TesterWithStatus{
Status: fiber.StatusOK,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var categories []models.Category

err := app.Conn.Find(&categories).Error
err := eaa.App.Conn.Find(&categories).Error

assert.NilError(err)
eaa.Assert.NilError(err)

var respCategories []models.Category

err = json.NewDecoder(resp.Body).Decode(&respCategories)

assert.NilError(err)
eaa.Assert.NilError(err)

assert.Equal(1, len(respCategories))
assert.Equal(1, len(categories))
eaa.Assert.Equal(1, len(respCategories))
eaa.Assert.Equal(1, len(categories))

assert.Equal(categories[0].ID, respCategories[0].ID)
eaa.Assert.Equal(categories[0].ID, respCategories[0].ID)

assert.Equal(categories[0].Name, respCategories[0].Name)
eaa.Assert.Equal(categories[0].Name, respCategories[0].Name)

assert.Equal((*SampleCategoryFactory())["name"].(string), categories[0].Name)
eaa.Assert.Equal((*SampleCategoryFactory())["name"].(string), categories[0].Name)
},
},
).Close()
}

func AssertUpdatedCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response, body *map[string]interface{}) {
func AssertUpdatedCategoryBodyRespDB(eaa h.ExistingAppAssert, resp *http.Response, body *map[string]interface{}) {
var respCategory models.Category

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

assert.NilError(err)
eaa.Assert.NilError(err)

var dbCategories []models.Category

err = app.Conn.Find(&dbCategories).Error
err = eaa.App.Conn.Find(&dbCategories).Error

assert.NilError(err)
eaa.Assert.NilError(err)

assert.Equal(1, len(dbCategories))
eaa.Assert.Equal(1, len(dbCategories))

dbCategory := dbCategories[0]

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

assert.Equal((*body)["id"].(uuid.UUID), dbCategory.ID)
assert.Equal((*body)["name"].(string), dbCategory.Name)
eaa.Assert.Equal((*body)["id"].(uuid.UUID), dbCategory.ID)
eaa.Assert.Equal((*body)["name"].(string), dbCategory.Name)
}

func TestUpdateCategoryWorks(t *testing.T) {
Expand All @@ -317,11 +316,11 @@ func TestUpdateCategoryWorks(t *testing.T) {
"name": "Arts & Crafts",
}

AssertUpdatedCategoryBodyRespDB := func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertUpdatedCategoryBodyRespDB(app, assert, resp, &category)
AssertUpdatedCategoryBodyRespDB := func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertUpdatedCategoryBodyRespDB(eaa, resp, &category)
}

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPatch,
Path: fmt.Sprintf("/api/v1/categories/%s", uuid),
Expand All @@ -341,11 +340,11 @@ func TestUpdateCategoryWorksWithSameDetails(t *testing.T) {
category := *SampleCategoryFactory()
category["id"] = uuid

AssertSampleCategoryBodyRespDB := func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertUpdatedCategoryBodyRespDB(app, assert, resp, &category)
AssertSampleCategoryBodyRespDB := func(eaa h.ExistingAppAssert, resp *http.Response) {
AssertUpdatedCategoryBodyRespDB(eaa, resp, &category)
}

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPatch,
Path: fmt.Sprintf("/api/v1/categories/%s", uuid),
Expand Down Expand Up @@ -388,7 +387,7 @@ func TestUpdateCategoryFailsBadRequest(t *testing.T) {
func TestDeleteCategoryWorks(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
existingAppAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/categories/%s", uuid),
Expand Down
Loading
Loading