Skip to content

第7回の課題(大倉) #40

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 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion test/api/interfaces/database/user_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func TestStore(t *testing.T) {

// 課題にするメソッド
func TestFirstNameLike(t *testing.T) {
test_user := domain.User{
FirstName: "roger",
LastName: "federer",
}

tx := GetTestTransaction()
id, err := Store(tx, test_user)
if err != nil {
return
}
test_user.ID = id
tx.Commit()

Comment on lines +73 to +85
Copy link
Collaborator

Choose a reason for hiding this comment

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

fixture使うとymlでテストデータ管理できたりして便利なんですが、おそらくテストケース別にyml書いたり今のところはできないので何が最適解なのかなーといつも思ってます。
https://github.com/mf-https://github.com/go-testfixtures/testfixtures

func TestMain(m *testing.M) {
conn, err := sqlx.Open("mysql", "root:rootpassword@tcp(127.0.0.1:3314)/golang_study_test")
if err != nil {
panic(err.Error())
}
err = conn.Ping()
if err != nil {
panic(err)
}
db = conn
fixtures, err = testfixtures.New(
testfixtures.Database(db.DB),
testfixtures.Dialect("mysql"),
testfixtures.Directory("../../testdata/fixtures"),
)
if err != nil {
panic(err)
}
os.Exit(m.Run())
}
func prepareTestDB() {
fmt.Printf("%v", fixtures)
if err := fixtures.Load(); err != nil {
panic(err)
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

共通したテストデータを使い回す場合にはfixtureだと便利かなーというイメージです。
普通に前後でデータ作って終了時に消すでもいいと思います。

Copy link
Author

Choose a reason for hiding this comment

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

なるほどです

これ、書いてて思ったのは rspec+factory_botって便利すぎるよね
せめてgoにもfactory_bot的なmoduleがあれば・・・

Railsもfixtureは使いづらいよね・・・

Copy link
Collaborator

Choose a reason for hiding this comment

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

これは作るしかないのか。。

type args struct {
firstName string
}
Expand All @@ -79,7 +92,16 @@ func TestFirstNameLike(t *testing.T) {
want domain.Users
wantErr bool
}{
// TODO: Add test cases.
{
name: "検索にヒットする",
args: args{firstName: "rog"},
want: domain.Users{test_user},
},
{
name: "検索にヒットしない",
args: args{firstName: "nadal"},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -93,4 +115,5 @@ func TestFirstNameLike(t *testing.T) {
}
})
}
db.Exec("delete from users;")
Copy link
Collaborator

Choose a reason for hiding this comment

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

ここはdeferで書いてしまったらいいと思います。

Copy link
Author

Choose a reason for hiding this comment

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

なるほど

}