diff --git a/.circleci/config.yml b/.circleci/config.yml index 74f2bd86..d3c071fa 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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/ diff --git a/internal/jet/db/db.go b/internal/jet/db/db.go index da930034..dba490a7 100644 --- a/internal/jet/db/db.go +++ b/internal/jet/db/db.go @@ -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 +} + // Begin starts sql transaction and returns wrapped Tx object. func (d *DB) Begin() (*Tx, error) { tx, err := d.DB.Begin() diff --git a/tests/postgres/main_test.go b/tests/postgres/main_test.go index 991c6d35..6b57c8de 100644 --- a/tests/postgres/main_test.go +++ b/tests/postgres/main_test.go @@ -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 { @@ -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) } - }() - } + } + }() } }