Skip to content

Commit

Permalink
refactor: Removing some duplicates for sonar cloud
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 2, 2021
1 parent f5a5d0e commit af96186
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
31 changes: 13 additions & 18 deletions pkg/store/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func testWithMock(t *testing.T, action func(*sql.DB, sqlmock.Sqlmock)) {
}
}

func testWithTransaction(t *testing.T, mockDb *sql.DB, mock sqlmock.Sqlmock) context.Context {
mock.ExpectBegin()
tx, err := mockDb.Begin()
if err != nil {
t.Errorf("unable to create tx: %s", err)
}

return db.StoreTx(testCtx, tx)
}

func TestList(t *testing.T) {
type args struct {
page uint
Expand Down Expand Up @@ -361,12 +371,7 @@ func TestCreate(t *testing.T) {
for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
testWithMock(t, func(mockDb *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectBegin()
tx, err := mockDb.Begin()
if err != nil {
t.Errorf("unable to create tx: %s", err)
}
ctx := db.StoreTx(testCtx, tx)
ctx := testWithTransaction(t, mockDb, mock)

mock.ExpectQuery("INSERT INTO ketchup.ketchup").WithArgs(model.DefaultPattern, "0.9.0", 1, 3).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))

Expand Down Expand Up @@ -414,12 +419,7 @@ func TestUpdate(t *testing.T) {
for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
testWithMock(t, func(mockDb *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectBegin()
tx, err := mockDb.Begin()
if err != nil {
t.Errorf("unable to create tx: %s", err)
}
ctx := db.StoreTx(testCtx, tx)
ctx := testWithTransaction(t, mockDb, mock)

mock.ExpectExec("UPDATE ketchup.ketchup SET pattern = .+, version = .+").WithArgs(1, 3, model.DefaultPattern, "0.9.0").WillReturnResult(sqlmock.NewResult(0, 1))

Expand Down Expand Up @@ -463,12 +463,7 @@ func TestDelete(t *testing.T) {
for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
testWithMock(t, func(mockDb *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectBegin()
tx, err := mockDb.Begin()
if err != nil {
t.Errorf("unable to create tx: %s", err)
}
ctx := db.StoreTx(testCtx, tx)
ctx := testWithTransaction(t, mockDb, mock)

mock.ExpectExec("DELETE FROM ketchup.ketchup").WithArgs(1, 3).WillReturnResult(sqlmock.NewResult(0, 1))

Expand Down
18 changes: 6 additions & 12 deletions pkg/store/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
var (
ketchupRepository = "vibioh/ketchup"
viwsRepository = "vibioh/viws"

expectQueryVersion = "SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY"
)

func testWithMock(t *testing.T, action func(*sql.DB, sqlmock.Sqlmock)) {
Expand Down Expand Up @@ -100,9 +102,7 @@ func TestList(t *testing.T) {

switch tc.intention {
case "success":
mock.ExpectQuery(
"SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY",
).WithArgs(
mock.ExpectQuery(expectQueryVersion).WithArgs(
pq.Array([]uint64{1, 2}),
).WillReturnRows(
sqlmock.NewRows([]string{"repository_id", "pattern", "version"}).AddRow(1, model.DefaultPattern, "1.0.0").AddRow(2, model.DefaultPattern, "1.2.3"),
Expand Down Expand Up @@ -265,9 +265,7 @@ func TestGet(t *testing.T) {
case "for update":
rows.AddRow(1, ketchupRepository, "helm")

mock.ExpectQuery(
"SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY",
).WithArgs(
mock.ExpectQuery(expectQueryVersion).WithArgs(
pq.Array([]uint64{1}),
).WillReturnRows(
sqlmock.NewRows([]string{"repository_id", "pattern", "version"}).AddRow(1, model.DefaultPattern, "1.0.0"),
Expand Down Expand Up @@ -339,9 +337,7 @@ func TestGetByName(t *testing.T) {
switch tc.intention {
case "simple":
rows.AddRow(1, ketchupRepository, "github")
mock.ExpectQuery(
"SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY",
).WithArgs(
mock.ExpectQuery(expectQueryVersion).WithArgs(
pq.Array([]uint64{1}),
).WillReturnRows(
sqlmock.NewRows([]string{"repository_id", "pattern", "version"}).AddRow(1, model.DefaultPattern, "1.0.0"),
Expand Down Expand Up @@ -451,9 +447,7 @@ func TestCreate(t *testing.T) {
case "found get":
lockQuery.WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(selectQueryString).WithArgs(ketchupRepository, "github").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"}).AddRow(1, ketchupRepository, "github"))
mock.ExpectQuery(
"SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY",
).WithArgs(
mock.ExpectQuery(expectQueryVersion).WithArgs(
pq.Array([]uint64{1}),
).WillReturnRows(
sqlmock.NewRows([]string{"repository_id", "pattern", "version"}).AddRow(1, model.DefaultPattern, "1.0.0"),
Expand Down

0 comments on commit af96186

Please sign in to comment.