Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(QOL) Improve test setup for logger, improve test coverage for database #447

Merged
merged 6 commits into from Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,7 +1,9 @@
package tests
package audit

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
Expand All @@ -10,16 +12,35 @@ import (
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/database/repository"
"github.com/thrasher-corp/gocryptotrader/database/repository/audit"
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
"github.com/thrasher-corp/goose"
)

func TestMain(m *testing.M) {
var err error
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
if err != nil {
fmt.Printf("failed to create temp file: %v", err)
os.Exit(1)
}

t := m.Run()

err = os.RemoveAll(testhelpers.TempDir)
if err != nil {
fmt.Printf("Failed to remove temp db file: %v", err)
}

os.Exit(t)
}

func TestAudit(t *testing.T) {
testCases := []struct {
name string
config *database.Config
runner func(t *testing.T)
closer func(t *testing.T, dbConn *database.Db) error
closer func(dbConn *database.Db) error
output interface{}
}{
{
Expand All @@ -30,7 +51,7 @@ func TestAudit(t *testing.T) {
},

writeAudit,
closeDatabase,
testhelpers.CloseDatabase,
nil,
},
{
Expand All @@ -41,19 +62,19 @@ func TestAudit(t *testing.T) {
},

readHelper,
closeDatabase,
testhelpers.CloseDatabase,
nil,
},
{
"Postgres-Write",
postgresTestDatabase,
testhelpers.PostgresTestDatabase,
writeAudit,
nil,
nil,
},
{
"Postgres-Read",
postgresTestDatabase,
testhelpers.PostgresTestDatabase,
readHelper,
nil,
nil,
Expand All @@ -64,16 +85,16 @@ func TestAudit(t *testing.T) {
test := tests

t.Run(test.name, func(t *testing.T) {
if !checkValidConfig(t, &test.config.ConnectionDetails) {
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
t.Skip("database not configured skipping test")
}

dbConn, err := connectToDatabase(t, test.config)
dbConn, err := testhelpers.ConnectToDatabase(test.config)

if err != nil {
t.Fatal(err)
}
path := filepath.Join("..", "migrations")
path := filepath.Join("..", "..", "migrations")
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
if err != nil {
t.Fatalf("failed to run migrations %v", err)
Expand All @@ -84,7 +105,7 @@ func TestAudit(t *testing.T) {
}

if test.closer != nil {
err = test.closer(t, dbConn)
err = test.closer(dbConn)
if err != nil {
t.Log(err)
}
Expand All @@ -103,7 +124,7 @@ func writeAudit(t *testing.T) {
go func(x int) {
defer wg.Done()
test := fmt.Sprintf("test-%v", x)
audit.Event(test, test, test)
Event(test, test, test)
}(x)
}

Expand All @@ -113,7 +134,7 @@ func writeAudit(t *testing.T) {
func readHelper(t *testing.T) {
t.Helper()

_, err := audit.GetEvent(time.Now().Add(-time.Hour*60), time.Now(), "asc", 1)
_, err := GetEvent(time.Now().Add(-time.Hour*60), time.Now(), "asc", 1)
if err != nil {
t.Error(err)
}
Expand Down
@@ -1,7 +1,9 @@
package tests
package script

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
Expand All @@ -10,17 +12,36 @@ import (
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/database/repository"
"github.com/thrasher-corp/gocryptotrader/database/repository/script"
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
"github.com/thrasher-corp/goose"
"github.com/volatiletech/null"
)

func TestMain(m *testing.M) {
var err error
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
if err != nil {
fmt.Printf("failed to create temp file: %v", err)
os.Exit(1)
}

t := m.Run()

err = os.RemoveAll(testhelpers.TempDir)
if err != nil {
fmt.Printf("Failed to remove temp db file: %v", err)
}

os.Exit(t)
}

func TestScript(t *testing.T) {
testCases := []struct {
name string
config *database.Config
runner func()
closer func(t *testing.T, dbConn *database.Db) error
closer func(dbConn *database.Db) error
output interface{}
}{
{
Expand All @@ -31,12 +52,12 @@ func TestScript(t *testing.T) {
},

writeScript,
closeDatabase,
testhelpers.CloseDatabase,
nil,
},
{
"Postgres-Write",
postgresTestDatabase,
testhelpers.PostgresTestDatabase,
writeScript,
nil,
nil,
Expand All @@ -47,16 +68,16 @@ func TestScript(t *testing.T) {
test := tests

t.Run(test.name, func(t *testing.T) {
if !checkValidConfig(t, &test.config.ConnectionDetails) {
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
t.Skip("database not configured skipping test")
}

dbConn, err := connectToDatabase(t, test.config)
dbConn, err := testhelpers.ConnectToDatabase(test.config)

if err != nil {
t.Fatal(err)
}
path := filepath.Join("..", "migrations")
path := filepath.Join("..", "..", "migrations")
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
if err != nil {
t.Fatalf("failed to run migrations %v", err)
Expand All @@ -67,7 +88,7 @@ func TestScript(t *testing.T) {
}

if test.closer != nil {
err = test.closer(t, dbConn)
err = test.closer(dbConn)
if err != nil {
t.Log(err)
}
Expand All @@ -85,7 +106,7 @@ func writeScript() {
defer wg.Done()
test := fmt.Sprintf("test-%v", x)
var data null.Bytes
script.Event(test, test, test, data, test, test, time.Now())
Event(test, test, test, data, test, test, time.Now())
}(x)
}
wg.Wait()
Expand Down
100 changes: 100 additions & 0 deletions database/testhelpers/test_helpers.go
@@ -0,0 +1,100 @@
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.Db, 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.Db) (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)
}