Extract common db operations for the test
This commit is contained in:
parent
7241cdbfcb
commit
adaa0c63ef
@ -11,6 +11,25 @@ import (
|
|||||||
memdb "git.defalsify.org/vise.git/db/mem"
|
memdb "git.defalsify.org/vise.git/db/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InitializeTestDb sets up and returns an in-memory database and store.
|
||||||
|
func InitializeTestDb(t *testing.T) (context.Context, *UserDataStore) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Initialize memDb
|
||||||
|
db := memdb.NewMemDb()
|
||||||
|
err := db.Connect(ctx, "")
|
||||||
|
require.NoError(t, err, "Failed to connect to memDb")
|
||||||
|
|
||||||
|
// Create UserDataStore with memDb
|
||||||
|
store := &UserDataStore{Db: db}
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
db.Close() // Ensure the DB is closed after each test
|
||||||
|
})
|
||||||
|
|
||||||
|
return ctx, store
|
||||||
|
}
|
||||||
|
|
||||||
// AssertEmptyValue checks if a value is empty/nil/zero
|
// AssertEmptyValue checks if a value is empty/nil/zero
|
||||||
func AssertEmptyValue(t *testing.T, value []byte, msgAndArgs ...interface{}) {
|
func AssertEmptyValue(t *testing.T, value []byte, msgAndArgs ...interface{}) {
|
||||||
assert.Equal(t, len(value), 0, msgAndArgs...)
|
assert.Equal(t, len(value), 0, msgAndArgs...)
|
||||||
@ -100,31 +119,20 @@ func TestGetVoucherData(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreTemporaryVoucher(t *testing.T) {
|
func TestStoreTemporaryVoucher(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx, store := InitializeTestDb(t)
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
|
|
||||||
// Initialize memDb
|
|
||||||
db := memdb.NewMemDb()
|
|
||||||
err := db.Connect(ctx, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Create UserDataStore with memDb
|
|
||||||
store := &UserDataStore{
|
|
||||||
Db: db,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test data
|
// Test data
|
||||||
voucherData := &VoucherMetadata{
|
voucherData := &VoucherMetadata{
|
||||||
Symbol: "SRF",
|
Symbol: "SRF",
|
||||||
Balance: "200",
|
Balance: "200",
|
||||||
Decimal: "6",
|
Decimal: "6",
|
||||||
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the function being tested
|
// Execute the function being tested
|
||||||
err = StoreTemporaryVoucher(ctx, store, sessionId, voucherData)
|
err := StoreTemporaryVoucher(ctx, store, sessionId, voucherData)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify stored data
|
// Verify stored data
|
||||||
expectedEntries := map[DataTyp][]byte{
|
expectedEntries := map[DataTyp][]byte{
|
||||||
@ -136,92 +144,70 @@ func TestStoreTemporaryVoucher(t *testing.T) {
|
|||||||
|
|
||||||
for key, expectedValue := range expectedEntries {
|
for key, expectedValue := range expectedEntries {
|
||||||
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expectedValue, storedValue, "Mismatch for key %v", key)
|
require.Equal(t, expectedValue, storedValue, "Mismatch for key %v", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetTemporaryVoucherData(t *testing.T) {
|
func TestGetTemporaryVoucherData(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestDb(t)
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
||||||
|
|
||||||
// Initialize memDb
|
|
||||||
db := memdb.NewMemDb()
|
|
||||||
err := db.Connect(ctx, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Create UserDataStore with memDb
|
|
||||||
store := &UserDataStore{
|
|
||||||
Db: db,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test voucher data
|
// Test voucher data
|
||||||
tempData := &VoucherMetadata{
|
tempData := &VoucherMetadata{
|
||||||
Symbol: "SRF",
|
Symbol: "SRF",
|
||||||
Balance: "200",
|
Balance: "200",
|
||||||
Decimal: "6",
|
Decimal: "6",
|
||||||
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
||||||
}
|
}
|
||||||
|
|
||||||
// store the data
|
// Store the data
|
||||||
err = StoreTemporaryVoucher(ctx, store, sessionId, tempData)
|
err := StoreTemporaryVoucher(ctx, store, sessionId, tempData)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Execute the function being tested
|
// Execute the function being tested
|
||||||
data, err := GetTemporaryVoucherData(ctx, store, sessionId)
|
data, err := GetTemporaryVoucherData(ctx, store, sessionId)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, tempData, data)
|
require.Equal(t, tempData, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateVoucherData(t *testing.T) {
|
func TestUpdateVoucherData(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestDb(t)
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
||||||
|
|
||||||
// Initialize memDb
|
// New voucher data
|
||||||
db := memdb.NewMemDb()
|
newData := &VoucherMetadata{
|
||||||
err := db.Connect(ctx, "")
|
Symbol: "SRF",
|
||||||
require.NoError(t, err)
|
Balance: "200",
|
||||||
defer db.Close()
|
Decimal: "6",
|
||||||
|
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
||||||
store := &UserDataStore{
|
|
||||||
Db: db,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test data
|
// Old temporary data
|
||||||
data := &VoucherMetadata{
|
|
||||||
Symbol: "SRF",
|
|
||||||
Balance: "200",
|
|
||||||
Decimal: "6",
|
|
||||||
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
|
||||||
}
|
|
||||||
|
|
||||||
// First store some temporary data to verify it gets cleared
|
|
||||||
tempData := &VoucherMetadata{
|
tempData := &VoucherMetadata{
|
||||||
Symbol: "OLD",
|
Symbol: "OLD",
|
||||||
Balance: "100",
|
Balance: "100",
|
||||||
Decimal: "8",
|
Decimal: "8",
|
||||||
Address: "0xold",
|
Address: "0xold",
|
||||||
}
|
}
|
||||||
err = StoreTemporaryVoucher(ctx, store, sessionId, tempData)
|
require.NoError(t, StoreTemporaryVoucher(ctx, store, sessionId, tempData))
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Execute update
|
// Execute update
|
||||||
err = UpdateVoucherData(ctx, store, sessionId, data)
|
err := UpdateVoucherData(ctx, store, sessionId, newData)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify active data was stored correctly
|
// Verify active data was stored correctly
|
||||||
activeEntries := map[DataTyp][]byte{
|
activeEntries := map[DataTyp][]byte{
|
||||||
DATA_ACTIVE_SYM: []byte(data.Symbol),
|
DATA_ACTIVE_SYM: []byte(newData.Symbol),
|
||||||
DATA_ACTIVE_BAL: []byte(data.Balance),
|
DATA_ACTIVE_BAL: []byte(newData.Balance),
|
||||||
DATA_ACTIVE_DECIMAL: []byte(data.Decimal),
|
DATA_ACTIVE_DECIMAL: []byte(newData.Decimal),
|
||||||
DATA_ACTIVE_ADDRESS: []byte(data.Address),
|
DATA_ACTIVE_ADDRESS: []byte(newData.Address),
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, expectedValue := range activeEntries {
|
for key, expectedValue := range activeEntries {
|
||||||
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expectedValue, storedValue, "Active data mismatch for key %v", key)
|
require.Equal(t, expectedValue, storedValue, "Active data mismatch for key %v", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify temporary data was cleared
|
// Verify temporary data was cleared
|
||||||
@ -234,7 +220,7 @@ func TestUpdateVoucherData(t *testing.T) {
|
|||||||
|
|
||||||
for _, key := range tempKeys {
|
for _, key := range tempKeys {
|
||||||
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
AssertEmptyValue(t, storedValue, "Temporary data not cleared for key %v", key)
|
AssertEmptyValue(t, storedValue, "Temporary data not cleared for key %v", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user