mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: convert dbauthz tests to also run with Postgres (#15862)
Another PR to address https://github.com/coder/coder/issues/15109. - adds the DisableForeignKeysAndTriggers utility, which simplifies converting tests from in-mem to postgres - converts the dbauthz test suite to pass on both the in-mem db and Postgres
This commit is contained in:
@@ -87,6 +87,18 @@ func NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub
|
||||
return db, ps, sqlDB
|
||||
}
|
||||
|
||||
var DefaultTimezone = "Canada/Newfoundland"
|
||||
|
||||
// NowInDefaultTimezone returns the current time rounded to the nearest microsecond in the default timezone
|
||||
// used by postgres in tests. Useful for object equality checks.
|
||||
func NowInDefaultTimezone() time.Time {
|
||||
loc, err := time.LoadLocation(DefaultTimezone)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return time.Now().In(loc).Round(time.Microsecond)
|
||||
}
|
||||
|
||||
func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
|
||||
t.Helper()
|
||||
|
||||
@@ -115,7 +127,7 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
|
||||
// - It has a non-UTC offset
|
||||
// - It has a fractional hour UTC offset
|
||||
// - It includes a daylight savings time component
|
||||
o.fixedTimezone = "Canada/Newfoundland"
|
||||
o.fixedTimezone = DefaultTimezone
|
||||
}
|
||||
dbName := dbNameFromConnectionURL(t, connectionURL)
|
||||
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
|
||||
@@ -318,3 +330,15 @@ func normalizeDump(schema []byte) []byte {
|
||||
|
||||
return schema
|
||||
}
|
||||
|
||||
// Deprecated: disable foreign keys was created to aid in migrating off
|
||||
// of the test-only in-memory database. Do not use this in new code.
|
||||
func DisableForeignKeysAndTriggers(t *testing.T, db database.Store) {
|
||||
err := db.DisableForeignKeysAndTriggers(context.Background())
|
||||
if t != nil {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user