diff --git a/.travis.yml b/.travis.yml index 1d7f3e2..85bc80d 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 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 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) }