Skip to content

Commit 58c8e07

Browse files
author
Chunting Wu
committed
Go Crash Course 04 is done
1 parent 4a14132 commit 58c8e07

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.16
55
require (
66
cloud.google.com/go/firestore v1.5.0
77
github.com/gorilla/mux v1.8.0
8+
github.com/stretchr/testify v1.7.0
89
google.golang.org/api v0.49.0
910
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
5252
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
5353
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
5454
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
55+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
5556
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5657
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5758
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -145,13 +146,17 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
145146
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
146147
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
147148
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
149+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
148150
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
149151
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
150152
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
153+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
151154
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
152155
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
153156
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
154157
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
158+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
159+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
155160
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
156161
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
157162
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -500,6 +505,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
500505
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
501506
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
502507
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
508+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
503509
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
504510
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
505511
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

service/post-service_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package service
2+
3+
import (
4+
"testing"
5+
6+
"example.com/clean-arch/entity"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
func TestValidateEmptyPost(t *testing.T) {
12+
testService := NewPostService(nil)
13+
14+
err := testService.Validate(nil)
15+
16+
assert.NotNil(t, err)
17+
assert.Equal(t, "the post is empty", err.Error())
18+
}
19+
20+
type MockRepository struct {
21+
mock.Mock
22+
}
23+
24+
func (mock *MockRepository) Save(post *entity.Post) (*entity.Post, error) {
25+
args := mock.Called()
26+
result := args.Get(0)
27+
return result.(*entity.Post), args.Error(1)
28+
}
29+
30+
func (mock *MockRepository) FindAll() ([]entity.Post, error) {
31+
args := mock.Called()
32+
result := args.Get(0)
33+
return result.([]entity.Post), args.Error(1)
34+
}
35+
36+
func TestValidateEmptyPostTitle(t *testing.T) {
37+
post := entity.Post{
38+
ID: 1,
39+
Title: "",
40+
Text: "B",
41+
}
42+
43+
testService := NewPostService(nil)
44+
45+
err := testService.Validate(&post)
46+
47+
assert.NotNil(t, err)
48+
assert.Equal(t, "the post title is empty", err.Error())
49+
}
50+
51+
func TestFindAll(t *testing.T) {
52+
mockRepo := new(MockRepository)
53+
54+
var identifier int64 = 1
55+
56+
post := entity.Post{
57+
ID: identifier,
58+
Title: "A",
59+
Text: "B",
60+
}
61+
// Setup expectations
62+
mockRepo.On("FindAll").Return([]entity.Post{post}, nil)
63+
64+
testService := NewPostService(mockRepo)
65+
66+
result, _ := testService.FindAll()
67+
68+
// Mock Assertion: Behavioral
69+
mockRepo.AssertExpectations(t)
70+
71+
// Data Assertion
72+
assert.Equal(t, identifier, result[0].ID)
73+
assert.Equal(t, "A", result[0].Title)
74+
assert.Equal(t, "B", result[0].Text)
75+
}
76+
77+
func TestCreate(t *testing.T) {
78+
mockRepo := new(MockRepository)
79+
80+
post := entity.Post{
81+
Title: "A",
82+
Text: "B",
83+
}
84+
// Setup expectations
85+
mockRepo.On("Save").Return(&post, nil)
86+
87+
testService := NewPostService(mockRepo)
88+
89+
result, err := testService.Create(&post)
90+
91+
mockRepo.AssertExpectations(t)
92+
93+
assert.NotNil(t, result.ID)
94+
assert.Equal(t, "A", result.Title)
95+
assert.Equal(t, "B", result.Text)
96+
assert.Nil(t, err)
97+
}

0 commit comments

Comments
 (0)