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

assert vs require #830

Open
wants to merge 27 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
784b869
requestsにLimitとOffsetパラメータを追加
reiroop Nov 8, 2024
ce1b7a0
:wrench: Add `github.com/google/go-cmp` directly
H1rono Dec 28, 2024
806f1f5
:adhesive_bandage: Fix `CreateRequest.Group`
H1rono Dec 28, 2024
348eda4
:white_check_mark: Cmp all fields in `GetRequests` test
H1rono Dec 28, 2024
3e8bad8
:recycle: Introduce `testutil.RequireEqual`
H1rono Dec 28, 2024
3065520
:adhesive_bandage: Fix `UpdateRequest.Statuses`
H1rono Dec 28, 2024
b49b908
:white_check_mark: Pass `model/request_impl_test.go`
H1rono Dec 28, 2024
6e26ec7
Merge branch 'v2' into feat/add_limit_offset_to_requests_parameters
reiroop Dec 28, 2024
ef85c50
fix: RequestQuery の CreatedBy が削除されていたのを修正
reiroop Dec 28, 2024
8bc772f
style: 行の長さの制限を満たすようにフォーマットを変更
reiroop Dec 28, 2024
3a79c5d
change assert to require in admin_test
Hueter57 Dec 28, 2024
ada6830
test: TestHandlers_GetRequests の RequestQuery に Limit と Offset を追加
reiroop Dec 28, 2024
2b90544
:white_check_mark: Cmp all fields in `model` tests
H1rono Dec 28, 2024
3035835
chore: run go mod tidy
reiroop Dec 29, 2024
27d5da6
Merge pull request #806 from traPtitech/feat/add_limit_offset_to_requ…
reiroop Dec 29, 2024
a31d262
:recycle: Add `EquateEmpty` to `ApproxEqualOptions`
H1rono Dec 29, 2024
127fb9b
:construction: WIP fix tests in `router`
H1rono Dec 29, 2024
6aaf5f9
:white_check_mark: Fix `router` tests
H1rono Dec 29, 2024
a49569a
Merge remote-tracking branch 'origin/v2' into test-cmp
H1rono Dec 30, 2024
a637f70
Merge pull request #829 from traPtitech/test-cmp
H1rono Dec 30, 2024
904ea3e
:recycle: Use `json.Marshal` in tests
H1rono Dec 31, 2024
7682abb
:pencil: Fix comment
H1rono Dec 31, 2024
b74aaec
:recycle: Ensure `assert.Equal` only in HTTP status code
H1rono Dec 31, 2024
0ed1a52
:recycle: `Equal` with 0 into `Empty`
H1rono Dec 31, 2024
c51d498
Merge pull request #840 from traPtitech/test-cmp2
H1rono Dec 31, 2024
ae60dd1
change assert to require in admin_test
Hueter57 Dec 28, 2024
a42e004
Merge branch 'change_assert_to_require' of github.com:traPtitech/Jomo…
Hueter57 Dec 31, 2024
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: 2 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ paths:
- $ref: "#/components/parameters/targetQuery"
- $ref: "#/components/parameters/sinceQuery"
- $ref: "#/components/parameters/untilQuery"
- $ref: "#/components/parameters/limitQuery"
- $ref: "#/components/parameters/offsetQuery"
- $ref: "#/components/parameters/tagQuery"
- $ref: "#/components/parameters/groupQuery"
- $ref: "#/components/parameters/createdByQuery"
Expand Down
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