Skip to content

Commit

Permalink
DELETE and GET test cases for club and user
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space committed Feb 3, 2024
1 parent 347310d commit 5b76f07
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 1 deletion.
97 changes: 96 additions & 1 deletion backend/tests/api/club_follower_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,98 @@
package tests

// TODO
import (
stdliberrors "errors"
"fmt"
"net/http"
"testing"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/huandu/go-assert"
"gorm.io/gorm"
)


func TestGetClubFollowersWorks(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", clubUUID),
}.TestOnStatusAndDB(t, &appAssert,
DBTesterWithStatus{
Status: fiber.StatusCreated,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var dbClub *models.Club
err := app.Conn.Preload("Follower").First(dbClub, clubUUID).Error
assert.NilError(err)
assert.Equal(len(dbClub.Follower), 1)

var user *models.User
err = app.Conn.First(user, userUUID).Error
assert.NilError(err)

user, _ = transactions.GetUser(app.Conn, userUUID)
userFollower := &dbClub.Follower[0]
assert.Equal(userFollower, user)
},
},
)
appAssert.Close()
}


func TestGetClubFollowersFailsClubNotExist(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)
clubUUIDNotExist := uuid.New()

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", clubUUIDNotExist),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.ClubNotFound,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var club models.Club
err := app.Conn.Where("id = ?", clubUUIDNotExist).First(&club).Error
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound))
},
},
).Close()
}

func TestGetClubFollowersFailsClubIdBadRequest(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}
for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", badRequest),
}.TestOnError(t, &appAssert, errors.FailedToValidateID).Close()
}
}
183 changes: 183 additions & 0 deletions backend/tests/api/user_follower_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/huandu/go-assert"
Expand Down Expand Up @@ -110,3 +111,185 @@ func TestCreateUserFollowingFailsClubNotExist(t *testing.T) {
},
).Close()
}

func TestDeleteUserFollowingWorks(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)
TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatusAndDB(t, &appAssert,
DBTesterWithStatus{
Status: fiber.StatusCreated,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var dbUser models.User
err := app.Conn.Preload("Follower").First(&dbUser, userUUID).Error
assert.NilError(err)

assert.Equal(len(dbUser.Follower), 0)
},
},
)
appAssert.Close()
}

func TestDeleteUserFollowingFailsClubIdBadRequest(t *testing.T) {
appAssert, userUUID := CreateSampleUser(t, nil)

badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}

for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, badRequest),
}.TestOnError(t, &appAssert, errors.FailedToValidateID).Close()
}
}

func TestDeleteUserFollowingFailsUserIdBadRequest(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(t, nil)

badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}

for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", badRequest, clubUUID),
}.TestOnError(t, &appAssert, errors.FailedToValidateID).Close()
}
}


func TestDeleteUserFollowingFailsUserNotExist(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(t, nil)
userUUIDNotExist := uuid.New()

TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUIDNotExist, clubUUID),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.UserNotFound,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var user models.User
err := app.Conn.Where("id = ?", userUUIDNotExist).First(&user).Error
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound))
},
},
).Close()
}

func TestDeleteUserFollowingFailsClubNotExist(t *testing.T) {
appAssert, userUUID := CreateSampleUser(t, nil)
clubUUIDNotExist := uuid.New()

TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUIDNotExist),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.ClubNotFound,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var club models.Club
err := app.Conn.Where("id = ?", clubUUIDNotExist).First(&club).Error
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound))
},
},
).Close()
}

func TestGetUserFollowingWorks(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)
TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/users/%s/follower", userUUID),
}.TestOnStatusAndDB(t, &appAssert,
DBTesterWithStatus{
Status: fiber.StatusCreated,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var dbUser *models.User
err := app.Conn.Preload("Follower").First(dbUser, userUUID).Error
assert.NilError(err)
assert.Equal(len(dbUser.Follower), 1)

var club *models.Club
err = app.Conn.First(club, clubUUID).Error
assert.NilError(err)

club, _ = transactions.GetClub(app.Conn, clubUUID)
clubFollowed := &dbUser.Follower[0]
assert.Equal(clubFollowed, club)
},
},
)
appAssert.Close()
}


func TestGetUserFailsUserNotExist(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)
userUUIDNotExist := uuid.New()

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/users/%s/follower", userUUIDNotExist),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.ClubNotFound,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var user models.User
err := app.Conn.Where("id = ?", userUUIDNotExist).First(&user).Error
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound))
},
},
).Close()
}

func TestGetUserFailsUserIdBadRequest(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}
for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/users/%s/follower", badRequest),
}.TestOnError(t, &appAssert, errors.FailedToValidateID).Close()
}
}
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

0 comments on commit 5b76f07

Please sign in to comment.