Skip to content

Commit

Permalink
fix(db tests): use lenient timestamp check for time.Now query matching (
Browse files Browse the repository at this point in the history
#902)

Co-authored-by: dave vader <[email protected]>
  • Loading branch information
ecrupper and plyr4 authored Jul 10, 2023
1 parent 563f226 commit 3f0c184
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 12 deletions.
15 changes: 15 additions & 0 deletions database/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"database/sql/driver"
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -266,3 +267,17 @@ type AnyArgument struct{}
func (a AnyArgument) Match(v driver.Value) bool {
return true
}

// NowTimestamp is used to test whether timestamps get updated correctly to the current time with lenience.
type NowTimestamp struct{}

// Match satisfies sqlmock.Argument interface.
func (t NowTimestamp) Match(v driver.Value) bool {
ts, ok := v.(int64)
if !ok {
return false
}
now := time.Now().Unix()

return now-ts < 10
}
3 changes: 1 addition & 2 deletions database/build/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package build
import (
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func TestBuild_Engine_CleanBuilds(t *testing.T) {

// ensure the mock expects the name query
_mock.ExpectExec(`UPDATE "builds" SET "status"=$1,"error"=$2,"finished"=$3,"deploy_payload"=$4 WHERE created < $5 AND (status = 'running' OR status = 'pending')`).
WithArgs("error", "msg", time.Now().UTC().Unix(), AnyArgument{}, 3).
WithArgs("error", "msg", NowTimestamp{}, AnyArgument{}, 3).
WillReturnResult(sqlmock.NewResult(1, 2))

_sqlite := testSqlite(t)
Expand Down
20 changes: 20 additions & 0 deletions database/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package schedule

import (
"database/sql/driver"
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -210,3 +212,21 @@ func testRepo() *library.Repo {
AllowComment: new(bool),
}
}

// This will be used with the github.com/DATA-DOG/go-sqlmock library to compare values
// that are otherwise not easily compared. These typically would be values generated
// before adding or updating them in the database.
//
// https://github.com/DATA-DOG/go-sqlmock#matching-arguments-like-timetime
type NowTimestamp struct{}

// Match satisfies sqlmock.Argument interface.
func (t NowTimestamp) Match(v driver.Value) bool {
ts, ok := v.(int64)
if !ok {
return false
}
now := time.Now().Unix()

return now-ts < 10
}
3 changes: 1 addition & 2 deletions database/schedule/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package schedule

import (
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
)
Expand Down Expand Up @@ -35,7 +34,7 @@ func TestSchedule_Engine_UpdateSchedule_Config(t *testing.T) {
_mock.ExpectExec(`UPDATE "schedules"
SET "repo_id"=$1,"active"=$2,"name"=$3,"entry"=$4,"created_at"=$5,"created_by"=$6,"updated_at"=$7,"updated_by"=$8,"scheduled_at"=$9
WHERE "id" = $10`).
WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", time.Now().UTC().Unix(), "user2", nil, 1).
WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", NowTimestamp{}, "user2", nil, 1).
WillReturnResult(sqlmock.NewResult(1, 1))

_sqlite := testSqlite(t)
Expand Down
15 changes: 15 additions & 0 deletions database/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"database/sql/driver"
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -238,3 +239,17 @@ type AnyArgument struct{}
func (a AnyArgument) Match(_ driver.Value) bool {
return true
}

// NowTimestamp is used to test whether timestamps get updated correctly to the current time with lenience.
type NowTimestamp struct{}

// Match satisfies sqlmock.Argument interface.
func (t NowTimestamp) Match(v driver.Value) bool {
ts, ok := v.(int64)
if !ok {
return false
}
now := time.Now().Unix()

return now-ts < 10
}
7 changes: 3 additions & 4 deletions database/secret/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package secret

import (
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -57,21 +56,21 @@ func TestSecret_Engine_UpdateSecret(t *testing.T) {
_mock.ExpectExec(`UPDATE "secrets"
SET "org"=$1,"repo"=$2,"team"=$3,"name"=$4,"value"=$5,"type"=$6,"images"=$7,"events"=$8,"allow_command"=$9,"created_at"=$10,"created_by"=$11,"updated_at"=$12,"updated_by"=$13
WHERE "id" = $14`).
WithArgs("foo", "bar", nil, "baz", AnyArgument{}, "repo", nil, nil, false, 1, "user", time.Now().UTC().Unix(), "user2", 1).
WithArgs("foo", "bar", nil, "baz", AnyArgument{}, "repo", nil, nil, false, 1, "user", AnyArgument{}, "user2", 1).
WillReturnResult(sqlmock.NewResult(1, 1))

// ensure the mock expects the org query
_mock.ExpectExec(`UPDATE "secrets"
SET "org"=$1,"repo"=$2,"team"=$3,"name"=$4,"value"=$5,"type"=$6,"images"=$7,"events"=$8,"allow_command"=$9,"created_at"=$10,"created_by"=$11,"updated_at"=$12,"updated_by"=$13
WHERE "id" = $14`).
WithArgs("foo", "*", nil, "bar", AnyArgument{}, "org", nil, nil, false, 1, "user", time.Now().UTC().Unix(), "user2", 2).
WithArgs("foo", "*", nil, "bar", AnyArgument{}, "org", nil, nil, false, 1, "user", AnyArgument{}, "user2", 2).
WillReturnResult(sqlmock.NewResult(1, 1))

// ensure the mock expects the shared query
_mock.ExpectExec(`UPDATE "secrets"
SET "org"=$1,"repo"=$2,"team"=$3,"name"=$4,"value"=$5,"type"=$6,"images"=$7,"events"=$8,"allow_command"=$9,"created_at"=$10,"created_by"=$11,"updated_at"=$12,"updated_by"=$13
WHERE "id" = $14`).
WithArgs("foo", nil, "bar", "baz", AnyArgument{}, "shared", nil, nil, false, 1, "user", time.Now().UTC().Unix(), "user2", 3).
WithArgs("foo", nil, "bar", "baz", AnyArgument{}, "shared", nil, nil, false, 1, "user", NowTimestamp{}, "user2", 3).
WillReturnResult(sqlmock.NewResult(1, 1))

_sqlite := testSqlite(t)
Expand Down
3 changes: 1 addition & 2 deletions database/service/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package service
import (
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func TestService_Engine_CleanService(t *testing.T) {

// ensure the mock expects the name query
_mock.ExpectExec(`UPDATE "services" SET "status"=$1,"error"=$2,"finished"=$3 WHERE created < $4 AND (status = 'running' OR status = 'pending')`).
WithArgs("error", "msg", time.Now().UTC().Unix(), 3).
WithArgs("error", "msg", NowTimestamp{}, 3).
WillReturnResult(sqlmock.NewResult(1, 2))

_sqlite := testSqlite(t)
Expand Down
22 changes: 22 additions & 0 deletions database/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package service

import (
"database/sql/driver"
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -222,3 +224,23 @@ func testService() *library.Service {
Distribution: new(string),
}
}

// This will be used with the github.com/DATA-DOG/go-sqlmock library to compare values
// that are otherwise not easily compared. These typically would be values generated
// before adding or updating them in the database.
//
// https://github.com/DATA-DOG/go-sqlmock#matching-arguments-like-timetime

// NowTimestamp is used to test whether timestamps get updated correctly to the current time with lenience.
type NowTimestamp struct{}

// Match satisfies sqlmock.Argument interface.
func (t NowTimestamp) Match(v driver.Value) bool {
ts, ok := v.(int64)
if !ok {
return false
}
now := time.Now().Unix()

return now-ts < 10
}
3 changes: 1 addition & 2 deletions database/step/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package step
import (
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func TestStep_Engine_CleanStep(t *testing.T) {

// ensure the mock expects the name query
_mock.ExpectExec(`UPDATE "steps" SET "status"=$1,"error"=$2,"finished"=$3 WHERE created < $4 AND (status = 'running' OR status = 'pending')`).
WithArgs("error", "msg", time.Now().UTC().Unix(), 3).
WithArgs("error", "msg", NowTimestamp{}, 3).
WillReturnResult(sqlmock.NewResult(1, 2))

_sqlite := testSqlite(t)
Expand Down
22 changes: 22 additions & 0 deletions database/step/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package step

import (
"database/sql/driver"
"reflect"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -223,3 +225,23 @@ func testStep() *library.Step {
Distribution: new(string),
}
}

// This will be used with the github.com/DATA-DOG/go-sqlmock library to compare values
// that are otherwise not easily compared. These typically would be values generated
// before adding or updating them in the database.
//
// https://github.com/DATA-DOG/go-sqlmock#matching-arguments-like-timetime

// NowTimestamp is used to test whether timestamps get updated correctly to the current time with lenience.
type NowTimestamp struct{}

// Match satisfies sqlmock.Argument interface.
func (t NowTimestamp) Match(v driver.Value) bool {
ts, ok := v.(int64)
if !ok {
return false
}
now := time.Now().Unix()

return now-ts < 10
}

0 comments on commit 3f0c184

Please sign in to comment.