generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prevent db access to dev dbs in tests (#1462)
fixes #1460 Issue says to panic, but in the end I just returned an error Example error message: > accessing non-test database "oidcauth" while testing: try adding ftltest.WithDatabase(db) as an option with ftltest.Context(...)
- Loading branch information
Showing
3 changed files
with
63 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package modulecontext | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"strconv" | ||
|
||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
) | ||
|
||
// Database represents a database connection based on a DSN | ||
// It holds a private field for the database which is accessible through moduleCtx.GetDatabase(name) | ||
type Database struct { | ||
DSN string | ||
DBType DBType | ||
isTestDB bool | ||
db *sql.DB | ||
} | ||
|
||
// NewDatabase creates a Database that can be added to ModuleContext | ||
func NewDatabase(dbType DBType, dsn string) (Database, error) { | ||
db, err := sql.Open("pgx", dsn) | ||
if err != nil { | ||
return Database{}, err | ||
} | ||
return Database{ | ||
DSN: dsn, | ||
DBType: dbType, | ||
db: db, | ||
}, nil | ||
} | ||
|
||
// NewTestDatabase creates a Database that can be added to ModuleContext | ||
// | ||
// Test databases can be used within module tests | ||
func NewTestDatabase(dbType DBType, dsn string) (Database, error) { | ||
db, err := NewDatabase(dbType, dsn) | ||
if err != nil { | ||
return Database{}, err | ||
} | ||
db.isTestDB = true | ||
return db, nil | ||
} | ||
|
||
type DBType ftlv1.ModuleContextResponse_DBType | ||
|
||
const ( | ||
DBTypePostgres = DBType(ftlv1.ModuleContextResponse_POSTGRES) | ||
) | ||
|
||
func (x DBType) String() string { | ||
switch x { | ||
case DBTypePostgres: | ||
return "Postgres" | ||
default: | ||
panic(fmt.Sprintf("unknown DB type: %s", strconv.Itoa(int(x)))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters