-
Notifications
You must be signed in to change notification settings - Fork 25
第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
base: master
Are you sure you want to change the base?
第8回の課題(大倉) #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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()) | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
// echoのtestについては以下を参照 | ||
// https://echo.labstack.com/guide/testing | ||
// 課題: ユーザー名が空のケースを通す | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package database | |
|
||
import ( | ||
"sync" | ||
"sort" | ||
|
||
"github.com/jmoiron/sqlx" | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. golintに以下の様な怒られ方をするので、userID, userIDsにするのが良いと思います。
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど 👀 |
||
userIds[index] = userId | ||
index++ | ||
} | ||
sort.Ints(userIds) | ||
|
||
users := domain.Users{} | ||
for _, id := range userIds { | ||
users = append(users, r.users[id]) | ||
} | ||
|
||
return users, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍