Skip to content

Commit 76d8c52

Browse files
committed
update test for remote add api with mocks
1 parent aa8e85a commit 76d8c52

File tree

7 files changed

+167
-49
lines changed

7 files changed

+167
-49
lines changed

generate_all_mocks.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ mockgen -source=git/commit/git_list_all_commit_logs.go -destination=mocks/mock_g
88
mockgen -source=git/middleware/commit.go -destination=mocks/mock_commit.go -package=mocks
99
mockgen -source=git/commit/git_commit_file_history.go -destination=mocks/mock_git_commit_file_history.go -package=mocks
1010
mockgen -source=git/remote/remote_validation.go -destination=mocks/mock_remote_validation.go -package=mocks
11-
mockgen -source=git/remote/git_remote_list.go -destination=mock_git_remote_list.go -package=mocks
11+
mockgen -source=git/remote/git_remote_list.go -destination=mocks/mock_git_remote_list.go -package=mocks
12+
mockgen -source=git/middleware/remotes.go -destination=mocks/mock_remotes.go -package=mocks

git/remote/git_remote_add.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ type Add interface {
1111
}
1212

1313
type addRemote struct {
14-
repo middleware.Repository
15-
remoteName string
16-
remoteURL string
14+
repo middleware.Repository
15+
remoteName string
16+
remoteURL string
17+
remoteValidation Validation
1718
}
1819

1920
// NewRemote adds a new remote to the target git repo
@@ -34,18 +35,19 @@ func (a addRemote) NewRemote() error {
3435
}
3536

3637
func (a addRemote) validateRemoteFields() error {
37-
validationErr := NewRemoteValidation(a.repo, a.remoteName, a.remoteURL).ValidateRemoteFields()
38+
validationErr := a.remoteValidation.ValidateRemoteFields()
3839
if validationErr != nil {
3940
return validationErr
4041
}
4142

4243
return nil
4344
}
4445

45-
func NewAddRemote(repo middleware.Repository, remoteName string, remoteURL string) Add {
46+
func NewAddRemote(repo middleware.Repository, remoteName string, remoteURL string, remoteValidation Validation) Add {
4647
return addRemote{
47-
repo: repo,
48-
remoteName: remoteName,
49-
remoteURL: remoteURL,
48+
repo: repo,
49+
remoteName: remoteName,
50+
remoteURL: remoteURL,
51+
remoteValidation: remoteValidation,
5052
}
5153
}

git/remote/git_remote_add_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package remote
22

33
import (
4+
"errors"
45
"fmt"
6+
"github.com/golang/mock/gomock"
57
git2go "github.com/libgit2/git2go/v31"
68
"github.com/neel1996/gitconvex/git/middleware"
79
"github.com/neel1996/gitconvex/global"
10+
"github.com/neel1996/gitconvex/mocks"
811
"github.com/stretchr/testify/suite"
912
"os"
1013
"testing"
1114
)
1215

1316
type RemoteAddTestSuite struct {
1417
suite.Suite
15-
repo middleware.Repository
16-
remoteName string
17-
remoteUrl string
18-
addRemote Add
18+
mockController *gomock.Controller
19+
repo middleware.Repository
20+
remotes middleware.Remotes
21+
mockRepo *mocks.MockRepository
22+
mockRemotes *mocks.MockRemotes
23+
mockRemoteValidation *mocks.MockValidation
24+
remoteName string
25+
remoteUrl string
26+
addRemote Add
27+
remoteValidation Validation
1928
}
2029

2130
func TestRemoteAddTestSuite(t *testing.T) {
@@ -28,9 +37,15 @@ func (suite *RemoteAddTestSuite) SetupTest() {
2837
fmt.Println(err)
2938
}
3039
suite.repo = middleware.NewRepository(r)
40+
suite.remotes = middleware.NewRemotes(r.Remotes)
3141
suite.remoteName = "new_origin"
3242
suite.remoteUrl = "https://github.com/neel1996/gitconvex-test.git"
33-
suite.addRemote = NewAddRemote(suite.repo, suite.remoteName, suite.remoteUrl)
43+
suite.mockController = gomock.NewController(suite.T())
44+
suite.mockRepo = mocks.NewMockRepository(suite.mockController)
45+
suite.mockRemotes = mocks.NewMockRemotes(suite.mockController)
46+
suite.mockRemoteValidation = mocks.NewMockValidation(suite.mockController)
47+
suite.remoteValidation = NewRemoteValidation(suite.repo, suite.remoteName, suite.remoteUrl)
48+
suite.addRemote = NewAddRemote(suite.mockRepo, suite.remoteName, suite.remoteUrl, suite.mockRemoteValidation)
3449
}
3550

3651
func (suite *RemoteAddTestSuite) TearDownSuite() {
@@ -42,39 +57,25 @@ func (suite *RemoteAddTestSuite) TearDownSuite() {
4257
}
4358

4459
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenNewRemoteIsAdded_ShouldReturnNil() {
45-
err := suite.addRemote.NewRemote()
46-
47-
suite.Nil(err)
48-
}
49-
50-
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenRepoIsNil_ShouldReturnError() {
51-
suite.addRemote = NewAddRemote(nil, suite.remoteName, suite.remoteUrl)
52-
53-
err := suite.addRemote.NewRemote()
54-
55-
suite.NotNil(err)
56-
}
57-
58-
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenRemoteNameIsEmpty_ShouldReturnError() {
59-
suite.addRemote = NewAddRemote(suite.repo, "", suite.remoteUrl)
60+
suite.addRemote = NewAddRemote(suite.repo, suite.remoteName, suite.remoteUrl, suite.remoteValidation)
6061

6162
err := suite.addRemote.NewRemote()
6263

63-
suite.NotNil(err)
64+
suite.Nil(err)
6465
}
6566

66-
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenRemoteUrlIsEmpty_ShouldReturnError() {
67-
suite.addRemote = NewAddRemote(suite.repo, suite.remoteName, "")
67+
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenValidationFails_ShouldReturnError() {
68+
suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(errors.New("VALIDATION_ERROR"))
6869

6970
err := suite.addRemote.NewRemote()
7071

7172
suite.NotNil(err)
7273
}
7374

7475
func (suite *RemoteAddTestSuite) TestAddNewRemote_WhenRemoteCreationFails_ShouldReturnError() {
75-
r, _ := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
76-
77-
suite.addRemote = NewAddRemote(middleware.NewRepository(r), "new_origin", "https://github.com/neel1996/gitconvex-test.git")
76+
suite.mockRemoteValidation.EXPECT().ValidateRemoteFields().Return(nil)
77+
suite.mockRepo.EXPECT().Remotes().Return(suite.mockRemotes)
78+
suite.mockRemotes.EXPECT().Create(suite.remoteName, suite.remoteUrl).Return(&git2go.Remote{}, errors.New("REMOTE_ERROR"))
7879

7980
err := suite.addRemote.NewRemote()
8081

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.16
44

55
require (
66
github.com/99designs/gqlgen v0.13.0
7-
github.com/golang/mock v1.6.0 // indirect
7+
github.com/golang/mock v1.6.0
88
github.com/google/uuid v1.1.2
99
github.com/gorilla/mux v1.8.0
1010
github.com/libgit2/git2go/v31 v31.4.10

go.sum

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg
88
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
99
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
1010
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
11-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
1211
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
1312
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1413
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -47,19 +46,16 @@ github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047 h1:zCoDWFD5
4746
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
4847
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
4948
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
50-
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
5149
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5250
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5351
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5452
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
5553
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
5654
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
57-
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
5855
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5956
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
6057
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
6158
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
62-
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
6359
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
6460
github.com/shurcooL/vfsgen v0.0.0-20180121065927-ffb13db8def0/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
6561
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
@@ -70,7 +66,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
7066
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7167
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7268
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
73-
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
7469
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
7570
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
7671
github.com/vektah/gqlparser/v2 v2.1.0 h1:uiKJ+T5HMGGQM2kRKQ8Pxw8+Zq9qhhZhz/lieYvCMns=
@@ -81,7 +76,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
8176
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c h1:9HhBz5L/UjnK9XLtiZhYAdue5BVKep3PMmS2LuPDt8k=
8277
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
8378
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
84-
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
8579
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
8680
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8781
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -95,27 +89,23 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
9589
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9690
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9791
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
98-
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso=
9992
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10093
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10194
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
10295
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
103-
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
10496
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
97+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
10598
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
10699
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
107100
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
108101
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
109102
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
110103
golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
111104
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
112-
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589 h1:rjUrONFu4kLchcZTfp3/96bR8bW8dIa8uz3cR5n0cgM=
113105
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
114-
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
115106
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
116107
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
117108
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
118-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
119109
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
120110
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
121111
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=

init/init_remote.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ func RemoteObjects(ctx context.Context) Remote {
2828
remoteName := ctx.Value(RemoteName).(string)
2929
remoteUrl := ctx.Value(RemoteUrl).(string)
3030

31-
addRemote := remote.NewAddRemote(repo, remoteName, remoteUrl)
31+
remoteValidation := remote.NewRemoteValidation(repo, remoteName, remoteUrl)
32+
33+
addRemote := remote.NewAddRemote(repo, remoteName, remoteUrl, remoteValidation)
3234
deleteRemote := remote.NewDeleteRemote(repo, remoteName)
3335
editRemote := remote.NewEditRemote(repo, remoteName, remoteUrl)
3436
listRemote := remote.NewRemoteList(repo)
35-
name := remote.NewGetRemoteName(repo, remoteUrl)
37+
name := remote.NewGetRemoteName(repo, remoteUrl, remoteValidation, listRemote)
3638
listRemoteURL := remote.NewRemoteUrlData(repo)
3739

3840
return Remote{

mocks/mock_remotes.go

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)