Skip to content

Commit

Permalink
repositoryテストの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
pirosiki197 committed Jan 15, 2025
1 parent 29e2276 commit 1a8a63e
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 31 deletions.
5 changes: 3 additions & 2 deletions server/repository/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"
"time"

"github.com/stephenafamo/bob"
"github.com/testcontainers/testcontainers-go/modules/mysql"
dbrepo "github.com/traPtitech/piscon-portal-v2/server/repository/db"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestMain(m *testing.M) {
m.Run()
}

func setupRepository(t *testing.T) *dbrepo.Repository {
func setupRepository(t *testing.T) (*dbrepo.Repository, bob.Executor) {
t.Helper()

ctx := context.Background()
Expand All @@ -59,7 +60,7 @@ func setupRepository(t *testing.T) *dbrepo.Repository {
t.Fatal(err)
}

return dbrepo.NewRepository(db)
return dbrepo.NewRepository(db), bob.New(db)
}

func createDatabase(name string) error {
Expand Down
154 changes: 125 additions & 29 deletions server/repository/db/team_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package db_test

import (
"cmp"
"context"
"slices"
"testing"
"time"

Expand All @@ -11,50 +13,111 @@ import (
"github.com/traPtitech/piscon-portal-v2/server/domain"
)

func TestCreateTeam(t *testing.T) {
func TestGetTeam(t *testing.T) {
t.Parallel()

repo := setupRepository(t)
repo, db := setupRepository(t)

teamID := uuid.New()
members := []domain.User{
{
ID: uuid.New(),
Name: "user1",
ID: uuid.New(),
Name: "user1",
TeamID: uuid.NullUUID{UUID: teamID, Valid: true},
},
{
ID: uuid.New(),
Name: "user2",
ID: uuid.New(),
Name: "user2",
TeamID: uuid.NullUUID{UUID: teamID, Valid: true},
},
}
team := domain.Team{
ID: uuid.New(),
ID: teamID,
Name: "team1",
Members: members,
CreatedAt: time.Now(),
}
mustMakeTeam(t, db, team)
for _, member := range members {
err := repo.CreateUser(context.Background(), member)
require.NoError(t, err)
mustMakeUser(t, db, member)
}

err := repo.CreateTeam(context.Background(), team)
if !assert.NoError(t, err) {
return
got, err := repo.FindTeam(context.Background(), team.ID)
require.NoError(t, err)

compareTeam(t, team, got)
}

func TestGetTeams(t *testing.T) {
t.Parallel()

repo, db := setupRepository(t)

teamID1 := uuid.New()
teamID2 := uuid.New()

members1 := []domain.User{
{
ID: uuid.New(),
Name: "user1",
TeamID: uuid.NullUUID{UUID: teamID1, Valid: true},
},
{
ID: uuid.New(),
Name: "user2",
TeamID: uuid.NullUUID{UUID: teamID1, Valid: true},
},
}
members2 := []domain.User{
{
ID: uuid.New(),
Name: "user3",
TeamID: uuid.NullUUID{UUID: teamID2, Valid: true},
},
{
ID: uuid.New(),
Name: "user4",
TeamID: uuid.NullUUID{UUID: teamID2, Valid: true},
},
}
teams := []domain.Team{
{
ID: teamID1,
Name: "team1",
Members: members1,
CreatedAt: time.Now(),
},
{
ID: teamID2,
Name: "team2",
Members: members2,
CreatedAt: time.Now(),
},
}
for _, team := range teams {
mustMakeTeam(t, db, team)
for _, member := range team.Members {
mustMakeUser(t, db, member)
}
}

got, err := repo.FindTeam(context.Background(), team.ID)
got, err := repo.GetTeams(context.Background())
require.NoError(t, err)

assert.Equal(t, team.ID, got.ID)
for _, member := range got.Members {
assert.Equal(t, team.ID, member.TeamID.UUID)
assert.Len(t, got, len(teams))

// sort teams by ID
slices.SortFunc(got, func(a, b domain.Team) int { return cmp.Compare(a.ID.String(), b.ID.String()) })
slices.SortFunc(teams, func(a, b domain.Team) int { return cmp.Compare(a.ID.String(), b.ID.String()) })
for i, team := range teams {
compareTeam(t, team, got[i])
}
}

func TestUpdateTeam(t *testing.T) {
func TestCreateTeam(t *testing.T) {
t.Parallel()

repo := setupRepository(t)
repo, db := setupRepository(t)

members := []domain.User{
{
Expand All @@ -72,29 +135,62 @@ func TestUpdateTeam(t *testing.T) {
Members: members,
CreatedAt: time.Now(),
}

for _, member := range members {
err := repo.CreateUser(context.Background(), member)
require.NoError(t, err)
mustMakeUser(t, db, member)
}

err := repo.CreateTeam(context.Background(), team)
if !assert.NoError(t, err) {
return
}

got, err := repo.FindTeam(context.Background(), team.ID)
require.NoError(t, err)

compareTeam(t, team, got)
}

func TestUpdateTeam(t *testing.T) {
t.Parallel()

repo, db := setupRepository(t)

team := domain.Team{
ID: uuid.New(),
Name: "team1",
Members: nil,
CreatedAt: time.Now(),
}
newMember := domain.User{
ID: uuid.New(),
Name: "user2",
}
mustMakeUser(t, db, newMember)
mustMakeTeam(t, db, team)

// change the team name and add a new member
team.Name = "team2"
team.AddMember(domain.User{
ID: uuid.New(),
Name: "user3",
})
err = repo.UpdateTeam(context.Background(), team)
team.AddMember(newMember)

Check failure on line 173 in server/repository/db/team_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `team.AddMember` is not checked (errcheck)
err := repo.UpdateTeam(context.Background(), team)
assert.NoError(t, err)

got, err := repo.FindTeam(context.Background(), team.ID)
require.NoError(t, err)

assert.Equal(t, team.ID, got.ID)
for _, member := range got.Members {
assert.Equal(t, team.ID, member.TeamID.UUID)
compareTeam(t, team, got)
}

func compareTeam(t *testing.T, want domain.Team, got domain.Team) {
t.Helper()

assert.Equal(t, want.ID, got.ID)
assert.Equal(t, want.Name, got.Name)
assert.Len(t, want.Members, len(got.Members))
// sort members by ID
slices.SortFunc(got.Members, func(a, b domain.User) int { return cmp.Compare(a.ID.String(), b.ID.String()) })
slices.SortFunc(want.Members, func(a, b domain.User) int { return cmp.Compare(a.ID.String(), b.ID.String()) })
for i, member := range want.Members {
assert.Equal(t, member.ID, got.Members[i].ID)
assert.Equal(t, member.Name, got.Members[i].Name)
}
}
34 changes: 34 additions & 0 deletions server/repository/db/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package db_test

import (
"context"
"testing"

"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/samber/lo"
"github.com/stephenafamo/bob"
"github.com/stretchr/testify/require"
"github.com/traPtitech/piscon-portal-v2/server/domain"
"github.com/traPtitech/piscon-portal-v2/server/repository/db/models"
)

func mustMakeUser(t *testing.T, executor bob.Executor, user domain.User) {
t.Helper()
_, err := models.Users.Insert(&models.UserSetter{
ID: omit.From(user.ID.String()),
Name: omit.From(user.Name),
TeamID: lo.Ternary(user.TeamID.Valid, omitnull.From(user.TeamID.UUID.String()), omitnull.Val[string]{}),
}).Exec(context.Background(), executor)
require.NoError(t, err)
}

func mustMakeTeam(t *testing.T, executor bob.Executor, team domain.Team) {
t.Helper()
_, err := models.Teams.Insert(&models.TeamSetter{
ID: omit.From(team.ID.String()),
Name: omit.From(team.Name),
CreatedAt: omit.From(team.CreatedAt),
}).Exec(context.Background(), executor)
require.NoError(t, err)
}

0 comments on commit 1a8a63e

Please sign in to comment.