forked from go-bond/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_unsafe_test.go
89 lines (71 loc) · 2.24 KB
/
table_unsafe_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package bond
import (
"context"
"testing"
"github.com/cockroachdb/pebble"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBondTable_UnsafeUpdate(t *testing.T) {
db := setupDatabase()
defer tearDownDatabase(db)
const (
TokenBalanceTableID = TableID(1)
)
tokenBalanceTable := NewTable[*TokenBalance](TableOptions[*TokenBalance]{
DB: db,
TableID: TokenBalanceTableID,
TableName: "token_balance",
TablePrimaryKeyFunc: func(builder KeyBuilder, tb *TokenBalance) []byte {
return builder.AddUint64Field(tb.ID).Bytes()
},
})
tokenBalanceAccount := &TokenBalance{
ID: 1,
AccountID: 1,
ContractAddress: "0xtestContract",
AccountAddress: "0xtestAccount",
Balance: 5,
}
tokenBalanceAccountUpdated := &TokenBalance{
ID: 1,
AccountID: 1,
ContractAddress: "0xtestContract",
AccountAddress: "0xtestAccount",
Balance: 7,
}
err := tokenBalanceTable.Insert(context.Background(), []*TokenBalance{tokenBalanceAccount})
require.NoError(t, err)
it := db.Backend().NewIter(&pebble.IterOptions{
LowerBound: []byte{byte(TokenBalanceTableID)},
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
for it.First(); it.Valid(); it.Next() {
rawData := it.Value()
var tokenBalanceAccount1FromDB TokenBalance
err = db.Serializer().Deserialize(rawData, &tokenBalanceAccount1FromDB)
require.NoError(t, err)
assert.Equal(t, tokenBalanceAccount, &tokenBalanceAccount1FromDB)
}
_ = it.Close()
tableUnsafeUpdater, ok := tokenBalanceTable.(TableUnsafeUpdater[*TokenBalance])
require.True(t, ok)
err = tableUnsafeUpdater.UnsafeUpdate(
context.Background(),
[]*TokenBalance{tokenBalanceAccountUpdated},
[]*TokenBalance{tokenBalanceAccount},
)
require.NoError(t, err)
it = db.Backend().NewIter(&pebble.IterOptions{
LowerBound: []byte{byte(TokenBalanceTableID)},
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
for it.First(); it.Valid(); it.Next() {
rawData := it.Value()
var tokenBalanceAccount1FromDB TokenBalance
err = db.Serializer().Deserialize(rawData, &tokenBalanceAccount1FromDB)
require.NoError(t, err)
assert.Equal(t, tokenBalanceAccountUpdated, &tokenBalanceAccount1FromDB)
}
_ = it.Close()
}