Skip to content

Commit

Permalink
test: Improving code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Oct 18, 2021
1 parent b0725a7 commit 31c3371
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/store/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (a App) DoAtomic(ctx context.Context, action func(context.Context) error) e

func (a App) list(ctx context.Context, query string, args ...interface{}) ([]model.Repository, uint64, error) {
var count uint64
list := make([]model.Repository, 0)
var list []model.Repository

scanner := func(rows pgx.Rows) error {
var rawRepositoryKind string
Expand Down
29 changes: 27 additions & 2 deletions pkg/store/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,32 @@ func TestList(t *testing.T) {
2,
nil,
},
{
"invalid last",
args{
pageSize: 20,
last: "abc",
},
nil,
0,
errors.New("invalid last key"),
},
{
"last",
args{
pageSize: 20,
last: "2",
},
nil,
0,
nil,
},
{
"error",
args{
pageSize: 20,
},
[]model.Repository{},
nil,
0,
errors.New("timeout"),
},
Expand All @@ -58,7 +78,7 @@ func TestList(t *testing.T) {
args{
pageSize: 20,
},
[]model.Repository{},
nil,
0,
errors.New("invalid value `wrong` for repository kind"),
},
Expand Down Expand Up @@ -124,8 +144,13 @@ func TestList(t *testing.T) {
return scanner(enrichRows)
}
mockDatabase.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any(), []uint64{1, 2}).DoAndReturn(enrichFn)

case "last":
mockDatabase.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any(), uint64(2), uint(20)).Return(nil)

case "error":
mockDatabase.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any(), uint(20)).Return(errors.New("timeout"))

case "invalid kind":
mockRows := mocks.NewRows(ctrl)
mockRows.EXPECT().Scan(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(pointers ...interface{}) error {
Expand Down
53 changes: 53 additions & 0 deletions pkg/store/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,56 @@ func TestCreate(t *testing.T) {
})
}
}

func TestCount(t *testing.T) {
var cases = []struct {
intention string
want uint64
wantErr error
}{
{
"simple",
1,
nil,
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDatabase := mocks.NewDatabase(ctrl)

instance := App{db: mockDatabase}

switch tc.intention {
case "simple":
mockRow := mocks.NewRow(ctrl)
mockRow.EXPECT().Scan(gomock.Any()).DoAndReturn(func(pointers ...interface{}) error {
*pointers[0].(*uint64) = 1

return nil
})
dummyFn := func(_ context.Context, scanner func(pgx.Row) error, _ string, _ ...interface{}) error {
return scanner(mockRow)
}
mockDatabase.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(dummyFn)
}

got, gotErr := instance.Count(context.Background())

failed := false

if !errors.Is(gotErr, tc.wantErr) {
failed = true
} else if got != tc.want {
failed = true
}

if failed {
t.Errorf("Count() = (%d, `%s`), want (%d, `%s`)", got, gotErr, tc.want, tc.wantErr)
}
})
}
}

0 comments on commit 31c3371

Please sign in to comment.