-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from DATA-DOG/tests
Refactor tests to work with testcontainers
- Loading branch information
Showing
8 changed files
with
3,314 additions
and
391 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 |
---|---|---|
@@ -1,52 +1,4 @@ | ||
define MYSQL_SQL | ||
CREATE TABLE users ( | ||
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, | ||
username VARCHAR(32) NOT NULL, | ||
email VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (id), | ||
UNIQUE INDEX uniq_email (email) | ||
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB; | ||
endef | ||
|
||
define PSQL_SQL | ||
CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
username VARCHAR(32) NOT NULL, | ||
email VARCHAR(255) UNIQUE NOT NULL | ||
); | ||
endef | ||
|
||
export MYSQL_SQL | ||
MYSQL := "$$MYSQL_SQL" | ||
|
||
export PSQL_SQL | ||
PSQL := "$$PSQL_SQL" | ||
|
||
INSERTS := "INSERT INTO users (username, email) VALUES ('gopher', '[email protected]'), ('john', '[email protected]'), ('jane', '[email protected]');" | ||
|
||
MYSQLCMD=mysql | ||
ifndef CI | ||
MYSQLCMD=docker compose exec mysql mysql | ||
endif | ||
|
||
PSQLCMD=psql | ||
ifndef CI | ||
PSQLCMD=docker compose exec postgres psql | ||
endif | ||
|
||
test: MYSQL_DSN=root:pass@/txdb_test | ||
test: PSQL_DSN=postgres://postgres:pass@localhost/txdb_test | ||
test: mysql psql | ||
@go test -race -tags "mysql psql" | ||
|
||
mysql: | ||
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass -e 'DROP DATABASE IF EXISTS txdb_test' | ||
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass -e 'CREATE DATABASE txdb_test' | ||
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass txdb_test -e $(MYSQL) | ||
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass txdb_test -e $(INSERTS) | ||
|
||
psql: | ||
@$(PSQLCMD) "postgresql://postgres:[email protected]" -c 'DROP DATABASE IF EXISTS txdb_test' | ||
@$(PSQLCMD) "postgresql://postgres:[email protected]" -c 'CREATE DATABASE txdb_test' | ||
@$(PSQLCMD) "postgresql://postgres:[email protected]/txdb_test" -c $(PSQL) | ||
@$(PSQLCMD) "postgresql://postgres:[email protected]/txdb_test" -c $(INSERTS) | ||
|
||
.PHONY: test mysql psql | ||
@go test -race |
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,111 @@ | ||
package txdb_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/mysql" | ||
"github.com/testcontainers/testcontainers-go/modules/postgres" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
mysql_sql = `CREATE TABLE users ( | ||
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, | ||
username VARCHAR(32) NOT NULL, | ||
email VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (id), | ||
UNIQUE INDEX uniq_email (email) | ||
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB` | ||
|
||
psql_sql = `CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
username VARCHAR(32) NOT NULL, | ||
email VARCHAR(255) UNIQUE NOT NULL | ||
)` | ||
|
||
inserts = `INSERT INTO users (username, email) VALUES ('gopher', '[email protected]'), ('john', '[email protected]'), ('jane', '[email protected]')` | ||
|
||
testDB = "txdb_test" | ||
) | ||
|
||
// bootstrap bootstraps the database for tests. | ||
func bootstrap(t *testing.T, driver, dsn string) { | ||
db, err := sql.Open(driver, dsn) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
switch driver { | ||
case "mysql": | ||
if _, err := db.Exec(mysql_sql); err != nil { | ||
t.Fatal(err) | ||
} | ||
case "postgres": | ||
if _, err := db.Exec(psql_sql); err != nil { | ||
t.Fatal(err) | ||
} | ||
default: | ||
panic("unrecognized driver: " + driver) | ||
} | ||
if _, err := db.Exec(inserts); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func createDB(t *testing.T, driver, dsn string) { | ||
db, err := sql.Open(driver, dsn) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if _, err := db.Exec("DROP DATABASE IF EXISTS " + testDB); err != nil { | ||
t.Fatal(err) | ||
} | ||
if _, err := db.Exec("CREATE DATABASE " + testDB); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func startPostgres(t *testing.T) string { | ||
ctx := context.Background() | ||
|
||
postgresContainer, err := postgres.RunContainer(ctx, | ||
testcontainers.WithImage("docker.io/postgres:15.2-alpine"), | ||
postgres.WithDatabase(testDB), | ||
testcontainers.WithWaitStrategy( | ||
wait.ForLog("database system is ready to accept connections"). | ||
WithOccurrence(2). | ||
WithStartupTimeout(5*time.Second)), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
dsn, err := postgresContainer.ConnectionString(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return strings.TrimSuffix(dsn, testDB+"?") | ||
} | ||
|
||
func startMySQL(t *testing.T) string { | ||
ctx := context.Background() | ||
|
||
mysqlContainer, err := mysql.RunContainer(ctx, | ||
testcontainers.WithImage("mysql:8"), | ||
mysql.WithUsername("root"), | ||
mysql.WithPassword("password"), | ||
mysql.WithDatabase(testDB), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dsn, err := mysqlContainer.ConnectionString(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return strings.TrimSuffix(dsn, testDB) | ||
} |
Oops, something went wrong.