mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 23:16:51 +00:00
Database interface & auditing feature (#332)
* added audit manager * Basic database DOA setup * Added base config file * added sqlite support and creation of schema * added basic tests and config entry * corrected issues of database is disabled * fixed path for test * WIP * Added tests fixed config checking * reverted files back to upstream * reverted go.mod files * no more test test test * removed local testing details for psql * hello * added comments * increased ping to 30 seconds * renamed database table and added additional condition around test * removed database test details * goimport ran on all files * WIP * first attempt at migration * fixes for migration system * Migration system logger interface implemented * fixes to print functions * added write pooling pass * gofmt :D * formatted imports correctly * removed old code * added creation of migration * gofmt * :D Hello * ❌ 🏎️ * maybe one day i will remember to revert go mod files * checked err return condition correctly * first changes for PR feedback * code clean up * protect Connected with RWmutex & event with mutex * : D * we can just pretend like it never happened * MOved migrations back to source directory and added README * readme formatting update * Addd command line override for datadir * use correct var when creating a migration and confirm folder is created * Check if database version is newer than latest migration and also you know make migrations work..... * uses filepath instead of manual path to use correct path seperator * Add connection message and lower timeout * Added support for sslmode for psql * no longer force Close of database instead allow driver to maage * Added closer func to test output * sslmode added to example config
This commit is contained in:
126
database/tests/audit_test.go
Normal file
126
database/tests/audit_test.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
mg "github.com/thrasher-corp/gocryptotrader/database/migration"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/audit"
|
||||
auditPSQL "github.com/thrasher-corp/gocryptotrader/database/repository/audit/postgres"
|
||||
auditSQlite "github.com/thrasher-corp/gocryptotrader/database/repository/audit/sqlite"
|
||||
)
|
||||
|
||||
func TestAudit(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config database.Config
|
||||
audit audit.Repository
|
||||
runner func(t *testing.T)
|
||||
closer func(t *testing.T, dbConn *database.Database) error
|
||||
output interface{}
|
||||
}{
|
||||
{
|
||||
"SQLite",
|
||||
database.Config{
|
||||
Driver: "sqlite",
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: path.Join(tempDir, "./testdb.db")},
|
||||
},
|
||||
auditSQlite.Audit(),
|
||||
writeAudit,
|
||||
closeDatabase,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"Postgres",
|
||||
postgresTestDatabase,
|
||||
auditPSQL.Audit(),
|
||||
writeAudit,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
mg.MigrationDir = filepath.Join("../migration", "migrations")
|
||||
|
||||
if !checkValidConfig(t, &test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := connectToDatabase(t, &test.config)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mLogger := mg.MLogger{}
|
||||
migrations := mg.Migrator{
|
||||
Log: mLogger,
|
||||
}
|
||||
|
||||
migrations.Conn = dbConn
|
||||
|
||||
err = migrations.LoadMigrations()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = migrations.RunMigration()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.audit != nil {
|
||||
audit.Audit = test.audit
|
||||
}
|
||||
|
||||
if test.runner != nil {
|
||||
test.runner(t)
|
||||
}
|
||||
|
||||
switch v := test.output.(type) {
|
||||
|
||||
case error:
|
||||
if v.Error() != test.output.(error).Error() {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if test.closer != nil {
|
||||
err = test.closer(t, dbConn)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func writeAudit(t *testing.T) {
|
||||
t.Helper()
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for x := 0; x < 200; x++ {
|
||||
wg.Add(1)
|
||||
|
||||
go func(x int) {
|
||||
defer wg.Done()
|
||||
test := fmt.Sprintf("test-%v", x)
|
||||
audit.Event(test, test, test)
|
||||
}(x)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
148
database/tests/db_test.go
Normal file
148
database/tests/db_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
dbpsql "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
||||
dbsqlite "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
tempDir string
|
||||
|
||||
postgresTestDatabase = database.Config{
|
||||
Enabled: true,
|
||||
Driver: "postgres",
|
||||
ConnectionDetails: drivers.ConnectionDetails{
|
||||
//Host: "",
|
||||
//Port: 5432,
|
||||
//Username: "",
|
||||
//Password: "",
|
||||
//Database: "",
|
||||
//SSLMode: "",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
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(tempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
|
||||
os.Exit(t)
|
||||
}
|
||||
|
||||
func TestDatabaseConnect(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config database.Config
|
||||
closer func(t *testing.T, dbConn *database.Database) error
|
||||
output interface{}
|
||||
}{
|
||||
{
|
||||
"SQLite",
|
||||
database.Config{
|
||||
Driver: "sqlite",
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: path.Join(tempDir, "./testdb.db")},
|
||||
},
|
||||
closeDatabase,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"SQliteNoDatabase",
|
||||
database.Config{
|
||||
Driver: "sqlite",
|
||||
ConnectionDetails: drivers.ConnectionDetails{
|
||||
Host: "localhost",
|
||||
},
|
||||
},
|
||||
nil,
|
||||
database.ErrNoDatabaseProvided,
|
||||
},
|
||||
{
|
||||
name: "Postgres",
|
||||
config: postgresTestDatabase,
|
||||
output: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !checkValidConfig(t, &test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := connectToDatabase(t, &test.config)
|
||||
if err != nil {
|
||||
switch v := test.output.(type) {
|
||||
case error:
|
||||
if v.Error() != err.Error() {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if test.closer != nil {
|
||||
err = test.closer(t, dbConn)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func connectToDatabase(t *testing.T, conn *database.Config) (dbConn *database.Database, err error) {
|
||||
t.Helper()
|
||||
database.Conn.Config = conn
|
||||
|
||||
if conn.Driver == "postgres" {
|
||||
dbConn, err = dbpsql.Connect()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else if conn.Driver == "sqlite" {
|
||||
dbConn, err = dbsqlite.Connect()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
database.Conn.Connected = true
|
||||
return
|
||||
}
|
||||
|
||||
func closeDatabase(t *testing.T, conn *database.Database) (err error) {
|
||||
t.Helper()
|
||||
|
||||
if conn != nil {
|
||||
return conn.SQL.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkValidConfig(t *testing.T, config *drivers.ConnectionDetails) bool {
|
||||
t.Helper()
|
||||
|
||||
return !reflect.DeepEqual(drivers.ConnectionDetails{}, *config)
|
||||
}
|
||||
Reference in New Issue
Block a user