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

✅ Testでgo-cmpを使うように #829

Merged
merged 11 commits into from
Dec 30, 2024
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.3
require (
entgo.io/ent v0.14.1
github.com/go-sql-driver/mysql v1.8.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/gorilla/sessions v1.4.0
github.com/labstack/echo-contrib v0.17.2
Expand All @@ -24,7 +25,6 @@ require (
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/inflect v0.21.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/hashicorp/hcl/v2 v2.23.0 // indirect
Expand Down
12 changes: 5 additions & 7 deletions model/admin_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@ func TestEntRepository_GetAdmins(t *testing.T) {

got, err := repo.GetAdmins(ctx)
assert.NoError(t, err)
if assert.Len(t, got, 2) && got[0].ID == user1.ID {
assert.Equal(t, got[0].ID, user1.ID)
assert.Equal(t, got[1].ID, user2.ID)
} else if assert.Len(t, got, 2) {
assert.Equal(t, got[0].ID, user2.ID)
assert.Equal(t, got[1].ID, user1.ID)
exp := []*Admin{
{ID: user1.ID},
{ID: user2.ID},
}
assert.ElementsMatch(t, exp, got)
})

t.Run("Success2", func(t *testing.T) {
t.Parallel()

got, err := repo2.GetAdmins(ctx)
assert.NoError(t, err)
assert.Len(t, got, 0)
assert.Empty(t, got)
})
}

Expand Down
73 changes: 45 additions & 28 deletions model/comment_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package model
import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/traPtitech/Jomon/testutil"
"github.com/traPtitech/Jomon/testutil/random"
)

Expand Down Expand Up @@ -42,21 +45,13 @@ func TestEntRepository_GetComments(t *testing.T) {

got, err := repo.GetComments(ctx, request.ID)
assert.NoError(t, err)
if assert.Len(t, got, 2) && got[0].ID == comment1.ID {
assert.Equal(t, got[0].ID, comment1.ID)
assert.Equal(t, got[0].User, comment1.User)
assert.Equal(t, got[0].Comment, comment1.Comment)
assert.Equal(t, got[1].ID, comment2.ID)
assert.Equal(t, got[1].User, comment2.User)
assert.Equal(t, got[1].Comment, comment2.Comment)
} else if assert.Len(t, got, 2) {
assert.Equal(t, got[0].ID, comment2.ID)
assert.Equal(t, got[0].User, comment2.User)
assert.Equal(t, got[0].Comment, comment2.Comment)
assert.Equal(t, got[1].ID, comment1.ID)
assert.Equal(t, got[1].User, comment1.User)
assert.Equal(t, got[1].Comment, comment1.Comment)
}
opts := testutil.ApproxEqualOptions()
opts = append(opts,
cmpopts.SortSlices(func(l, r *Comment) bool {
return l.ID.ID() < r.ID.ID()
}))
exp := []*Comment{comment1, comment2}
testutil.RequireEqual(t, exp, got, opts...)
})

t.Run("Success2", func(t *testing.T) {
Expand All @@ -77,7 +72,7 @@ func TestEntRepository_GetComments(t *testing.T) {

got, err := repo2.GetComments(ctx, request.ID)
assert.NoError(t, err)
assert.Len(t, got, 0)
assert.Empty(t, got)
})

t.Run("UnknownRequest", func(t *testing.T) {
Expand Down Expand Up @@ -118,8 +113,16 @@ func TestEntRepository_CreateComment(t *testing.T) {
require.NoError(t, err)
created, err := repo.CreateComment(ctx, comment, request.ID, user2.ID)
assert.NoError(t, err)
assert.Equal(t, created.User, user2.ID)
assert.Equal(t, created.Comment, comment)
opts := testutil.ApproxEqualOptions()
opts = append(opts,
cmpopts.IgnoreFields(Comment{}, "ID"))
exp := &Comment{
User: user2.ID,
Comment: comment,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
testutil.RequireEqual(t, exp, created, opts...)
})

t.Run("UnknownRequest", func(t *testing.T) {
Expand Down Expand Up @@ -155,7 +158,7 @@ func TestEntRepository_CreateComment(t *testing.T) {
})
}

func TestEntREpository_UpdateComment(t *testing.T) {
func TestEntRepository_UpdateComment(t *testing.T) {
ctx := context.Background()
client, storage, err := setup(t, ctx, "update_comment")
require.NoError(t, err)
Expand All @@ -182,9 +185,15 @@ func TestEntREpository_UpdateComment(t *testing.T) {
comment := random.AlphaNumeric(t, 30)
updated, err := repo.UpdateComment(ctx, comment, request.ID, created.ID)
assert.NoError(t, err)
assert.Equal(t, updated.ID, created.ID)
assert.Equal(t, updated.User, created.User)
assert.Equal(t, updated.Comment, comment)
opts := testutil.ApproxEqualOptions()
exp := &Comment{
ID: created.ID,
User: created.User,
Comment: comment,
CreatedAt: created.CreatedAt,
UpdatedAt: time.Now(),
}
testutil.RequireEqual(t, exp, updated, opts...)
})

t.Run("Success2", func(t *testing.T) {
Expand Down Expand Up @@ -214,15 +223,19 @@ func TestEntREpository_UpdateComment(t *testing.T) {
require.NoError(t, err)
updated, err := repo.UpdateComment(ctx, comment.Comment, request2.ID, comment.ID)
assert.NoError(t, err)
assert.Equal(t, updated.ID, comment.ID)
assert.Equal(t, updated.User, comment.User)
assert.Equal(t, updated.Comment, comment.Comment)
opts := testutil.ApproxEqualOptions()
exp := &Comment{
ID: comment.ID,
User: comment.User,
Comment: comment.Comment,
CreatedAt: comment.CreatedAt,
UpdatedAt: time.Now(),
}
testutil.RequireEqual(t, exp, updated, opts...)

got, err := repo.GetComments(ctx, request2.ID)
require.NoError(t, err)
assert.Equal(t, got[0].ID, updated.ID)
assert.Equal(t, got[0].User, updated.User)
assert.Equal(t, got[0].Comment, updated.Comment)
testutil.RequireEqual(t, []*Comment{updated}, got, opts...)
})

t.Run("UnknownComment", func(t *testing.T) {
Expand Down Expand Up @@ -297,6 +310,10 @@ func TestEntRepository_DeleteComment(t *testing.T) {

err = repo.DeleteComment(ctx, request.ID, comment.ID)
assert.NoError(t, err)

comments, err := repo.GetComments(ctx, request.ID)
require.NoError(t, err)
assert.Empty(t, comments)
})

t.Run("UnknownRequest", func(t *testing.T) {
Expand Down
29 changes: 23 additions & 6 deletions model/file_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package model
import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/traPtitech/Jomon/testutil"
"github.com/traPtitech/Jomon/testutil/random"
)

Expand Down Expand Up @@ -43,8 +46,16 @@ func TestEntRepository_CreateFile(t *testing.T) {

file, err := repo.CreateFile(ctx, name, mimetype, request.ID, user.ID)
assert.NoError(t, err)
assert.Equal(t, name, file.Name)
assert.Equal(t, mimetype, file.MimeType)
opts := testutil.ApproxEqualOptions()
opts = append(opts,
cmpopts.IgnoreFields(File{}, "ID"))
exp := &File{
Name: name,
MimeType: mimetype,
CreatedBy: user.ID,
CreatedAt: time.Now(),
}
testutil.RequireEqual(t, exp, file, opts...)
})

t.Run("UnknownRequest", func(t *testing.T) {
Expand Down Expand Up @@ -132,9 +143,15 @@ func TestEntRepository_GetFile(t *testing.T) {
assert.NoError(t, err)
got, err := repo.GetFile(ctx, file.ID)
assert.NoError(t, err)
assert.Equal(t, file.ID, got.ID)
assert.Equal(t, file.Name, got.Name)
assert.Equal(t, file.MimeType, got.MimeType)
opts := testutil.ApproxEqualOptions()
exp := &File{
ID: file.ID,
Name: name,
MimeType: mimetype,
CreatedBy: user.ID,
CreatedAt: file.CreatedAt,
}
testutil.RequireEqual(t, exp, got, opts...)
})

t.Run("UnknownFile", func(t *testing.T) {
Expand Down Expand Up @@ -185,7 +202,7 @@ func TestEntRepository_DeleteFile(t *testing.T) {

r, err := repo.GetRequest(ctx, request.ID)
require.NoError(t, err)
assert.Len(t, r.Files, 0)
assert.Empty(t, r.Files)
})

t.Run("UnknownFile", func(t *testing.T) {
Expand Down
Loading
Loading