Skip to content

第8回の課題(大倉) #45

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 44 additions & 1 deletion interface/api/interfaces/controllers/user_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
mockID = 2
userJSON = `{"first_name":"John ","last_name":"Doe"}`
invalidUserJSON = `{"first_name": 1,"last_name": 2}`
noNameUserJSON = `{"first_name": "","last_name": ""}`
)

func (r *mockUserRepository) Store(db *sqlx.DB, u domain.User) (int, error) {
Expand All @@ -31,6 +32,9 @@ func (r *mockUserRepository) Store(db *sqlx.DB, u domain.User) (int, error) {
}

func (r *mockUserRepository) FindAll(db *sqlx.DB) (domain.Users, error) {
if r.isError {
return nil, errors.New("error")
}
return nil, nil
}

Expand All @@ -48,6 +52,20 @@ func newInvalidUserContext() echo.Context {
return e.NewContext(invalidReq, httptest.NewRecorder())
}

func noNameUserContext() echo.Context {
e := echo.New()
invalidReq := httptest.NewRequest(http.MethodPost, "/users", strings.NewReader(noNameUserJSON))
invalidReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
return e.NewContext(invalidReq, httptest.NewRecorder())
}
Comment on lines +55 to +60
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


func indexContext() echo.Context {
e := echo.New()
validReq := httptest.NewRequest(http.MethodGet, "/users", nil)
validReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
return e.NewContext(validReq, httptest.NewRecorder())
}
Comment on lines +62 to +67
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


// echoのtestについては以下を参照
// https://echo.labstack.com/guide/testing
// 課題: ユーザー名が空のケースを通す
Expand Down Expand Up @@ -91,6 +109,15 @@ func TestUserController_Create(t *testing.T) {
args: args{c: newInvalidUserContext()},
wantErr: true,
},
{
name: "ユーザー名が空",
fields: fields{
db: nil,
repository: &mockUserRepository{},
},
args: args{c: noNameUserContext()},
wantErr: true,
},
Comment on lines +112 to +120
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -120,7 +147,23 @@ func TestUserController_Index(t *testing.T) {
args args
wantErr bool
}{
// TODO: Add test cases.
{
name: "正常系",
fields: fields{
db: nil,
repository: &mockUserRepository{},
},
args: args{c: indexContext()},
},
{
name: "異常系",
fields: fields{
db: nil,
repository: &mockUserRepository{isError: true},
},
args: args{c: indexContext()},
wantErr: true,
Comment on lines +150 to +165
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion interface/api/interfaces/database/user_local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"sync"
"sort"

"github.com/jmoiron/sqlx"

Expand Down Expand Up @@ -40,5 +41,19 @@ func (r *onMemoryUserRepository) Store(db *sqlx.DB, u domain.User) (int, error)
// 実装は課題
// 出来ればuser_id昇順で返す
func (r *onMemoryUserRepository) FindAll(db *sqlx.DB) (domain.Users, error) {
return nil, nil
//r.usersをid順にソートする
userIds := make([]int, len(r.users))
index := 0
for userId := range r.users {
Comment on lines +45 to +47
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golintに以下の様な怒られ方をするので、userID, userIDsにするのが良いと思います。
IDは固有名詞の様なものなので、IdではなくIDが推奨だと思われます。

$ golint ./interface/api/interfaces/database/user_local_repository.go 
./interface/api/interfaces/database/user_local_repository.go:47:6: range var userId should be userID

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど 👀
確かに他の箇所は ID 表記ですね

userIds[index] = userId
index++
}
sort.Ints(userIds)

users := domain.Users{}
for _, id := range userIds {
users = append(users, r.users[id])
}

return users, nil
}