Files
gocryptotrader/database/database_test.go
Scott 63257ce4ca Improvement: Speeding up slow tests (#707)
* Speeds up tests

* Reduces time.Sleeps, lowers CreateTestBot complexity. Breaks things

* Removal of unecessary config reads. Parallel tests. Lower times

* Speeds up recent trades results

* mini update

* zoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooom

* Removes the dupes

* Lint

* post cherrypick

* Fix rare kraken data race

* Fixes banking global issues. Fixes postgres trades

* rmline for appveyor test

* Expands timeout in event that channel is closed before send

* Fix data race

* No rows, no bows and definitely no shows

* Removes parallel from createsnapshot tests

* Extends timedmutext test a smidge. Exchange fatality

* Shorter end timeframe and bigger candle
2021-07-07 12:42:03 +10:00

214 lines
5.2 KiB
Go

package database
import (
"database/sql"
"errors"
"os"
"path/filepath"
"testing"
// import sqlite3 driver
_ "github.com/mattn/go-sqlite3"
)
func TestSetConfig(t *testing.T) {
t.Parallel()
inst := &Instance{}
err := inst.SetConfig(&Config{Verbose: true})
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.SetConfig(nil)
if !errors.Is(err, ErrNilConfig) {
t.Errorf("received %v, expected %v", err, ErrNilConfig)
}
inst = nil
err = inst.SetConfig(&Config{})
if !errors.Is(err, ErrNilInstance) {
t.Errorf("received %v, expected %v", err, ErrNilInstance)
}
}
func TestSetSQLiteConnection(t *testing.T) {
t.Parallel()
inst := &Instance{}
err := inst.SetSQLiteConnection(nil)
if !errors.Is(err, errNilSQL) {
t.Errorf("received %v, expected %v", err, errNilSQL)
}
err = inst.SetSQLiteConnection(&sql.DB{})
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
inst = nil
err = inst.SetSQLiteConnection(nil)
if !errors.Is(err, ErrNilInstance) {
t.Errorf("received %v, expected %v", err, ErrNilInstance)
}
}
func TestSetPostgresConnection(t *testing.T) {
// there is nothing actually requiring a postgres connection specifically
// so this is testing the checks and the ability to set values
// however, such settings would be bad for a sqlite connection irl
t.Parallel()
inst := &Instance{}
databaseFullLocation := filepath.Join(DB.DataPath, "TestSetPostgresConnection")
con, err := sql.Open("sqlite3", databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.SetPostgresConnection(con)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = con.Close()
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = os.Remove(databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
}
func TestSetConnected(t *testing.T) {
t.Parallel()
inst := &Instance{}
inst.SetConnected(true)
if !inst.connected {
t.Errorf("received %v, expected %v", false, true)
}
inst.SetConnected(false)
if inst.connected {
t.Errorf("received %v, expected %v", true, false)
}
}
func TestCloseConnection(t *testing.T) {
t.Parallel()
inst := &Instance{}
databaseFullLocation := filepath.Join(DB.DataPath, "TestCloseConnection")
con, err := sql.Open("sqlite3", databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.SetSQLiteConnection(con)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.CloseConnection()
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
}
func TestIsConnected(t *testing.T) {
t.Parallel()
inst := &Instance{}
inst.SetConnected(true)
if !inst.IsConnected() {
t.Errorf("received %v, expected %v", false, true)
}
inst.SetConnected(false)
if inst.IsConnected() {
t.Errorf("received %v, expected %v", true, false)
}
}
func TestGetConfig(t *testing.T) {
t.Parallel()
inst := &Instance{}
cfg := inst.GetConfig()
if cfg != nil {
t.Errorf("received %v, expected %v", cfg, nil)
}
err := inst.SetConfig(&Config{Enabled: true})
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
cfg = inst.GetConfig()
if cfg == nil {
t.Errorf("received %v, expected %v", cfg, &Config{Enabled: true})
}
}
func TestPing(t *testing.T) {
t.Parallel()
inst := &Instance{}
databaseFullLocation := filepath.Join(DB.DataPath, "TestPing")
con, err := sql.Open("sqlite3", databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.SetSQLiteConnection(con)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
inst.SetConnected(true)
err = inst.Ping()
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
inst.SQL = nil
err = inst.Ping()
if !errors.Is(err, errNilSQL) {
t.Errorf("received %v, expected %v", err, errNilSQL)
}
inst.SetConnected(false)
err = inst.Ping()
if !errors.Is(err, ErrDatabaseNotConnected) {
t.Errorf("received %v, expected %v", err, ErrDatabaseNotConnected)
}
inst = nil
err = inst.Ping()
if !errors.Is(err, ErrNilInstance) {
t.Errorf("received %v, expected %v", err, ErrNilInstance)
}
err = con.Close()
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = os.Remove(databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
}
func TestGetSQL(t *testing.T) {
t.Parallel()
inst := &Instance{}
_, err := inst.GetSQL()
if !errors.Is(err, errNilSQL) {
t.Errorf("received %v, expected %v", err, errNilSQL)
}
databaseFullLocation := filepath.Join(DB.DataPath, "TestGetSQL")
con, err := sql.Open("sqlite3", databaseFullLocation)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
err = inst.SetSQLiteConnection(con)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
_, err = inst.GetSQL()
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
inst = nil
_, err = inst.GetSQL()
if !errors.Is(err, ErrNilInstance) {
t.Errorf("received %v, expected %v", err, ErrNilInstance)
}
}