-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.store.sqlite_test.go
229 lines (203 loc) · 5.3 KB
/
command.store.sqlite_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package store_test
import (
"context"
"testing"
store "github.com/gradientzero/comby-store-sqlite"
"github.com/gradientzero/comby/v2"
)
func TestCommandStore1(t *testing.T) {
var err error
ctx := context.Background()
// setup and init store
commandStore := store.NewCommandStoreSQLite("./commandStore.db")
if err = commandStore.Init(ctx,
comby.CommandStoreOptionWithAttribute("key1", "value"),
); err != nil {
t.Fatal(err)
}
// check if the attribute is set
if v, ok := commandStore.Options().Attributes.Get("key1"); ok {
if v != "value" {
t.Fatalf("wrong value: %q", v)
}
} else {
t.Fatalf("missing key")
}
// check totals
if commandStore.Total(ctx) != 0 {
t.Fatalf("wrong total %d", commandStore.Total(ctx))
}
// Create values
cmd1 := &comby.BaseCommand{
CommandUuid: comby.NewUuid(),
TenantUuid: "TenantUuid_1",
Domain: "Domain_1",
CreatedAt: 1000,
}
if err := commandStore.Create(ctx,
comby.CommandStoreCreateOptionWithCommand(cmd1),
); err != nil {
t.Fatal(err)
}
cmd2 := &comby.BaseCommand{
CommandUuid: comby.NewUuid(),
TenantUuid: "TenantUuid_2",
Domain: "Domain_2",
CreatedAt: 1000,
}
if err := commandStore.Create(ctx,
comby.CommandStoreCreateOptionWithCommand(cmd2),
comby.CommandStoreCreateOptionWithAttribute("anyKey1", "anyValue1"),
); err != nil {
t.Fatal(err)
}
// check totals
if commandStore.Total(ctx) != 2 {
t.Fatalf("wrong total %d", commandStore.Total(ctx))
}
// Get a value
if _cmd1, err := commandStore.Get(ctx,
comby.CommandStoreGetOptionWithCommandUuid(cmd1.CommandUuid),
); err != nil {
t.Fatal(err)
} else {
if _cmd1.GetTenantUuid() != "TenantUuid_1" {
t.Fatalf("wrong value: %q", _cmd1)
}
}
// List all commands
if evts, total, err := commandStore.List(ctx); err == nil {
if len(evts) != 2 {
t.Fatalf("wrong number of commands: %d", len(evts))
}
if int64(len(evts)) != total {
t.Fatalf("wrong number of totals: %d", total)
}
}
// Delete an event
if err := commandStore.Delete(ctx,
comby.CommandStoreDeleteOptionWithCommandUuid(cmd1.CommandUuid),
); err != nil {
t.Fatal(err)
}
// check totals
if commandStore.Total(ctx) != 1 {
t.Fatalf("wrong total %d", commandStore.Total(ctx))
}
// reset database
if err := commandStore.Reset(ctx); err != nil {
t.Fatal(err)
}
// close connection
if err := commandStore.Close(ctx); err != nil {
t.Fatalf("failed to close connection: %v", err)
}
}
func TestCommandStoreEncrypted(t *testing.T) {
var err error
ctx := context.Background()
// create crypto service
key := []byte("12345678901234567890123456789012")
cryptoService, _ := comby.NewCryptoService(key)
// setup and init store
commandStore := store.NewCommandStoreSQLite("./commandStore-encrypted.db")
if err = commandStore.Init(ctx,
comby.CommandStoreOptionWithCryptoService(cryptoService),
); err != nil {
t.Fatal(err)
}
// create domain data to encrypt/decrypt
type MyDomainCommand struct {
String string
Int int64
Boolean bool
}
domainData := &MyDomainCommand{
String: "string",
Int: 123,
Boolean: true,
}
// Create values
cmd1 := &comby.BaseCommand{
CommandUuid: comby.NewUuid(),
TenantUuid: "TenantUuid_1",
Domain: "Domain_1",
CreatedAt: 1000,
DomainCmd: domainData,
}
if err := commandStore.Create(ctx,
comby.CommandStoreCreateOptionWithCommand(cmd1),
); err != nil {
t.Fatal(err)
}
// Get a value
if _cmd1, err := commandStore.Get(ctx,
comby.CommandStoreGetOptionWithCommandUuid(cmd1.CommandUuid),
); err != nil {
t.Fatal(err)
} else {
_domainData := &MyDomainCommand{}
_domainData, _ = comby.Deserialize(_cmd1.GetDomainCmdBytes(), _domainData)
if _domainData.String != "string" {
t.Fatalf("wrong value: %q", _domainData.String)
}
if _domainData.Int != 123 {
t.Fatalf("wrong value: %q", _domainData.Int)
}
if _domainData.Boolean != true {
t.Fatalf("wrong value")
}
}
// List all commands
if cmds, _, err := commandStore.List(ctx); err == nil {
_cmd1 := cmds[0]
_domainData := &MyDomainCommand{}
_domainData, _ = comby.Deserialize(_cmd1.GetDomainCmdBytes(), _domainData)
if _domainData.String != "string" {
t.Fatalf("wrong value: %q", _domainData.String)
}
if _domainData.Int != 123 {
t.Fatalf("wrong value: %q", _domainData.Int)
}
if _domainData.Boolean != true {
t.Fatalf("wrong value")
}
}
// Update event
domainData.String = "string2"
domainData.Int = 456
domainData.Boolean = false
cmd1.DomainCmd = domainData
// Delete an event
if err := commandStore.Update(ctx,
comby.CommandStoreUpdateOptionWithCommand(cmd1),
); err != nil {
t.Fatal(err)
}
// Get a value
if _cmd1, err := commandStore.Get(ctx,
comby.CommandStoreGetOptionWithCommandUuid(cmd1.CommandUuid),
); err != nil {
t.Fatal(err)
} else {
_domainData := &MyDomainCommand{}
_domainData, _ = comby.Deserialize(_cmd1.GetDomainCmdBytes(), _domainData)
if _domainData.String != "string2" {
t.Fatalf("wrong value: %q", _domainData.String)
}
if _domainData.Int != 456 {
t.Fatalf("wrong value: %q", _domainData.Int)
}
if _domainData.Boolean != false {
t.Fatalf("wrong value")
}
}
// reset database
if err := commandStore.Reset(ctx); err != nil {
t.Fatal(err)
}
// close connection
if err := commandStore.Close(ctx); err != nil {
t.Fatalf("failed to close connection: %v", err)
}
}