diff --git a/model/admin_impl_test.go b/model/admin_impl_test.go index 57a962c0..5ac48115 100644 --- a/model/admin_impl_test.go +++ b/model/admin_impl_test.go @@ -36,13 +36,11 @@ 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) { @@ -50,7 +48,7 @@ func TestEntRepository_GetAdmins(t *testing.T) { got, err := repo2.GetAdmins(ctx) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) } diff --git a/model/comment_impl_test.go b/model/comment_impl_test.go index dd7b8af5..c76c4137 100644 --- a/model/comment_impl_test.go +++ b/model/comment_impl_test.go @@ -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" ) @@ -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) { @@ -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) { @@ -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) { @@ -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) @@ -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) { @@ -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) { @@ -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) { diff --git a/model/file_impl_test.go b/model/file_impl_test.go index 0fd3178f..8bda6143 100644 --- a/model/file_impl_test.go +++ b/model/file_impl_test.go @@ -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" ) @@ -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) { @@ -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) { @@ -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) { diff --git a/model/group_impl_test.go b/model/group_impl_test.go index 6104647d..c45247e8 100644 --- a/model/group_impl_test.go +++ b/model/group_impl_test.go @@ -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" ) @@ -31,8 +34,8 @@ func TestEntRepository_GetGroups(t *testing.T) { groups, err := repo.GetGroups(ctx) require.NoError(t, err) - assert.Equal(t, 1, len(groups)) - assert.Equal(t, group.ID, groups[0].ID) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, []*Group{group}, groups, opts...) }) t.Run("Success2", func(t *testing.T) { @@ -52,19 +55,17 @@ func TestEntRepository_GetGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup( + created, err := repo.CreateGroup( ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) require.NoError(t, err) - g, err := repo.GetGroup(ctx, group.ID) + got, err := repo.GetGroup(ctx, created.ID) assert.NoError(t, err) - assert.Equal(t, group.ID, g.ID) - assert.Equal(t, group.Name, g.Name) - assert.Equal(t, group.Description, g.Description) - assert.Equal(t, group.Budget, g.Budget) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, created, got, opts...) }) t.Run("UnknownGroup", func(t *testing.T) { @@ -85,22 +86,40 @@ func TestEntRepository_CreateGroup(t *testing.T) { budget := random.Numeric(t, 100000) name := random.AlphaNumeric(t, 20) description := random.AlphaNumeric(t, 15) - group, err := repo.CreateGroup(ctx, name, description, &budget) + created, err := repo.CreateGroup(ctx, name, description, &budget) require.NoError(t, err) - assert.Equal(t, name, group.Name) - assert.Equal(t, description, group.Description) - assert.Equal(t, *group.Budget, budget) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(Group{}, "ID")) + exp := &Group{ + Name: name, + Description: description, + Budget: &budget, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, created, opts...) }) t.Run("SuccessWithNilBudget", func(t *testing.T) { t.Parallel() name := random.AlphaNumeric(t, 20) description := random.AlphaNumeric(t, 15) - group, err := repo.CreateGroup(ctx, name, description, nil) + created, err := repo.CreateGroup(ctx, name, description, nil) assert.NoError(t, err) - assert.Equal(t, name, group.Name) - assert.Equal(t, description, group.Description) - assert.Nil(t, group.Budget) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(Group{}, "ID")) + exp := &Group{ + Name: name, + Description: description, + Budget: nil, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, created, opts...) }) t.Run("FailedWithEmptyName", func(t *testing.T) { @@ -120,7 +139,7 @@ func TestEntRepository_UpdateGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup( + created, err := repo.CreateGroup( ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), @@ -129,16 +148,24 @@ func TestEntRepository_UpdateGroup(t *testing.T) { updatedBudget := random.Numeric(t, 10000) ug := Group{ - ID: group.ID, + ID: created.ID, Name: random.AlphaNumeric(t, 20), Description: random.AlphaNumeric(t, 15), Budget: &updatedBudget, } - updated, err := repo.UpdateGroup(ctx, group.ID, ug.Name, ug.Description, ug.Budget) + updated, err := repo.UpdateGroup(ctx, created.ID, ug.Name, ug.Description, ug.Budget) assert.NoError(t, err) - assert.Equal(t, ug.Name, updated.Name) - assert.Equal(t, ug.Description, updated.Description) - assert.Equal(t, ug.Budget, updated.Budget) + opts := testutil.ApproxEqualOptions() + exp := &Group{ + ID: created.ID, + Name: ug.Name, + Description: ug.Description, + Budget: &updatedBudget, + CreatedAt: created.CreatedAt, + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, updated, opts...) }) t.Run("UnknownGroup", func(t *testing.T) { @@ -156,7 +183,7 @@ func TestEntRepository_UpdateGroup(t *testing.T) { t.Run("SuccessWithNilBudget", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup( + created, err := repo.CreateGroup( ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), @@ -164,16 +191,24 @@ func TestEntRepository_UpdateGroup(t *testing.T) { require.NoError(t, err) ug := Group{ - ID: group.ID, + ID: created.ID, Name: random.AlphaNumeric(t, 20), Description: random.AlphaNumeric(t, 15), Budget: nil, } - updated, err := repo.UpdateGroup(ctx, group.ID, ug.Name, ug.Description, ug.Budget) + updated, err := repo.UpdateGroup(ctx, created.ID, ug.Name, ug.Description, ug.Budget) assert.NoError(t, err) - assert.Equal(t, ug.Name, updated.Name) - assert.Equal(t, ug.Description, updated.Description) - assert.Nil(t, updated.Budget) + opts := testutil.ApproxEqualOptions() + exp := &Group{ + ID: created.ID, + Name: ug.Name, + Description: ug.Description, + Budget: nil, + CreatedAt: created.CreatedAt, + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, updated, opts...) }) t.Run("FailedWithEmptyName", func(t *testing.T) { @@ -209,6 +244,10 @@ func TestEntRepository_DeleteGroup(t *testing.T) { err = repo.DeleteGroup(ctx, group.ID) assert.NoError(t, err) + + groups, err := repo.GetGroups(ctx) + require.NoError(t, err) + assert.Empty(t, groups) }) t.Run("UnknownGroup", func(t *testing.T) { @@ -255,13 +294,14 @@ func TestEntRepository_GetMembers(t *testing.T) { got, err := repo.GetMembers(ctx, group.ID) 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) + sortOpt := cmpopts.SortSlices(func(a, b *Member) bool { + return a.ID.ID() < b.ID.ID() + }) + exp := []*Member{ + {ID: user1.ID}, + {ID: user2.ID}, } + testutil.RequireEqual(t, exp, got, sortOpt) }) t.Run("Success2", func(t *testing.T) { @@ -304,9 +344,10 @@ func TestEntRepository_CreateMember(t *testing.T) { true) require.NoError(t, err) - member, err := repo.AddMembers(ctx, group.ID, []uuid.UUID{user.ID}) + got, err := repo.AddMembers(ctx, group.ID, []uuid.UUID{user.ID}) assert.NoError(t, err) - assert.Equal(t, member[0].ID, user.ID) + exp := []*Member{{ID: user.ID}} + testutil.RequireEqual(t, exp, got) }) t.Run("UnknownUser", func(t *testing.T) { @@ -374,7 +415,8 @@ func TestEntRepository_GetOwners(t *testing.T) { user1, err := repo.CreateUser( ctx, random.AlphaNumeric(t, 20), - random.AlphaNumeric(t, 15), true) + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) user2, err := repo.CreateUser( ctx, @@ -388,13 +430,14 @@ func TestEntRepository_GetOwners(t *testing.T) { got, err := repo.GetOwners(ctx, group.ID) 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[1].ID, user1.ID) - assert.Equal(t, got[0].ID, user2.ID) + sortOpt := cmpopts.SortSlices(func(a, b *Owner) bool { + return a.ID.ID() < b.ID.ID() + }) + exp := []*Owner{ + {ID: user1.ID}, + {ID: user2.ID}, } + testutil.RequireEqual(t, exp, got, sortOpt) }) t.Run("Success2", func(t *testing.T) { @@ -410,10 +453,11 @@ func TestEntRepository_GetOwners(t *testing.T) { got, err := repo.GetOwners(ctx, group.ID) assert.NoError(t, err) - assert.Equal(t, got, []*Owner{}) + assert.Empty(t, got) }) } +// FIXME: これAddOwnersでは? func TestEntRepository_CreateOwner(t *testing.T) { ctx := context.Background() client, storage, err := setup(t, ctx, "create_owner") @@ -439,7 +483,8 @@ func TestEntRepository_CreateOwner(t *testing.T) { owner, err := repo.AddOwners(ctx, group.ID, []uuid.UUID{user.ID}) assert.NoError(t, err) - assert.Equal(t, owner[0].ID, user.ID) + exp := []*Owner{{ID: user.ID}} + testutil.RequireEqual(t, exp, owner) }) t.Run("UnknownUser", func(t *testing.T) { diff --git a/model/request_impl_test.go b/model/request_impl_test.go index e8ebdd43..643d6c50 100644 --- a/model/request_impl_test.go +++ b/model/request_impl_test.go @@ -116,14 +116,16 @@ func TestEntRepository_GetRequests(t *testing.T) { Sort: &sort, }) assert.NoError(t, err) - if assert.Len(t, got, 2) && assert.Equal(t, got[1].ID, request1.ID) { - exp := []*RequestResponse{ - request2.toExpectedRequestResponse(t), - request1.toExpectedRequestResponse(t), - } - opts := testutil.ApproxEqualOptions() - testutil.RequireEqual(t, exp, got, opts...) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(a, b *RequestResponse) bool { + return a.ID.ID() < b.ID.ID() + })) + exp := []*RequestResponse{ + request1.toExpectedRequestResponse(t), + request2.toExpectedRequestResponse(t), } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithReverseSortCreatedAt", func(t *testing.T) { @@ -182,14 +184,16 @@ func TestEntRepository_GetRequests(t *testing.T) { Sort: &sort, }) assert.NoError(t, err) - if assert.Len(t, got, 2) && assert.Equal(t, got[0].ID, request1.ID) { - exp := []*RequestResponse{ - request1.toExpectedRequestResponse(t), - request2.toExpectedRequestResponse(t), - } - opts := testutil.ApproxEqualOptions() - testutil.RequireEqual(t, exp, got, opts...) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(a, b *RequestResponse) bool { + return a.ID.ID() < b.ID.ID() + })) + exp := []*RequestResponse{ + request1.toExpectedRequestResponse(t), + request2.toExpectedRequestResponse(t), } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithSortTitle", func(t *testing.T) { @@ -247,14 +251,16 @@ func TestEntRepository_GetRequests(t *testing.T) { Sort: &sort, }) assert.NoError(t, err) - if assert.Len(t, got, 2) && assert.Equal(t, got[0].ID, request2.ID) { - exp := []*RequestResponse{ - request2.toExpectedRequestResponse(t), - request1.toExpectedRequestResponse(t), - } - opts := testutil.ApproxEqualOptions() - testutil.RequireEqual(t, exp, got, opts...) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(a, b *RequestResponse) bool { + return a.ID.ID() < b.ID.ID() + })) + exp := []*RequestResponse{ + request2.toExpectedRequestResponse(t), + request1.toExpectedRequestResponse(t), } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithReverseSortTitle", func(t *testing.T) { @@ -312,14 +318,16 @@ func TestEntRepository_GetRequests(t *testing.T) { Sort: &sort, }) assert.NoError(t, err) - if assert.Len(t, got, 2) && assert.Equal(t, got[0].ID, request1.ID) { - exp := []*RequestResponse{ - request1.toExpectedRequestResponse(t), - request2.toExpectedRequestResponse(t), - } - opts := testutil.ApproxEqualOptions() - testutil.RequireEqual(t, exp, got, opts...) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(a, b *RequestResponse) bool { + return a.ID.ID() < b.ID.ID() + })) + exp := []*RequestResponse{ + request1.toExpectedRequestResponse(t), + request2.toExpectedRequestResponse(t), } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithQueryTarget", func(t *testing.T) { diff --git a/model/request_status_impl_test.go b/model/request_status_impl_test.go index b3ecd830..f5eee7e2 100644 --- a/model/request_status_impl_test.go +++ b/model/request_status_impl_test.go @@ -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" ) @@ -35,7 +38,15 @@ func TestEntRepository_CreateStatus(t *testing.T) { status := Status(random.Numeric(t, 5) + 1) created, err := repo.CreateStatus(ctx, request.ID, user.ID, status) assert.NoError(t, err) - assert.Equal(t, created.Status, status) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(RequestStatus{}, "ID")) + exp := &RequestStatus{ + CreatedBy: user.ID, + Status: status, + CreatedAt: time.Now(), + } + testutil.RequireEqual(t, exp, created, opts...) }) t.Run("InvalidStatus", func(t *testing.T) { diff --git a/model/request_target_impl_test.go b/model/request_target_impl_test.go index 402caa45..c81210a2 100644 --- a/model/request_target_impl_test.go +++ b/model/request_target_impl_test.go @@ -3,9 +3,12 @@ package model import ( "context" "testing" + "time" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/traPtitech/Jomon/testutil" "github.com/traPtitech/Jomon/testutil/random" ) @@ -50,17 +53,17 @@ func TestEntRepository_GetRequestTargets(t *testing.T) { require.NoError(t, err) got, err := repo.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) - if assert.Len(t, got, 2) && got[0].Target == target1.Target { - assert.Equal(t, got[0].Target, target1.Target) - assert.Equal(t, got[0].Amount, target1.Amount) - assert.Equal(t, got[1].Target, target2.Target) - assert.Equal(t, got[1].Amount, target2.Amount) - } else if assert.Len(t, got, 2) { - assert.Equal(t, got[0].Target, target2.Target) - assert.Equal(t, got[0].Amount, target2.Amount) - assert.Equal(t, got[1].Target, target1.Target) - assert.Equal(t, got[1].Amount, target1.Amount) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(RequestTargetDetail{}, "ID", "PaidAt"), + cmpopts.SortSlices(func(l, r *RequestTargetDetail) bool { + return l.Target.ID() < r.Target.ID() + })) + exp := []*RequestTargetDetail{ + {Target: target1.Target, Amount: target1.Amount, CreatedAt: time.Now()}, + {Target: target2.Target, Amount: target2.Amount, CreatedAt: time.Now()}, } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("Success2", func(t *testing.T) { @@ -81,7 +84,7 @@ func TestEntRepository_GetRequestTargets(t *testing.T) { require.NoError(t, err) got, err := repo2.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) } @@ -121,17 +124,17 @@ func TestEntRepository_createRequestTargets(t *testing.T) { nil, []*RequestTarget{target1, target2}, nil, user1.ID) assert.NoError(t, err) - if got.Targets[0].Target == target1.Target { - assert.Equal(t, got.Targets[0].Target, target1.Target) - assert.Equal(t, got.Targets[0].Amount, target1.Amount) - assert.Equal(t, got.Targets[1].Target, target2.Target) - assert.Equal(t, got.Targets[1].Amount, target2.Amount) - } else { - assert.Equal(t, got.Targets[0].Target, target2.Target) - assert.Equal(t, got.Targets[0].Amount, target2.Amount) - assert.Equal(t, got.Targets[1].Target, target1.Target) - assert.Equal(t, got.Targets[1].Amount, target1.Amount) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(RequestTargetDetail{}, "ID", "PaidAt"), + cmpopts.SortSlices(func(l, r *RequestTargetDetail) bool { + return l.Target.ID() < r.Target.ID() + })) + exp := []*RequestTargetDetail{ + {Target: target1.Target, Amount: target1.Amount, CreatedAt: time.Now()}, + {Target: target2.Target, Amount: target2.Amount, CreatedAt: time.Now()}, } + testutil.RequireEqual(t, exp, got.Targets, opts...) }) } @@ -184,7 +187,7 @@ func TestEntRepository_deleteRequestTargets(t *testing.T) { assert.NoError(t, err) got, err := repo.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) t.Run("Success2", func(t *testing.T) { diff --git a/model/tag_impl_test.go b/model/tag_impl_test.go index 86fac005..d734d002 100644 --- a/model/tag_impl_test.go +++ b/model/tag_impl_test.go @@ -3,9 +3,12 @@ package model import ( "context" "testing" + "time" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/traPtitech/Jomon/testutil" "github.com/traPtitech/Jomon/testutil/random" ) @@ -25,24 +28,20 @@ func TestEntRepository_GetTags(t *testing.T) { got, err := repo.GetTags(ctx) assert.NoError(t, err) - if assert.Len(t, got, 2) && got[0].ID == tag1.ID { - assert.Equal(t, got[0].ID, tag1.ID) - assert.Equal(t, got[0].Name, tag1.Name) - assert.Equal(t, got[1].ID, tag2.ID) - assert.Equal(t, got[1].Name, tag2.Name) - } else if assert.Len(t, got, 2) { - assert.Equal(t, got[0].ID, tag2.ID) - assert.Equal(t, got[0].Name, tag2.Name) - assert.Equal(t, got[1].ID, tag1.ID) - assert.Equal(t, got[1].Name, tag1.Name) - } + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(l, r *Tag) bool { + return l.ID.ID() < r.ID.ID() + })) + exp := []*Tag{tag1, tag2} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("Success2", func(t *testing.T) { t.Parallel() got, err := repo2.GetTags(ctx) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) } @@ -58,7 +57,16 @@ func TestEntRepository_CreateTag(t *testing.T) { tag, err := repo.CreateTag(ctx, name) assert.NoError(t, err) - assert.Equal(t, name, tag.Name) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(Tag{}, "ID")) + exp := &Tag{ + Name: name, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, tag, opts...) }) t.Run("MissingName", func(t *testing.T) { @@ -78,16 +86,23 @@ func TestEntRepository_UpdateTag(t *testing.T) { t.Run("Success1", func(t *testing.T) { t.Parallel() - tag, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) + created, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) assert.NoError(t, err) name := random.AlphaNumeric(t, 20) - updated, err := repo.UpdateTag(ctx, tag.ID, name) + updated, err := repo.UpdateTag(ctx, created.ID, name) assert.NoError(t, err) - assert.Equal(t, tag.ID, updated.ID) - assert.Equal(t, name, updated.Name) + opts := testutil.ApproxEqualOptions() + exp := &Tag{ + ID: created.ID, + Name: name, + CreatedAt: created.CreatedAt, + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, updated, opts...) }) t.Run("Success2", func(t *testing.T) { @@ -98,8 +113,8 @@ func TestEntRepository_UpdateTag(t *testing.T) { updated, err := repo.UpdateTag(ctx, tag.ID, tag.Name) assert.NoError(t, err) - assert.Equal(t, tag.ID, updated.ID) - assert.Equal(t, tag.Name, updated.Name) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, tag, updated, opts...) }) t.Run("MissingName", func(t *testing.T) { diff --git a/model/transaction_detail_impl_test.go b/model/transaction_detail_impl_test.go index 046f6883..bc118967 100644 --- a/model/transaction_detail_impl_test.go +++ b/model/transaction_detail_impl_test.go @@ -3,9 +3,12 @@ package model import ( "context" "testing" + "time" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/traPtitech/Jomon/testutil" "github.com/traPtitech/Jomon/testutil/random" ) @@ -30,14 +33,21 @@ func TestEntRepository_createTransactionDetail(t *testing.T) { title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 10) - // Create TransactionDetail td, err := repo.createTransactionDetail(ctx, tx, title, amount, target) assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) - assert.NotNil(t, td) - assert.Equal(t, td.Amount, amount) - assert.Equal(t, td.Target, target) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.IgnoreFields(TransactionDetail{}, "ID")) + exp := &TransactionDetail{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } + testutil.RequireEqual(t, exp, td, opts...) }) } @@ -77,8 +87,15 @@ func TestEntRepository_updateTransactionDetail(t *testing.T) { assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) - assert.NotNil(t, td) - assert.Equal(t, td.Amount, updatedAmount) - assert.Equal(t, td.Target, updatedTarget) + opts := testutil.ApproxEqualOptions() + exp := &TransactionDetail{ + ID: td.ID, + Title: updateTitle, + Amount: updatedAmount, + Target: updatedTarget, + CreatedAt: td.CreatedAt, + UpdatedAt: time.Now(), + } + testutil.RequireEqual(t, exp, td, opts...) }) } diff --git a/model/transaction_impl_test.go b/model/transaction_impl_test.go index a69f9444..e9bd4795 100644 --- a/model/transaction_impl_test.go +++ b/model/transaction_impl_test.go @@ -5,9 +5,11 @@ import ( "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" ) @@ -83,19 +85,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { } got, err := repo.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 2) { - assert.Equal(t, tx1.ID, got[1].ID) - assert.Equal(t, tx1.Title, got[1].Title) - assert.Equal(t, tx1.Amount, got[1].Amount) - assert.Equal(t, tx1.Target, got[1].Target) - assert.Equal(t, tx1.CreatedAt, got[1].CreatedAt) - assert.Equal(t, tx1.UpdatedAt, got[1].UpdatedAt) - assert.Equal(t, tx2.ID, got[0].ID) - assert.Equal(t, tx2.Amount, got[0].Amount) - assert.Equal(t, tx2.Target, got[0].Target) - assert.Equal(t, tx2.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx2.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx2, tx1} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithSortCreatedAtDesc", func(t *testing.T) { @@ -134,19 +127,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { } got, err := repo2.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 2) { - assert.Equal(t, tx1.ID, got[0].ID) - assert.Equal(t, tx1.Title, got[0].Title) - assert.Equal(t, tx1.Amount, got[0].Amount) - assert.Equal(t, tx1.Target, got[0].Target) - assert.Equal(t, tx1.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx1.UpdatedAt, got[0].UpdatedAt) - assert.Equal(t, tx2.ID, got[1].ID) - assert.Equal(t, tx2.Amount, got[1].Amount) - assert.Equal(t, tx2.Target, got[1].Target) - assert.Equal(t, tx2.CreatedAt, got[1].CreatedAt) - assert.Equal(t, tx2.UpdatedAt, got[1].UpdatedAt) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx1, tx2} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithSortAmount", func(t *testing.T) { @@ -184,19 +168,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { } got, err := repo3.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 2) { - assert.Equal(t, tx1.ID, got[0].ID) - assert.Equal(t, tx1.Title, got[0].Title) - assert.Equal(t, tx1.Amount, got[0].Amount) - assert.Equal(t, tx1.Target, got[0].Target) - assert.Equal(t, tx1.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx1.UpdatedAt, got[0].UpdatedAt) - assert.Equal(t, tx2.ID, got[1].ID) - assert.Equal(t, tx2.Amount, got[1].Amount) - assert.Equal(t, tx2.Target, got[1].Target) - assert.Equal(t, tx2.CreatedAt, got[1].CreatedAt) - assert.Equal(t, tx2.UpdatedAt, got[1].UpdatedAt) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx1, tx2} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithSortAmountDesc", func(t *testing.T) { @@ -241,19 +216,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { // nolint:contextcheck got, err := repo4.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 2) { - assert.Equal(t, tx2.ID, got[0].ID) - assert.Equal(t, tx2.Title, got[0].Title) - assert.Equal(t, tx2.Amount, got[0].Amount) - assert.Equal(t, tx2.Target, got[0].Target) - assert.Equal(t, tx2.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx2.UpdatedAt, got[0].UpdatedAt) - assert.Equal(t, tx1.ID, got[1].ID) - assert.Equal(t, tx1.Amount, got[1].Amount) - assert.Equal(t, tx1.Target, got[1].Target) - assert.Equal(t, tx1.CreatedAt, got[1].CreatedAt) - assert.Equal(t, tx1.UpdatedAt, got[1].UpdatedAt) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx2, tx1} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithNoneSort", func(t *testing.T) { @@ -292,19 +258,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { } got, err := repo5.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 2) { - assert.Equal(t, tx1.ID, got[1].ID) - assert.Equal(t, tx1.Title, got[1].Title) - assert.Equal(t, tx1.Amount, got[1].Amount) - assert.Equal(t, tx1.Target, got[1].Target) - assert.Equal(t, tx1.CreatedAt, got[1].CreatedAt) - assert.Equal(t, tx1.UpdatedAt, got[1].UpdatedAt) - assert.Equal(t, tx2.ID, got[0].ID) - assert.Equal(t, tx2.Amount, got[0].Amount) - assert.Equal(t, tx2.Target, got[0].Target) - assert.Equal(t, tx2.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx2.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx2, tx1} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithTarget", func(t *testing.T) { @@ -342,14 +299,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { } got, err := repo6.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 1) { - assert.Equal(t, tx.ID, got[0].ID) - assert.Equal(t, tx.Title, got[0].Title) - assert.Equal(t, tx.Amount, got[0].Amount) - assert.Equal(t, tx.Target, got[0].Target) - assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 1) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithSinceUntil", func(t *testing.T) { @@ -392,13 +345,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { got, err := repo7.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 1) { - assert.Equal(t, tx.ID, got[0].ID) - assert.Equal(t, tx.Amount, got[0].Amount) - assert.Equal(t, tx.Target, got[0].Target) - assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 1) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithTag", func(t *testing.T) { @@ -442,14 +392,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { got, err := repo8.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 1) { - assert.Equal(t, tx.ID, got[0].ID) - assert.Equal(t, tx.Title, got[0].Title) - assert.Equal(t, tx.Amount, got[0].Amount) - assert.Equal(t, tx.Target, got[0].Target) - assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 1) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithGroup", func(t *testing.T) { @@ -495,14 +441,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { got, err := repo9.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 1) { - assert.Equal(t, tx.ID, got[0].ID) - assert.Equal(t, tx.Title, got[0].Title) - assert.Equal(t, tx.Amount, got[0].Amount) - assert.Equal(t, tx.Target, got[0].Target) - assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 1) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("SuccessWithRequest", func(t *testing.T) { @@ -541,14 +483,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { got, err := repo10.GetTransactions(ctx, query) assert.NoError(t, err) - if assert.Len(t, got, 1) { - assert.Equal(t, tx.ID, got[0].ID) - assert.Equal(t, tx.Title, got[0].Title) - assert.Equal(t, tx.Amount, got[0].Amount) - assert.Equal(t, tx.Target, got[0].Target) - assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) - assert.Equal(t, tx.UpdatedAt, got[0].UpdatedAt) - } + assert.Len(t, got, 1) + opts := testutil.ApproxEqualOptions() + exp := []*TransactionResponse{tx} + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("Success", func(t *testing.T) { @@ -558,7 +496,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { query := TransactionQuery{} got, err := repo11.GetTransactions(ctx, query) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) } @@ -597,14 +535,9 @@ func TestEntRepository_GetTransaction(t *testing.T) { // Get Transaction got, err := repo.GetTransaction(ctx, tx.ID) assert.NoError(t, err) - if assert.NotNil(t, got) { - assert.Equal(t, tx.ID, got.ID) - assert.Equal(t, tx.Title, got.Title) - assert.Equal(t, tx.Amount, got.Amount) - assert.Equal(t, tx.Target, got.Target) - assert.Equal(t, tx.CreatedAt, got.CreatedAt) - assert.Equal(t, tx.UpdatedAt, got.UpdatedAt) - } + assert.NotNil(t, got) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, tx, got, opts...) }) } @@ -656,21 +589,21 @@ func TestEntRepository_CreateTransaction(t *testing.T) { title, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - if assert.Len(t, tx.Tags, 1) { - assert.Equal(t, tag.ID, tx.Tags[0].ID) - assert.Equal(t, tag.Name, tx.Tags[0].Name) - } - if assert.NotNil(t, tx.Group) { - assert.Equal(t, group.ID, tx.Group.ID) - assert.Equal(t, group.Name, tx.Group.Name) - assert.Equal(t, group.Description, tx.Group.Description) - assert.Equal(t, group.Budget, tx.Group.Budget) - } + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "ID", "UpdatedAt")) + exp := &TransactionResponse{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Request: &request.ID, + Tags: []*Tag{tag}, + Group: group, } + testutil.RequireEqual(t, exp, tx, opts...) }) t.Run("SuccessWithoutTags", func(t *testing.T) { @@ -708,18 +641,21 @@ func TestEntRepository_CreateTransaction(t *testing.T) { tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, &group.ID, &request.ID) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - assert.Len(t, tx.Tags, 0) - if assert.NotNil(t, tx.Group) { - assert.Equal(t, group.ID, tx.Group.ID) - assert.Equal(t, group.Name, tx.Group.Name) - assert.Equal(t, group.Description, tx.Group.Description) - assert.Equal(t, group.Budget, tx.Group.Budget) - } + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "ID", "UpdatedAt")) + exp := &TransactionResponse{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Request: &request.ID, + Tags: []*Tag{}, + Group: group, } + testutil.RequireEqual(t, exp, tx, opts...) }) t.Run("SuccessWithoutGroup", func(t *testing.T) { @@ -756,16 +692,21 @@ func TestEntRepository_CreateTransaction(t *testing.T) { title, amount, target, []*uuid.UUID{&tag.ID}, nil, &request.ID) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - if assert.Len(t, tx.Tags, 1) { - assert.Equal(t, tag.ID, tx.Tags[0].ID) - assert.Equal(t, tag.Name, tx.Tags[0].Name) - } - assert.Nil(t, tx.Group) + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "ID", "UpdatedAt")) + exp := &TransactionResponse{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Request: &request.ID, + Tags: []*Tag{tag}, + Group: nil, } + testutil.RequireEqual(t, exp, tx, opts...) }) t.Run("Success", func(t *testing.T) { @@ -779,13 +720,21 @@ func TestEntRepository_CreateTransaction(t *testing.T) { tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - assert.Len(t, tx.Tags, 0) - assert.Nil(t, tx.Group) + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "ID", "UpdatedAt")) + exp := &TransactionResponse{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Request: nil, + Tags: []*Tag{}, + Group: nil, } + testutil.RequireEqual(t, exp, tx, opts...) }) t.Run("SuccessWithNegativeAmount", func(t *testing.T) { @@ -799,13 +748,20 @@ func TestEntRepository_CreateTransaction(t *testing.T) { tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - assert.Len(t, tx.Tags, 0) - assert.Nil(t, tx.Group) + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "ID", "UpdatedAt")) + exp := &TransactionResponse{ + Title: title, + Amount: amount, + Target: target, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Tags: []*Tag{}, + Group: nil, } + testutil.RequireEqual(t, exp, tx, opts...) }) } @@ -884,25 +840,26 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx, err = repo.UpdateTransaction( + updated, err := repo.UpdateTransaction( ctx, tx.ID, title, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) - if assert.NotNil(t, tx) { - assert.Equal(t, title, tx.Title) - assert.Equal(t, amount, tx.Amount) - assert.Equal(t, target, tx.Target) - if assert.Len(t, tx.Tags, 1) { - assert.Equal(t, tag.ID, tx.Tags[0].ID) - assert.Equal(t, tag.Name, tx.Tags[0].Name) - } - if assert.NotNil(t, tx.Group) { - assert.Equal(t, group.ID, tx.Group.ID) - assert.Equal(t, group.Name, tx.Group.Name) - assert.Equal(t, group.Description, tx.Group.Description) - assert.Equal(t, group.Budget, tx.Group.Budget) - } + opts := testutil.ApproxEqualOptions() + // FIXME: #831 + opts = append(opts, + cmpopts.IgnoreFields(TransactionResponse{}, "UpdatedAt")) + exp := &TransactionResponse{ + ID: tx.ID, + Title: title, + Amount: amount, + Target: target, + CreatedAt: tx.CreatedAt, + UpdatedAt: time.Now(), + Request: &request.ID, + Tags: []*Tag{tag}, + Group: group, } + testutil.RequireEqual(t, exp, updated, opts...) }) } diff --git a/model/user_impl_test.go b/model/user_impl_test.go index 905060a7..50be2632 100644 --- a/model/user_impl_test.go +++ b/model/user_impl_test.go @@ -3,9 +3,12 @@ package model import ( "context" "testing" + "time" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/traPtitech/Jomon/testutil" "github.com/traPtitech/Jomon/testutil/random" ) @@ -24,7 +27,7 @@ func TestEntRepository_GetUsers(t *testing.T) { got, err := repo.GetUsers(ctx) assert.NoError(t, err) - assert.Len(t, got, 0) + assert.Empty(t, got) }) t.Run("Success2", func(t *testing.T) { @@ -38,25 +41,14 @@ func TestEntRepository_GetUsers(t *testing.T) { got, err := repo2.GetUsers(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[0].Name, user1.Name) - assert.Equal(t, got[0].DisplayName, user1.DisplayName) - assert.Equal(t, got[0].Admin, user1.Admin) - assert.Equal(t, got[1].ID, user2.ID) - assert.Equal(t, got[1].Name, user2.Name) - assert.Equal(t, got[1].DisplayName, user2.DisplayName) - assert.Equal(t, got[1].Admin, user2.Admin) - } else if assert.Len(t, got, 2) { - assert.Equal(t, got[0].ID, user2.ID) - assert.Equal(t, got[0].Name, user2.Name) - assert.Equal(t, got[0].DisplayName, user2.DisplayName) - assert.Equal(t, got[0].Admin, user2.Admin) - assert.Equal(t, got[1].ID, user1.ID) - assert.Equal(t, got[1].Name, user1.Name) - assert.Equal(t, got[1].DisplayName, user1.DisplayName) - assert.Equal(t, got[1].Admin, user1.Admin) - } + assert.Len(t, got, 2) + opts := testutil.ApproxEqualOptions() + opts = append(opts, + cmpopts.SortSlices(func(l, r *User) bool { + return l.ID.ID() < r.ID.ID() + })) + exp := []*User{user1, user2} + testutil.RequireEqual(t, exp, got, opts...) }) } @@ -76,9 +68,17 @@ func TestEntRepository_CreateUser(t *testing.T) { user, err := repo.CreateUser(ctx, name, dn, admin) assert.NoError(t, err) - assert.Equal(t, user.Name, name) - assert.Equal(t, user.DisplayName, dn) - assert.Equal(t, user.Admin, admin) + opts := testutil.ApproxEqualOptions() + opts = append(opts, cmpopts.IgnoreFields(User{}, "ID")) + exp := &User{ + Name: name, + DisplayName: dn, + Admin: admin, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + DeletedAt: nil, + } + testutil.RequireEqual(t, exp, user, opts...) }) t.Run("MissingName", func(t *testing.T) { @@ -115,10 +115,8 @@ func TestEntRepository_GetUserByName(t *testing.T) { got, err := repo.GetUserByName(ctx, name) assert.NoError(t, err) - assert.Equal(t, user.ID, got.ID) - assert.Equal(t, user.Name, got.Name) - assert.Equal(t, user.DisplayName, got.DisplayName) - assert.Equal(t, user.Admin, got.Admin) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, user, got, opts...) }) t.Run("UnknownName", func(t *testing.T) { @@ -150,10 +148,8 @@ func TestEntRepository_GetUserByID(t *testing.T) { got, err := repo.GetUserByID(ctx, user.ID) assert.NoError(t, err) - assert.Equal(t, user.ID, got.ID) - assert.Equal(t, user.Name, got.Name) - assert.Equal(t, user.DisplayName, got.DisplayName) - assert.Equal(t, user.Admin, got.Admin) + opts := testutil.ApproxEqualOptions() + testutil.RequireEqual(t, user, got, opts...) }) t.Run("UnknownUserID", func(t *testing.T) { @@ -186,11 +182,16 @@ func TestEntRepository_UpdateUser(t *testing.T) { uadmin := random.Numeric(t, 2) == 1 got, err := repo.UpdateUser(ctx, user.ID, uname, udn, uadmin) assert.NoError(t, err) - - assert.NoError(t, err) - assert.Equal(t, got.Name, uname) - assert.Equal(t, got.DisplayName, udn) - assert.Equal(t, got.Admin, uadmin) + opts := testutil.ApproxEqualOptions() + exp := &User{ + ID: user.ID, + Name: uname, + DisplayName: udn, + Admin: uadmin, + CreatedAt: user.CreatedAt, + UpdatedAt: time.Now(), + } + testutil.RequireEqual(t, exp, got, opts...) }) t.Run("MissingName", func(t *testing.T) {