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

Get all current tests to pass with MySQL

This is done by requiring 'parseTime=true' in the MySQL DSN's (required
by github.com/go-sql-driver/mysql when Scan()ing to time.Time's), and
not forcing update counts to match if rows were updated to what they
already were.
This commit is contained in:
Aaron Lindsay 2017-10-24 20:57:55 -04:00
parent a1d294c309
commit 3869f3a3b2
5 changed files with 20 additions and 7 deletions

View File

@ -19,7 +19,9 @@ db-type = sqlite3
# #
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc" # Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name # MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@localhost/dbname" # example DSN: "user:password@localhost/dbname&parseTime=true"
# (Note: MySQL DSN's *must* include the
# "parseTime=true" parameter)
# Postgres documentation: https://godoc.org/github.com/lib/pq # Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname" # example DSN: "postgres://user:password@localhost/dbname"
db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc

View File

@ -9,6 +9,8 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gopkg.in/gorp.v1" "gopkg.in/gorp.v1"
"log"
"strings"
) )
func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) { func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) {
@ -43,3 +45,10 @@ func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) {
return dbmap, nil return dbmap, nil
} }
func GetDSN(dbtype config.DbType, dsn string) string {
if dbtype == config.MySQL && !strings.Contains(dsn, "parseTime=true") {
log.Fatalf("The DSN for MySQL MUST contain 'parseTime=True' but does not!")
}
return dsn
}

View File

@ -182,7 +182,8 @@ func RunTests(m *testing.M) int {
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
dbpath := path.Join(tmpdir, "moneygo.sqlite") dbpath := path.Join(tmpdir, "moneygo.sqlite")
database, err := sql.Open("sqlite3", "file:"+dbpath+"?cache=shared&mode=rwc") dsn := db.GetDSN(config.SQLite, "file:"+dbpath+"?cache=shared&mode=rwc")
database, err := sql.Open("sqlite3", dsn)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -316,8 +316,8 @@ func UpdateTransaction(tx *Tx, t *Transaction, user *User) error {
if err != nil { if err != nil {
return err return err
} }
if count != 1 { if count > 1 {
return errors.New("Updated more than one transaction split") return fmt.Errorf("Updated %d transaction splits while attempting to update only 1", count)
} }
delete(s_map, t.Splits[i].SplitId) delete(s_map, t.Splits[i].SplitId)
} else { } else {
@ -360,8 +360,8 @@ func UpdateTransaction(tx *Tx, t *Transaction, user *User) error {
if err != nil { if err != nil {
return err return err
} }
if count != 1 { if count > 1 {
return errors.New("Updated more than one transaction") return fmt.Errorf("Updated %d transactions (expected 1)", count)
} }
return nil return nil

View File

@ -66,7 +66,8 @@ func staticHandler(w http.ResponseWriter, r *http.Request, basedir string) {
} }
func main() { func main() {
database, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN) dsn := db.GetDSN(cfg.MoneyGo.DbType, cfg.MoneyGo.DSN)
database, err := sql.Open(cfg.MoneyGo.DBType.String(), dsn)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }