-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinsert_public_key_accounts_test.go
113 lines (100 loc) · 2.79 KB
/
insert_public_key_accounts_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"example/flow-key-indexer/pkg/pg"
logger "log"
"testing"
"example/flow-key-indexer/model"
"github.com/axiomzen/envconfig"
"github.com/rs/zerolog/log"
)
func TestDuplicatedKeysInBatches(t *testing.T) {
var p Params
err := envconfig.Process("KEYIDX", &p)
if err != nil {
logger.Fatal(err.Error())
}
dbConfig := pg.DatabaseConfig{
Host: p.PostgreSQLHost,
Password: p.PostgreSQLPassword,
Name: p.PostgreSQLDatabase,
User: p.PostgreSQLUsername,
Port: int(p.PostgreSQLPort),
}
db := pg.NewStore(dbConfig, log.Logger)
_ = db.Start(true)
// Batch 1: Insert unique keys
batch1 := []model.PublicKeyAccountIndexer{
{
Account: "Account1",
KeyId: 0,
PublicKey: "publicKey1",
Weight: 1000,
},
{
Account: "Account2",
KeyId: 0,
PublicKey: "publicKey2",
Weight: 1000,
},
{
Account: "Account3",
KeyId: 0,
PublicKey: "publicKey3",
Weight: 1000,
},
}
// Batch 2: Insert duplicates with different SigAlgo and HashAlgo to trigger ON CONFLICT
batch2 := []model.PublicKeyAccountIndexer{
{
Account: "Account1",
KeyId: 0,
PublicKey: "publicKey1",
SigAlgo: 1,
HashAlgo: 1,
},
{
Account: "Account2",
KeyId: 0,
PublicKey: "publicKey2",
SigAlgo: 1,
HashAlgo: 1,
},
}
ctx := context.Background()
// Insert Batch 1
err = db.InsertPublicKeyAccounts(ctx, batch1)
if err != nil {
t.Fatalf("Failed to insert batch 1 of public key accounts: %v", err)
}
// Insert Batch 2 (with duplicates)
err = db.InsertPublicKeyAccounts(ctx, batch2)
if err != nil {
t.Fatalf("Failed to insert batch 2 of public key accounts: %v", err)
}
// Verify inserted data
checkKey1, err := db.GetAccountsByPublicKey("publicKey1")
if err != nil {
t.Fatalf("Failed to get accounts for publicKey1: %v", err)
}
checkKey2, err := db.GetAccountsByPublicKey("publicKey2")
if err != nil {
t.Fatalf("Failed to get accounts for publicKey2: %v", err)
}
checkKey3, err := db.GetAccountsByPublicKey("publicKey3")
if err != nil {
t.Fatalf("Failed to get accounts for publicKey3: %v", err)
}
// Expected behavior:
// - publicKey1 should have 1 account after conflict resolution
// - publicKey2 should have 1 account after conflict resolution
// - publicKey3 should have 1 account (no conflict)
if len(checkKey1.Accounts) != 1 || len(checkKey2.Accounts) != 1 || len(checkKey3.Accounts) != 1 {
t.Errorf("Unexpected number of accounts: publicKey1=%d, publicKey2=%d, publicKey3=%d",
len(checkKey1.Accounts), len(checkKey2.Accounts), len(checkKey3.Accounts))
}
// Verify the conflict resolution updated sigalgo and hashalgo
if checkKey1.Accounts[0].SigAlgo != 1 || checkKey2.Accounts[0].SigAlgo != 1 {
t.Errorf("Expected sigalgo to be 1 after conflict resolution")
}
}