-
Notifications
You must be signed in to change notification settings - Fork 0
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
wish_cardのusecase以下を追加 #25
base: develop
Are you sure you want to change the base?
Conversation
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.
一旦テスト関連でここまで!
内部実装などはまた見ます!
@@ -0,0 +1,201 @@ | |||
package wish_card |
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.
package名は、スネークケースやキャメルケースを利用しない形でお願い!
package wish_card | |
package wishcard |
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.
承知です!パッケージなんやけど、自動生成のmockも改めて指定した方がいいですか?
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.
いや、mockに関してはそのままで大丈夫!!!
@akubi0w1
var err error | ||
// TODO: 環境変数とか使いたい気持ちもする | ||
db, err = sql.Open("mysql", "root:root@tcp(localhost:3306)/wantum?parseTime=true") | ||
if err != nil { | ||
log.Fatal("faild to connect db: ", err) | ||
} |
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.
ここだけど、素直にwantum/db/mysql
のCreateSQLInstance
を使ってあげれば良さそう!
そうしてあげれば、DBの環境依存がなくせるかな!
var err error | |
// TODO: 環境変数とか使いたい気持ちもする | |
db, err = sql.Open("mysql", "root:root@tcp(localhost:3306)/wantum?parseTime=true") | |
if err != nil { | |
log.Fatal("faild to connect db: ", err) | |
} | |
db := mysql.CreateSQLInstance() |
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.
これなんやけど、テストで必ずローカルのDB使いたいから手動で毎回繋いでいるんよね。
CreateSQLInstance
つかってる状態で、gcp導入しちゃうと、本番環境のDBでテスト始めてしまうかも知らんので、それを避けるために毎回作ってる感じです。。。
テスト用に接続用の関数作るのはありかも。。。
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.
@akubi0w1
であれば、mysql.connectLocalSQL()
をグローバル化してそれを呼び出したら良いんじゃない??
func TestInsert(t *testing.T) { | ||
t.Run("success to insert data", func(t *testing.T) { | ||
var err error | ||
ctx := context.Background() | ||
date := time.Date(2020, 9, 1, 12, 0, 0, 0, time.Local) | ||
place := &model.PlaceModel{ | ||
Name: "sample place", | ||
CreatedAt: &date, | ||
UpdatedAt: &date, | ||
} | ||
|
||
var result int | ||
err = txManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
result, err = repo.Insert(ctx, masterTx, place) | ||
return err | ||
}) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, result) | ||
}) | ||
|
||
t.Run("failed to insert data. data is nil", func(t *testing.T) { | ||
var err error | ||
ctx := context.Background() | ||
|
||
var result int | ||
err = txManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
result, err = repo.Insert(ctx, masterTx, nil) | ||
return err | ||
}) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, 0, result) | ||
}) | ||
} |
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.
ここだけじゃないんだけど、基本的にt.Run
で入れ子構造は表せないので、素直にTestPlace_Insert_Success
などで定義してあげたほうが良さそう。
現状テストを走らせた時に得られる結果↓
{"level":"INFO","ts":"2020-09-09T16:19:48.646+0900","logger":"AppLogger","caller":"mysql/connect_db.go:63","msg":"connectLocalDB: root:96ta9kfzw1803_Takkuncolon@tcp(localhost:3306)/wantum?parseTime=true"}
=== RUN TestInsert
--- PASS: TestInsert (0.01s)
=== RUN TestInsert/success_to_insert_data
--- PASS: TestInsert/success_to_insert_data (0.01s)
=== RUN TestInsert/failure_to_insert_data._data_is_nil
--- PASS: TestInsert/failure_to_insert_data._data_is_nil (0.00s)
=== RUN TestUpdate
--- PASS: TestUpdate (0.01s)
=== RUN TestUpdate/success_to_update_data
--- PASS: TestUpdate/success_to_update_data (0.00s)
=== RUN TestUpdate/success_to_update_data._done_at_is_null
--- PASS: TestUpdate/success_to_update_data._done_at_is_null (0.00s)
=== RUN TestUpdate/failure_to_update_data._data_is_nil
--- PASS: TestUpdate/failure_to_update_data._data_is_nil (0.00s)
=== RUN TestUpDeleteFlag
--- PASS: TestUpDeleteFlag (0.03s)
=== RUN TestUpDeleteFlag/success_to_up_delete_flag
--- PASS: TestUpDeleteFlag/success_to_up_delete_flag (0.02s)
=== RUN TestUpDeleteFlag/failure_to_up_delete_flag._flag_is_nil
--- PASS: TestUpDeleteFlag/failure_to_up_delete_flag._flag_is_nil (0.00s)
=== RUN TestUpDeleteFlag/failure_to_up_delete_flag._data_is_nil
--- PASS: TestUpDeleteFlag/failure_to_up_delete_flag._data_is_nil (0.00s)
=== RUN TestDownDeleteFlag
--- PASS: TestDownDeleteFlag (0.01s)
=== RUN TestDownDeleteFlag/success_to_down_delete_flag
--- PASS: TestDownDeleteFlag/success_to_down_delete_flag (0.01s)
=== RUN TestDownDeleteFlag/failure_to_up_delete_flag._data_is_nil
--- PASS: TestDownDeleteFlag/failure_to_up_delete_flag._data_is_nil (0.00s)
=== RUN TestDelete
--- PASS: TestDelete (0.01s)
=== RUN TestDelete/success_to_up_delete_flag
あと、テストの関数については日本語も使えるので、エラーの検証ケースなどは日本語で条件やケースなどを書いてあげると良さそう!
俺が前内定者バイトでいたプロジェクトでも下記の様な関数名をつけていたよー
func TestInteractor_Enhance_アクセサリ所持上限(t *testing.T) {
...
}
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.
t.Runのところ、どういうことや...?サブテストみたいな分割でやってて、テスト走ってるから良いかなーと思ってたんだが...以下見た感じでもいけそうだなーと...
https://golang.org/pkg/testing/#hdr-Subtests_and_Sub_benchmarks
日本語いけるやつか!日本語にしますーー!!
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.
なんか、出力結果見た時にサブテスト(入れ子構造)がGo側では上手い感じで表示してくれないのかなーと思って出力結果を貼ったけど、今見たら、ちゃんと入れ子構造になってたわw
t.Runに関しては気にしないでOK!
place := &model.PlaceModel{ | ||
Name: "sample place", | ||
CreatedAt: &date, | ||
UpdatedAt: &date, | ||
} |
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.
この辺の構造体定義も、1ファイルの中で2回以上出てくる様な場合は一番上でvar()宣言してあげたほうが良いかな!
"sample place"
も変数として切り出して良さそうだね。
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.
そうやね、、、使う変数周りはちょっと見直してみますー!ありがとうーー!
…/Wantum-server into feat/1/control_wish_card
レビューいただいたところを修正したりなどしましたー!お手すきの際にみていただけると幸いです(ㅅ ;´꒳`;)
|
Issue
【やりたいことリスト詳細画面】やりたいことの追加・削除・更新
close #1
概要
やりたいことのCRUDを追加。
変更内容
wish_card
,tag
,place
,wish_card_tag
において、必要なmodel, entity, repository, service, usecaseの追加と、テストの追加。usecaseについては異常系のテストを書いていない。理由は、usecaseで起きるエラーが基本的にmockから返ってくるエラーのみであるため。
テストにおいてmockを使用する場合、mockは絶対にエラーを返さない前提でテストを記述した。
動作確認方法
log
パッケージなどを用いた出力で動作確認。補足事項
必要そうな機能って感じで作ったので、必要十分かはわからん...