Skip to content

Commit

Permalink
Separate statement cache test run.
Browse files Browse the repository at this point in the history
  • Loading branch information
go-jet committed Oct 24, 2024
1 parent 79405af commit c13112a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ jobs:
name: Running tests
command: gotestsum --junitfile $TEST_RESULTS/report.xml --format testname -- -coverprofile=cover.out -covermode=atomic -coverpkg=github.com/go-jet/jet/v2/postgres/...,github.com/go-jet/jet/v2/mysql/...,github.com/go-jet/jet/v2/sqlite/...,github.com/go-jet/jet/v2/qrm/...,github.com/go-jet/jet/v2/generator/...,github.com/go-jet/jet/v2/internal/... ./...

# run tests will statement caching enabled
- run: JET_TESTS_WITH_STMT_CACHE=true go test -v ./tests/postgres
# run mariaDB and cockroachdb tests. No need to collect coverage, because coverage is already included with mysql and postgres tests
- run: MY_SQL_SOURCE=MariaDB go test -v ./tests/mysql/
- run: PG_SOURCE=COCKROACH_DB go test -v ./tests/postgres/
Expand Down
5 changes: 5 additions & 0 deletions internal/jet/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (d *DB) WithStatementsCaching(enabled bool) *DB {
return d
}

// StatementsCachingEnabled returns true if statements caching is enabled
func (d *DB) StatementsCachingEnabled() bool {
return d.statementsCaching

Check warning on line 39 in internal/jet/db/db.go

View check run for this annotation

Codecov / codecov/patch

internal/jet/db/db.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}

// Begin starts sql transaction and returns wrapped Tx object.
func (d *DB) Begin() (*Tx, error) {
tx, err := d.DB.Begin()
Expand Down
53 changes: 31 additions & 22 deletions tests/postgres/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ var db *postgres.DB
var testRoot string

var source string
var skipStatementsCaching bool
var withStatementCaching bool

const CockroachDB = "COCKROACH_DB"

func init() {
source = os.Getenv("PG_SOURCE")
skipStatementsCaching = os.Getenv("JET_TESTS_NO_STMT_CACHE") == "true"
withStatementCaching = os.Getenv("JET_TESTS_WITH_STMT_CACHE") == "true"
}

func sourceIsCockroachDB() bool {
Expand All @@ -47,37 +47,46 @@ func TestMain(m *testing.M) {

setTestRoot()

for _, cachingEnabled := range []bool{false, true} {
for _, driverName := range []string{"pgx", "postgres"} {

if cachingEnabled && skipStatementsCaching {
continue //skipped by global env variable
}
fmt.Printf("\nRunning postgres tests for driver: %s, caching enabled: %t \n", driverName, withStatementCaching)

for _, driverName := range []string{"pgx", "postgres"} {
func() {
connectionString := dbconfig.PostgresConnectString

fmt.Printf("\nRunning postgres tests for driver: %s, caching enabled: %t \n", driverName, cachingEnabled)
if sourceIsCockroachDB() {
connectionString = dbconfig.CockroachConnectString
}

func() {
connectionString := dbconfig.PostgresConnectString

if sourceIsCockroachDB() {
connectionString = dbconfig.CockroachConnectString
}

sqlDB, err := sql.Open(driverName, connectionString)
sqlDB, err := sql.Open(driverName, connectionString)
if err != nil {
fmt.Println(err.Error())
panic("Failed to connect to test db")
}
db = postgres.NewDB(sqlDB).WithStatementsCaching(withStatementCaching)
defer func(db *postgres.DB) {
err := db.Close()
if err != nil {
fmt.Println(err.Error())
panic("Failed to connect to test db")
fmt.Printf("ERROR: Failed to close db connection, %v", err)
}
db = postgres.NewDB(sqlDB).WithStatementsCaching(cachingEnabled)
defer db.Close()
}(db)

runCount := 1

if withStatementCaching {
// With statement caching we run all tests twice to test caching logic.
// Unfortunately second call to m.Run does not add to code coverage
runCount = 2
}

for i := 0; i < runCount; i++ {
ret := m.Run()
if ret != 0 {
fmt.Printf("\nFAIL: Running postgres tests failed for driver: %s, caching enabled: %t \n", driverName, withStatementCaching)
os.Exit(ret)
}
}()
}
}
}()
}

}
Expand Down

0 comments on commit c13112a

Please sign in to comment.