Created
January 5, 2019 07:07
-
-
Save prog470dev/2ff36b3b47e2485c590c09e974778465 to your computer and use it in GitHub Desktop.
dip package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package data | |
import ( | |
"database/sql" | |
"golang-test-sample/dip/service" | |
) | |
type UserRepositoryImpl struct { | |
DB *sql.DB | |
} | |
func (res *UserRepositoryImpl) Get(id int) (*service.User, error) { | |
user := &service.User{} | |
if err := res.DB.QueryRow("SELECT * FROM user WHERE id = ? LIMIT 1", id).Scan( | |
&user.ID, | |
&user.Name, | |
&user.Age, | |
); err != nil { | |
return nil, err | |
} | |
return user, nil | |
} | |
func (res *UserRepositoryImpl) Put(user *service.User) error { | |
_, err := res.DB.Exec("INSERT INTO user (name, age) VALUES (?, ?) ", | |
user.Name, user.Age) | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
// Good: service(内側) から data(外側) への依存(参照)が存在しない。 | |
type User struct { | |
ID int | |
Name string | |
Age int | |
} | |
type UserRepository interface { | |
Get(id int) (*User, error) | |
Put(user *User) error | |
} | |
func ResisterUser(user *User, res UserRepository) error { | |
err := res.Put(user) | |
return err | |
} | |
func FetchUser(id int, res UserRepository) (*User, error) { | |
user, err := res.Get(id) | |
if err != nil { | |
return nil, err | |
} | |
return user, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
// Good: 外側への依存が存在しない。(errorは例外) | |
import ( | |
"errors" | |
"testing" | |
) | |
type UserRepositoryMock struct { | |
Users []*User | |
} | |
func (rep *UserRepositoryMock) Get(id int) (*User, error) { | |
if id < len(rep.Users) { | |
return nil, errors.New("error.") | |
} | |
return rep.Users[id-1], nil | |
} | |
func (rep *UserRepositoryMock) Put(user *User) error { | |
rep.Users = append(rep.Users, user) | |
return nil | |
} | |
func TestUser(t *testing.T) { | |
rep := UserRepositoryMock{ | |
[]*User{}, | |
} | |
user1 := &User{ | |
1, | |
"Alice", | |
20, | |
} | |
err := ResisterUser(user1, &rep) | |
if err != nil { | |
t.Error("error.") | |
return | |
} | |
user2, err := FetchUser(1, &rep) | |
if user2.ID != 1 || user2.Name != "Alice" || user2.Age != 20 || err != nil { | |
t.Error("error.") | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment