From 73cedfd7005193e652a14d0ce50bf468688ee2d6 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Thu, 16 Nov 2017 19:24:51 -0500 Subject: [PATCH 1/4] testing: Allow DB engine and DSN to be selected with environment variables --- internal/handlers/common_test.go | 33 +++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index acd87f2..071ec05 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -174,14 +174,41 @@ func RunWith(t *testing.T, d *TestData, fn TestDataFunc) { } func RunTests(m *testing.M) int { - dsn := db.GetDSN(config.SQLite, ":memory:") - database, err := sql.Open("sqlite3", dsn) + envDbType := os.Getenv("MONEYGO_TEST_DB") + var dbType config.DbType + var dsn string + + switch envDbType { + case "", "sqlite", "sqlite3": + dbType = config.SQLite + dsn = ":memory:" + case "mariadb", "mysql": + dbType = config.MySQL + dsn = "root@127.0.0.1/moneygo_test&parseTime=true" + case "postgres", "postgresql": + dbType = config.Postgres + dsn = "postgres://postgres@localhost/moneygo_test" + default: + log.Fatalf("Invalid value for $MONEYGO_TEST_DB: %s\n", envDbType) + } + + if envDSN := os.Getenv("MONEYGO_TEST_DSN"); len(envDSN) > 0 { + dsn = envDSN + } + + dsn = db.GetDSN(dbType, dsn) + database, err := sql.Open(dbType.String(), dsn) if err != nil { log.Fatal(err) } defer database.Close() - dbmap, err := db.GetDbMap(database, config.SQLite) + dbmap, err := db.GetDbMap(database, dbType) + if err != nil { + log.Fatal(err) + } + + err = dbmap.TruncateTables() if err != nil { log.Fatal(err) } From 769a74a9414b9af08806599b675d0e9153da74b0 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Thu, 16 Nov 2017 19:27:34 -0500 Subject: [PATCH 2/4] .travis.yml: Test MySQL and Postgres in addition to SQLite --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1d7f3e2..39468c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,20 @@ go: - 1.9.x - master +services: + - mysql + - postgresql + +env: + - MONEYGO_TEST_DB=sqlite + - MONEYGO_TEST_DB=mysql MONEYGO_TEST_DSN="root@tcp(127.0.0.1)/moneygo_test?parseTime=true" + - MONEYGO_TEST_DB=postgres MONEYGO_TEST_DSN="postgres://postgres@localhost/moneygo_test" + +before_script: + - sh -c "if [ $MONEYGO_TEST_DB = 'postgres' ]; then psql -c 'DROP DATABASE IF EXISTS moneygo_test;' -U postgres; fi" + - sh -c "if [ $MONEYGO_TEST_DB = 'postgres' ]; then psql -c 'CREATE DATABASE moneygo_test;' -U postgres; fi" + - sh -c "if [ $MONEYGO_TEST_DB = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS moneygo_test;'; fi" + script: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls From e3e381b18318490bc8ed589fecf256a0e786be37 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Thu, 16 Nov 2017 20:19:00 -0500 Subject: [PATCH 3/4] .travis.yml: Disable postgres until it gets fixed --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 39468c2..85bc80d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ services: env: - MONEYGO_TEST_DB=sqlite - MONEYGO_TEST_DB=mysql MONEYGO_TEST_DSN="root@tcp(127.0.0.1)/moneygo_test?parseTime=true" - - MONEYGO_TEST_DB=postgres MONEYGO_TEST_DSN="postgres://postgres@localhost/moneygo_test" +# - MONEYGO_TEST_DB=postgres MONEYGO_TEST_DSN="postgres://postgres@localhost/moneygo_test" before_script: - sh -c "if [ $MONEYGO_TEST_DB = 'postgres' ]; then psql -c 'DROP DATABASE IF EXISTS moneygo_test;' -U postgres; fi" From a80a46d83f8158d891a12fcd50cd989ac7db2a6e Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Thu, 16 Nov 2017 20:19:25 -0500 Subject: [PATCH 4/4] Add "tcp()" to the MySQL DSN in the example config This appears to be necessary for the MySQL driver we're using --- example_config.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_config.ini b/example_config.ini index a8ec85e..861fdea 100644 --- a/example_config.ini +++ b/example_config.ini @@ -19,7 +19,7 @@ db-type = sqlite3 # # Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc" # MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name -# example DSN: "user:password@localhost/dbname&parseTime=true" +# example DSN: "user:password@tcp(localhost)/dbname&parseTime=true" # (Note: MySQL DSN's *must* include the # "parseTime=true" parameter) # Postgres documentation: https://godoc.org/github.com/lib/pq