Files
gocryptotrader/database/repository/script/script_test.go
Adrian Gallagher 4651af5767 modernise: Run new gopls modernise tool against the codebase and fix minor issues (#1826)
* modernise: Run new gopls modernise tool against codebase

* Address shazbert's nits

* apichecker, gctcli: Simplify HTML scraping functions and improve depth limit handling

* refactor: Create minSyncInterval const and update order book limit handling for binance and binanceUS

* refactor: Various slice usage improvements and rename TODO

* tranches: Revert deleteByID changes due to performance decrease

Shazbert was a F1 driver in a past lifetime 🏎️

* tranches: Simply retrieve copy

Thanks to shazbert

* documentation: Sort contributors list by contributions

* tranches: Remove deadcode in deleteByID
2025-03-21 09:17:10 +11:00

112 lines
2.1 KiB
Go

package script
import (
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
"github.com/volatiletech/null"
)
var verbose = false
func TestMain(m *testing.M) {
var err error
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
testhelpers.TempDir, err = os.MkdirTemp("", "gct-temp")
if err != nil {
fmt.Printf("failed to create temp file: %v", err)
os.Exit(1)
}
if verbose {
err = testhelpers.EnableVerboseTestOutput()
if err != nil {
fmt.Printf("failed to enable verbose test output: %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(dbConn *database.Instance) error
output any
}{
{
"SQLite-Write",
&database.Config{
Driver: database.DBSQLite3,
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
},
writeScript,
testhelpers.CloseDatabase,
nil,
},
{
"Postgres-Write",
testhelpers.PostgresTestDatabase,
writeScript,
nil,
nil,
},
}
for x := range testCases {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
t.Skip("database not configured skipping test")
}
dbConn, err := testhelpers.ConnectToDatabase(test.config)
if err != nil {
t.Fatal(err)
}
if test.runner != nil {
test.runner()
}
if test.closer != nil {
err = test.closer(dbConn)
if err != nil {
t.Log(err)
}
}
})
}
}
func writeScript() {
var wg sync.WaitGroup
for x := range 20 {
wg.Add(1)
go func(x int) {
defer wg.Done()
test := fmt.Sprintf("test-%v", x)
var data null.Bytes
Event(test, test, test, data, test, test, time.Now())
}(x)
}
wg.Wait()
}