Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapdb+mssmt: update mssmt unit tests to run against all registered db backends #770

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tapdb/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ func NewTestPostgresDB(t *testing.T) *PostgresStore {
return store
}

// NewPostgresDB is a helper function that creates a Postgres database for
// testing.
func NewPostgresDB() (*PostgresStore, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my other comment about moving the driver registration into a test file. Then we can perhaps create the empty var t testing.T there and still accept it as a parameter here?
Really looks weird having this as non-test code and initialize that variable... Also would allow us to use t.Cleanup() for the sqlFixture.TearDown().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the testing.T was a bit of a hack so I could get things working to be able to write the tests that prompted this PR in the first place.

I don't think we want to move the driver init into a test fie though, as this was created so we could write tests in the mssmt package that span all our db versions. Postgres is a bit unique in that it wants an ephemeral container active. We could have the test (which lives in mssmt) create the container itself, but then those details would leak into a package that's only concerned about the mssmt abstraction.

var t testing.T
sqlFixture := NewTestPgFixture(&t, DefaultPostgresFixtureLifetime, true)
if t.Failed() {
return nil, fmt.Errorf("unable to make postgres db")
}

store, err := NewPostgresStore(sqlFixture.GetConfig())
if err != nil {
return nil, err
}

// sqlFixture.TearDown(t)

return store, nil
}

// NewTestPostgresDBWithVersion is a helper function that creates a Postgres
// database for testing and migrates it to the given version.
func NewTestPostgresDBWithVersion(t *testing.T, version uint) *PostgresStore {
Expand Down
14 changes: 14 additions & 0 deletions tapdb/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ func NewTestSqliteDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
return sqlDB
}

// NewSqliteDbHandleFromPath is a helper function that creates a SQLite
// database handle given a database file path.
func NewSqliteDbHandleFromPath(dbPath string) (*SqliteStore, error) {
sqlDB, err := NewSqliteStore(&SqliteConfig{
DatabaseFileName: dbPath,
SkipMigrations: false,
})
if err != nil {
return nil, err
}

return sqlDB, nil
}

// NewTestSqliteDBWithVersion is a helper function that creates an SQLite
// database for testing and migrates it to the given version.
func NewTestSqliteDBWithVersion(t *testing.T, version uint) *SqliteStore {
Expand Down
7 changes: 0 additions & 7 deletions tapdb/sqlutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,6 @@ func newDbHandleFromDb(db *BaseDB) *DbHandler {
}
}

// NewDbHandleFromPath creates a new database store handle given a database file
// path.
func NewDbHandleFromPath(t *testing.T, dbPath string) *DbHandler {
db := NewTestDbHandleFromPath(t, dbPath)
return newDbHandleFromDb(db.BaseDB)
}

// NewDbHandle creates a new database store handle.
func NewDbHandle(t *testing.T) *DbHandler {
// Create a new test database with the default database file path.
Expand Down
11 changes: 10 additions & 1 deletion tapdb/test_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import (
"testing"
)

const activeTestDB = "postgres"

// NewTestDB is a helper function that creates a Postgres database for testing.
func NewTestDB(t *testing.T) *PostgresStore {
return NewTestPostgresDB(t)
}

// NewTestDbHandleFromPath is a helper function that creates a new handle to an
// existing SQLite database for testing.
// existing Postgres database for testing.
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *PostgresStore {
return NewTestPostgresDB(t)
}

// NewDbHandleFromPath is a helper function that creates a new handle to an
// existing Postgres database for testing. This version returns an error if an
// an issue is hit during init.
func NewDbHandleFromPath(dbPath string) (*PostgresStore, error) {
return NewPostgresDB()
}

// NewTestDBWithVersion is a helper function that creates a Postgres database
// for testing and migrates it to the given version.
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {
Expand Down
9 changes: 9 additions & 0 deletions tapdb/test_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

const activeTestDB = "sqlite3"

// NewTestDB is a helper function that creates an SQLite database for testing.
func NewTestDB(t *testing.T) *SqliteStore {
return NewTestSqliteDB(t)
Expand All @@ -17,6 +19,13 @@ func NewTestDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
return NewTestSqliteDbHandleFromPath(t, dbPath)
}

// NewDbHandleFromPath is a helper function that creates a new handle to an
// existing SQLite database for testing. This version returns an error if an
// issue is encountered during init.
func NewDbHandleFromPath(dbPath string) (*SqliteStore, error) {
return NewSqliteDbHandleFromPath(dbPath)
}

// NewTestDBWithVersion is a helper function that creates an SQLite database for
// testing and migrates it to the given version.
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {
Expand Down