Skip to content

Commit c70f2b3

Browse files
committed
Added more tests, refactored code
1 parent cede079 commit c70f2b3

File tree

8 files changed

+193
-45
lines changed

8 files changed

+193
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,6 @@ Usage only `gin` package and oauth2 client/server mechanic
272272

273273
# Testing
274274
```
275-
export APP_WD=go_path to app/rsources or app/artifacts - needed for load templates
275+
export APP_WD=go_path to project_path/rsources or project_path/artifacts - needed for load templates
276276
➜ make test
277277
```

app/db/domain/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ type User struct {
88

99
// UsersRepo interface
1010
type UsersRepo interface {
11-
CreateUser(email string, password string) (*User, error)
11+
CreateUser(email string, encryptPassword string) (*User, error)
1212
FindByEmail(email string) (*User, error)
1313
}

app/db/repo/users.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@ package repo
22

33
import (
44
"github.com/aristat/golang-example-app/app/db/domain"
5-
"github.com/aristat/golang-example-app/common"
65
"github.com/jinzhu/gorm"
76
)
87

98
type UsersRepo struct {
109
db *gorm.DB
1110
}
1211

13-
func (u *UsersRepo) CreateUser(email string, password string) (*domain.User, error) {
14-
encryptedPassword, err := common.HashPassword(password, 8)
12+
func (u *UsersRepo) CreateUser(email string, encryptPassword string) (*domain.User, error) {
13+
user := &domain.User{Email: email, EncryptedPassword: encryptPassword}
1514

16-
if err != nil {
17-
return nil, err
18-
}
19-
20-
user := &domain.User{Email: email, EncryptedPassword: encryptedPassword}
2115
if err := u.db.Create(user).Error; err != nil {
2216
return nil, err
2317
}
@@ -28,7 +22,9 @@ func (u *UsersRepo) CreateUser(email string, password string) (*domain.User, err
2822
func (u *UsersRepo) FindByEmail(email string) (*domain.User, error) {
2923
user := &domain.User{}
3024

31-
err := u.db.Table("users").Select("id, email, encrypted_password").
25+
err := u.db.
26+
Table("users").
27+
Select("id, email, encrypted_password").
3228
Where("users.email = ?", email).
3329
Limit(1).
3430
Scan(&user).Error

app/db/repo/users_test.go

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,124 @@
1-
package repo
1+
package repo_test
22

33
import (
4+
"errors"
45
"testing"
56

67
"golang.org/x/crypto/bcrypt"
78

89
"github.com/aristat/golang-example-app/app/db"
10+
"github.com/aristat/golang-example-app/app/db/domain"
11+
912
mocket "github.com/selvatico/go-mocket"
13+
14+
"github.com/aristat/golang-example-app/app/db/repo"
1015
"github.com/stretchr/testify/assert"
1116
)
1217

18+
var customInsertError = errors.New("sql: custom insert error")
19+
20+
func TestCreateUser(t *testing.T) {
21+
mocket.Catcher.Logging = true
22+
23+
dbManager, _, e := db.BuildTest()
24+
assert.Nil(t, e, "DB manager error should be nil")
25+
26+
userRepo, _, e := repo.NewUsersRepo(dbManager.DB)
27+
assert.Nil(t, e, "Repo error should be nil")
28+
29+
defaultEmail := "test@gmail.com"
30+
defaultPassword := "123456789"
31+
primaryKey := 333
32+
33+
mockCreateWithError := func() {
34+
mocket.Catcher.
35+
Reset().
36+
NewMock().
37+
WithQuery("INSERT INTO \"users\" (\"email\",\"encrypted_password\") VALUES ($1,$2)").
38+
WithArgs(defaultEmail, defaultPassword).
39+
WithError(customInsertError)
40+
}
41+
mockCreate := func() {
42+
reply := []map[string]interface{}{{"id": primaryKey}}
43+
mocket.Catcher.
44+
Reset().
45+
NewMock().
46+
WithQuery("INSERT INTO \"users\" (\"email\",\"encrypted_password\") VALUES ($1,$2)").
47+
WithArgs(defaultEmail, defaultPassword).
48+
WithReply(reply)
49+
}
50+
51+
tests := []struct {
52+
name string
53+
mock func()
54+
asserts func(user *domain.User, err error)
55+
}{
56+
{
57+
name: "USER NOT CREATED",
58+
mock: mockCreateWithError,
59+
asserts: func(user *domain.User, err error) {
60+
assert.Nil(t, user, "user should be nil")
61+
assert.NotNil(t, err, "err should not be nil")
62+
assert.Equal(t, customInsertError.Error(), err.Error())
63+
},
64+
},
65+
{
66+
name: "USER CREATED",
67+
mock: mockCreate,
68+
asserts: func(user *domain.User, err error) {
69+
assert.Equal(t, primaryKey, user.ID)
70+
assert.Nil(t, err, "err should be nil")
71+
},
72+
},
73+
}
74+
75+
for _, test := range tests {
76+
t.Run(test.name, func(t *testing.T) {
77+
test.mock()
78+
79+
user, err := userRepo.CreateUser(defaultEmail, defaultPassword)
80+
test.asserts(user, err)
81+
})
82+
}
83+
}
84+
1385
func TestFindByEmail(t *testing.T) {
1486
mocket.Catcher.Logging = true
1587

1688
dbManager, _, e := db.BuildTest()
1789
assert.Nil(t, e, "DB manager error should be nil")
1890

19-
repo, _, e := NewUsersRepo(dbManager.DB)
91+
userRepo, _, e := repo.NewUsersRepo(dbManager.DB)
2092
assert.Nil(t, e, "Repo error should be nil")
2193

2294
ePassword, e := bcrypt.GenerateFromPassword([]byte("12345"), 8)
2395
assert.Nil(t, e, "Password is correct")
2496

2597
defaultEmail := "test@gmail.com"
26-
mockDefaultResult := func() {
98+
mockSelect := func() {
2799
reply := []map[string]interface{}{{"id": 1, "email": defaultEmail, "encrypted_password": ePassword}}
28100
mocket.Catcher.Reset().NewMock().WithQuery(`WHERE (users.email = $1) LIMIT 1`).WithArgs(defaultEmail).WithReply(reply)
29101
}
30102

31103
tests := []struct {
32104
name string
33-
email string
34105
mock func()
35-
asserts func(err error)
106+
asserts func(user *domain.User, err error)
36107
}{
37108
{
38-
name: "USER IS EMPTY",
39-
email: "",
40-
mock: func() {},
41-
asserts: func(err error) {
109+
name: "USER IS EMPTY",
110+
mock: func() {},
111+
asserts: func(user *domain.User, err error) {
112+
assert.Equal(t, "", user.Email, "email should be equals")
42113
assert.NotNil(t, err, "err should not be nil")
43114
assert.Equal(t, err.Error(), "record not found", "error should be corrects")
44115
},
45116
},
46117
{
47-
name: "USER EXIST",
48-
email: defaultEmail,
49-
mock: mockDefaultResult,
50-
asserts: func(err error) {
118+
name: "USER EXIST",
119+
mock: mockSelect,
120+
asserts: func(user *domain.User, err error) {
121+
assert.Equal(t, defaultEmail, user.Email, "email should be equals")
51122
assert.Nil(t, e, "err should be nil")
52123
},
53124
},
@@ -58,9 +129,8 @@ func TestFindByEmail(t *testing.T) {
58129
mocket.Catcher.Reset()
59130
test.mock()
60131

61-
user, err := repo.FindByEmail(defaultEmail)
62-
assert.Equal(t, test.email, user.Email, "email should be equals")
63-
test.asserts(err)
132+
user, err := userRepo.FindByEmail(defaultEmail)
133+
test.asserts(user, err)
64134
})
65135
}
66136
}

app/entrypoint/entrypoint_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package entrypoint_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/aristat/golang-example-app/app/entrypoint"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestInitialize(t *testing.T) {
14+
wd, _ := filepath.Abs(os.Getenv("APP_WD"))
15+
entryPoint, err := entrypoint.Initialize(wd, nil)
16+
17+
assert.Nil(t, err, "err should be nil")
18+
assert.NotNil(t, entryPoint, "entryPoint should not be nil")
19+
}

app/resolver/user.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package resolver
33
import (
44
"context"
55

6+
"github.com/aristat/golang-example-app/common"
7+
68
graphql1 "github.com/aristat/golang-example-app/generated/graphql"
79
"github.com/spf13/cast"
810
)
@@ -45,7 +47,12 @@ func (r *mutationResolver) Users(ctx context.Context) (*graphql1.UsersMutation,
4547
}
4648

4749
func (r *usersMutationResolver) CreateUser(ctx context.Context, obj *graphql1.UsersMutation, email string, password string) (*graphql1.UsersCreateOut, error) {
48-
user, err := r.repo.Users.CreateUser(email, password)
50+
encryptPassword, err := common.HashPassword(password, 8)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
user, err := r.repo.Users.CreateUser(email, encryptPassword)
4956

5057
if err != nil {
5158
return nil, err

go.mod

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ require (
1818
github.com/go-session/redis v3.0.1+incompatible
1919
github.com/go-session/session v3.1.2+incompatible
2020
github.com/gobuffalo/packr v1.30.1 // indirect
21-
github.com/golang/protobuf v1.3.2
22-
github.com/google/go-cmp v0.3.1 // indirect
21+
github.com/golang/protobuf v1.4.0
2322
github.com/google/go-querystring v1.0.0 // indirect
2423
github.com/google/wire v0.3.0
2524
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
@@ -28,14 +27,15 @@ require (
2827
github.com/jinzhu/gorm v1.9.10
2928
github.com/json-iterator/go v1.1.6 // indirect
3029
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
31-
github.com/lib/pq v1.1.1
30+
github.com/lib/pq v1.7.0
3231
github.com/magiconair/properties v1.8.1 // indirect
3332
github.com/mitchellh/mapstructure v1.1.2
3433
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3534
github.com/modern-go/reflect2 v1.0.1 // indirect
3635
github.com/moul/http2curl v1.0.0 // indirect
37-
github.com/nats-io/nats.go v1.9.2
38-
github.com/nats-io/stan.go v0.6.0
36+
github.com/nats-io/nats-streaming-server v0.18.0 // indirect
37+
github.com/nats-io/nats.go v1.10.0
38+
github.com/nats-io/stan.go v0.7.0
3939
github.com/onsi/ginkgo v1.8.0 // indirect
4040
github.com/onsi/gomega v1.5.0 // indirect
4141
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9
@@ -77,7 +77,6 @@ require (
7777
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59
7878
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b
7979
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
80-
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b // indirect
8180
google.golang.org/appengine v1.6.2 // indirect
8281
google.golang.org/genproto v0.0.0-20190905072037-92dd089d5514 // indirect
8382
google.golang.org/grpc v1.23.0

0 commit comments

Comments
 (0)