mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Bump golangci-lint version, update Go version deps and generic code improvements * Fix wesbocket resp nil check and zip closures * Update pprof path
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package testhelpers
|
|
|
|
import (
|
|
"os"
|
|
"reflect"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/database"
|
|
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
|
psqlConn "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
|
sqliteConn "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite3"
|
|
)
|
|
|
|
var (
|
|
// TempDir temp folder for sqlite database
|
|
TempDir string
|
|
// PostgresTestDatabase postgresql database config details
|
|
PostgresTestDatabase *database.Config
|
|
)
|
|
|
|
// GetConnectionDetails returns connection details for CI or test db instances
|
|
func GetConnectionDetails() *database.Config {
|
|
_, exists := os.LookupEnv("TRAVIS")
|
|
if exists {
|
|
return &database.Config{
|
|
Enabled: true,
|
|
Driver: "postgres",
|
|
ConnectionDetails: drivers.ConnectionDetails{
|
|
Host: "localhost",
|
|
Port: 5432,
|
|
Username: "postgres",
|
|
Password: "",
|
|
Database: "gct_dev_ci",
|
|
SSLMode: "",
|
|
},
|
|
}
|
|
}
|
|
|
|
_, exists = os.LookupEnv("APPVEYOR")
|
|
if exists {
|
|
return &database.Config{
|
|
Enabled: true,
|
|
Driver: "postgres",
|
|
ConnectionDetails: drivers.ConnectionDetails{
|
|
Host: "localhost",
|
|
Port: 5432,
|
|
Username: "postgres",
|
|
Password: "Password12!",
|
|
Database: "gct_dev_ci",
|
|
SSLMode: "",
|
|
},
|
|
}
|
|
}
|
|
|
|
return &database.Config{
|
|
Enabled: true,
|
|
Driver: "postgres",
|
|
ConnectionDetails: drivers.ConnectionDetails{
|
|
// Host: "",
|
|
// Port: 5432,
|
|
// Username: "",
|
|
// Password: "",
|
|
// Database: "",
|
|
// SSLMode: "",
|
|
},
|
|
}
|
|
}
|
|
|
|
// ConnectToDatabase opens connection to database and returns pointer to instance of database.DB
|
|
func ConnectToDatabase(conn *database.Config) (dbConn *database.Instance, err error) {
|
|
database.DB.Config = conn
|
|
|
|
if conn.Driver == database.DBPostgreSQL {
|
|
dbConn, err = psqlConn.Connect()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else if conn.Driver == database.DBSQLite3 || conn.Driver == database.DBSQLite {
|
|
database.DB.DataPath = TempDir
|
|
dbConn, err = sqliteConn.Connect()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
database.DB.Connected = true
|
|
return
|
|
}
|
|
|
|
// CloseDatabase closes database connection
|
|
func CloseDatabase(conn *database.Instance) (err error) {
|
|
if conn != nil {
|
|
return conn.SQL.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckValidConfig checks if database connection details are empty
|
|
func CheckValidConfig(config *drivers.ConnectionDetails) bool {
|
|
return !reflect.DeepEqual(drivers.ConnectionDetails{}, *config)
|
|
}
|