1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-25 23:23:21 -05:00

Merge pull request #25 from aclindsa/add_mysql_postgres

Testing: Add MySQL and Postgres
This commit is contained in:
Aaron Lindsay 2017-11-16 20:36:15 -05:00 committed by GitHub
commit f9c234c2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}