diff --git a/database/build/build_test.go b/database/build/build_test.go index 6304cd167..070744260 100644 --- a/database/build/build_test.go +++ b/database/build/build_test.go @@ -8,6 +8,7 @@ import ( "database/sql/driver" "reflect" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" "github.com/go-vela/types/library" @@ -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 +} diff --git a/database/build/clean_test.go b/database/build/clean_test.go index 05b606b3f..ff6e36556 100644 --- a/database/build/clean_test.go +++ b/database/build/clean_test.go @@ -7,7 +7,6 @@ package build import ( "reflect" "testing" - "time" "github.com/DATA-DOG/go-sqlmock" ) @@ -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) diff --git a/database/schedule/schedule_test.go b/database/schedule/schedule_test.go index c86f1f353..fc4b4c6e1 100644 --- a/database/schedule/schedule_test.go +++ b/database/schedule/schedule_test.go @@ -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" @@ -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 +} diff --git a/database/schedule/update_test.go b/database/schedule/update_test.go index 2554cefdc..07cbfbc58 100644 --- a/database/schedule/update_test.go +++ b/database/schedule/update_test.go @@ -6,7 +6,6 @@ package schedule import ( "testing" - "time" "github.com/DATA-DOG/go-sqlmock" ) @@ -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) diff --git a/database/secret/secret_test.go b/database/secret/secret_test.go index 228567344..0d6a61c25 100644 --- a/database/secret/secret_test.go +++ b/database/secret/secret_test.go @@ -8,6 +8,7 @@ import ( "database/sql/driver" "reflect" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" "github.com/go-vela/types/library" @@ -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 +} diff --git a/database/secret/update_test.go b/database/secret/update_test.go index e719e220e..fd9a24ff6 100644 --- a/database/secret/update_test.go +++ b/database/secret/update_test.go @@ -6,7 +6,6 @@ package secret import ( "testing" - "time" "github.com/DATA-DOG/go-sqlmock" "github.com/go-vela/types/library" @@ -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) diff --git a/database/service/clean_test.go b/database/service/clean_test.go index f30939880..3bd9baf15 100644 --- a/database/service/clean_test.go +++ b/database/service/clean_test.go @@ -7,7 +7,6 @@ package service import ( "reflect" "testing" - "time" "github.com/DATA-DOG/go-sqlmock" ) @@ -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) diff --git a/database/service/service_test.go b/database/service/service_test.go index 7749f43d3..c9d658769 100644 --- a/database/service/service_test.go +++ b/database/service/service_test.go @@ -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" @@ -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 +} diff --git a/database/step/clean_test.go b/database/step/clean_test.go index 22709749a..4d0c68e07 100644 --- a/database/step/clean_test.go +++ b/database/step/clean_test.go @@ -7,7 +7,6 @@ package step import ( "reflect" "testing" - "time" "github.com/DATA-DOG/go-sqlmock" ) @@ -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) diff --git a/database/step/step_test.go b/database/step/step_test.go index 26edfa5e1..136d5ca40 100644 --- a/database/step/step_test.go +++ b/database/step/step_test.go @@ -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" @@ -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 +}