From 31c33719a72b54b86d613b9a7b1888b0be9d1266 Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Mon, 18 Oct 2021 22:10:47 +0200 Subject: [PATCH] test: Improving code coverage Signed-off-by: Vincent Boutour --- pkg/store/repository/repository.go | 2 +- pkg/store/repository/repository_test.go | 29 +++++++++++++- pkg/store/user/user_test.go | 53 +++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/pkg/store/repository/repository.go b/pkg/store/repository/repository.go index 20b7f519..9aadf0bf 100644 --- a/pkg/store/repository/repository.go +++ b/pkg/store/repository/repository.go @@ -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 diff --git a/pkg/store/repository/repository_test.go b/pkg/store/repository/repository_test.go index 4fb0a36c..5e10ed05 100644 --- a/pkg/store/repository/repository_test.go +++ b/pkg/store/repository/repository_test.go @@ -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"), }, @@ -58,7 +78,7 @@ func TestList(t *testing.T) { args{ pageSize: 20, }, - []model.Repository{}, + nil, 0, errors.New("invalid value `wrong` for repository kind"), }, @@ -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 { diff --git a/pkg/store/user/user_test.go b/pkg/store/user/user_test.go index 471a5fef..806110c5 100644 --- a/pkg/store/user/user_test.go +++ b/pkg/store/user/user_test.go @@ -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) + } + }) + } +}