56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
testDb = "test.db"
|
|
backupDb = "test.db-backup"
|
|
db *VspDatabase
|
|
)
|
|
|
|
// TestDatabase runs all database tests.
|
|
func TestDatabase(t *testing.T) {
|
|
// Ensure we are starting with a clean environment.
|
|
os.Remove(testDb)
|
|
os.Remove(backupDb)
|
|
|
|
// All sub-tests to run.
|
|
tests := map[string]func(*testing.T){
|
|
"testInsertNewTicket": testInsertNewTicket,
|
|
"testGetTicketByHash": testGetTicketByHash,
|
|
"testUpdateTicket": testUpdateTicket,
|
|
"testTicketFeeExpired": testTicketFeeExpired,
|
|
"testAddressIndex": testAddressIndex,
|
|
}
|
|
|
|
for testName, test := range tests {
|
|
// Create a new blank database for each sub-test.
|
|
var err error
|
|
var wg sync.WaitGroup
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
db, err = Open(ctx, &wg, testDb, time.Hour)
|
|
if err != nil {
|
|
t.Fatalf("error creating test database: %v", err)
|
|
}
|
|
|
|
// Run the sub-test.
|
|
t.Run(testName, test)
|
|
|
|
// Request database shutdown and wait for it to complete.
|
|
cancel()
|
|
wg.Wait()
|
|
|
|
os.Remove(testDb)
|
|
os.Remove(backupDb)
|
|
}
|
|
}
|
|
|
|
// TODO: Add tests for CountTickets, GetUnconfirmedTickets, GetPendingFees,
|
|
// GetUnconfirmedFees.
|