-
Notifications
You must be signed in to change notification settings - Fork 0
/
goeql_test.go
524 lines (444 loc) · 14.1 KB
/
goeql_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package goeql
import (
"encoding/json"
"reflect"
"testing"
)
// Test EncryptedText Serialization
func TestEncryptedText_Serialize(t *testing.T) {
et := EncryptedText("Hello, World!")
table := "test_table"
column := "test_column"
serializedData, err := et.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
deserializedData, err := et.Deserialize(serializedData)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if !reflect.DeepEqual(deserializedData, et) {
t.Errorf("Expected deserialized value to be '%s', got '%s'", et, deserializedData)
}
}
// Test EncryptedText Deserialization
func TestEncryptedText_Deserialize(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "Hello, World!",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var et EncryptedText
deserialized, err := et.Deserialize(data)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if deserialized != EncryptedText("Hello, World!") {
t.Errorf("Expected deserialized value to be 'Hello, World!', got '%s'", deserialized)
}
}
// Test EncryptedJsonb Serialization
func TestEncryptedJsonb_Serialize(t *testing.T) {
// You must cast any int to float64 to get the correct JSON output
// Deserialization will always return a float64 for ints as json.Unmarshal will
// convert them to float64 by default
ej := EncryptedJsonb{
"name": "Alice",
"age": float64(30),
"is_member": true,
}
table := "test_table"
column := "test_column"
serializedData, err := ej.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
deserializedData, err := ej.Deserialize(serializedData)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if !reflect.DeepEqual(deserializedData, ej) {
t.Errorf("Expected deserialized value to be '%s', got '%s'", ej, deserializedData)
}
}
func TestNullEncryptedJsonb_Serialize(t *testing.T) {
ej := EncryptedJsonb(nil)
table := "test_table"
column := "test_column"
serializedData, err := ej.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
deserializedData, err := ej.Deserialize(serializedData)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if !reflect.DeepEqual(deserializedData, ej) {
t.Errorf("Expected deserialized value to be '%s', got '%s'", ej, deserializedData)
}
}
func TestNullEncryptedText_Serialize(t *testing.T) {
ej := EncryptedText("")
table := "test_table"
column := "test_column"
serializedData, err := ej.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
deserializedData, err := ej.Deserialize(serializedData)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if !reflect.DeepEqual(deserializedData, ej) {
t.Errorf("Expected deserialized value to be '%s', got '%s'", ej, deserializedData)
}
}
// Test EncryptedJsonb Deserialization
func TestEncryptedJsonb_Deserialize(t *testing.T) {
originalData := map[string]interface{}{
"name": "Alice",
"age": float64(30),
"is_member": true,
}
jsonString, err := json.Marshal(originalData)
if err != nil {
t.Fatalf("Error marshaling original data: %v", err)
}
ec := EncryptedColumn{
K: "pt",
P: string(jsonString),
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var ej EncryptedJsonb
deserialized, err := ej.Deserialize(data)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if !reflect.DeepEqual(deserialized, EncryptedJsonb(originalData)) {
t.Errorf("Deserialized data does not match original data")
}
}
// Test EncryptedInt Serialization
func TestEncryptedInt_Serialize(t *testing.T) {
ei := EncryptedInt(42)
table := "test_table"
column := "test_column"
serializedData, err := ei.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
expectedP := "42"
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
}
// Test EncryptedInt Deserialization
func TestEncryptedInt_Deserialize(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "42",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var ei EncryptedInt
deserialized, err := ei.Deserialize(data)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if deserialized != EncryptedInt(42) {
t.Errorf("Expected deserialized value to be 42, got %d", deserialized)
}
}
// Test EncryptedBool Serialization
func TestEncryptedBool_Serialize(t *testing.T) {
eb := EncryptedBool(true)
table := "test_table"
column := "test_column"
serializedData, err := eb.Serialize(table, column)
if err != nil {
t.Fatalf("Serialize returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != "true" {
t.Errorf("Expected P to be 'true', got '%s'", ec.P)
}
}
// Test EncryptedBool Deserialization
func TestEncryptedBool_Deserialize(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "true",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var eb EncryptedBool
deserialized, err := eb.Deserialize(data)
if err != nil {
t.Fatalf("Deserialize returned error: %v", err)
}
if deserialized != EncryptedBool(true) {
t.Errorf("Expected deserialized value to be true, got %v", deserialized)
}
}
func TestMatchQuerySerialization(t *testing.T) {
value := "test_string"
table := "table1"
column := "column1"
expectedP := "test_string"
expectedQ := "match"
serializedData, err := MatchQuery(value, table, column)
if err != nil {
t.Fatalf("SerializeQuery returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
if ec.Q != expectedQ {
t.Errorf("Expected Q to be '%s', got '%s'", expectedQ, ec.Q)
}
}
func TestOreQuerySerialization(t *testing.T) {
value := 123
table := "table1"
column := "column1"
expectedP := "123"
expectedQ := "ore"
serializedData, err := OreQuery(value, table, column)
if err != nil {
t.Fatalf("SerializeQuery returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
if ec.Q != expectedQ {
t.Errorf("Expected Q to be '%s', got '%s'", expectedQ, ec.Q)
}
}
func TestUniqueQuerySerialization(t *testing.T) {
value := true
table := "table1"
column := "column1"
expectedP := "true"
expectedQ := "unique"
serializedData, err := UniqueQuery(value, table, column)
if err != nil {
t.Fatalf("SerializeQuery returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
if ec.Q != expectedQ {
t.Errorf("Expected Q to be '%s', got '%s'", expectedQ, ec.Q)
}
}
func TestJsonbQuerySerialization(t *testing.T) {
value := map[string]interface{}{"key": "value"}
table := "table1"
column := "column1"
expectedP := `{"key":"value"}`
expectedQ := "ste_vec"
serializedData, err := JsonbQuery(value, table, column)
if err != nil {
t.Fatalf("SerializeQuery returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
if ec.Q != expectedQ {
t.Errorf("Expected Q to be '%s', got '%s'", expectedQ, ec.Q)
}
}
func TestEJsonPathQueryQuerySerialization(t *testing.T) {
value := "$.top"
table := "table1"
column := "column1"
expectedP := "$.top"
expectedQ := "ejson_path"
serializedData, err := EJsonPathQuery(value, table, column)
if err != nil {
t.Fatalf("SerializeQuery returned error: %v", err)
}
var ec EncryptedColumn
if err := json.Unmarshal(serializedData, &ec); err != nil {
t.Fatalf("Error unmarshaling serialized data: %v", err)
}
if ec.P != expectedP {
t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P)
}
if ec.Q != expectedQ {
t.Errorf("Expected Q to be '%s', got '%s'", expectedQ, ec.Q)
}
}
// Test ToEncryptedColumn Function
func TestToEncryptedColumn(t *testing.T) {
tests := []struct {
value interface{}
table string
column string
expectedP string
}{
{value: "test_string", table: "table1", column: "column1", expectedP: "test_string"},
{value: 123, table: "table2", column: "column2", expectedP: "123"},
{value: 123.456, table: "table3", column: "column3", expectedP: "123.456000"},
{value: true, table: "table4", column: "column4", expectedP: "true"},
{value: map[string]interface{}{"key": "value"}, table: "table5", column: "column5", expectedP: `{"key":"value"}`},
}
for _, tt := range tests {
ec, err := ToEncryptedColumn(tt.value, tt.table, tt.column, nil)
if err != nil {
t.Fatalf("ToEncryptedColumn returned error: %v", err)
}
if ec.P != tt.expectedP {
t.Errorf("Expected P to be '%s', got '%s'", tt.expectedP, ec.P)
}
}
}
// Test convertToString Function
func TestConvertToString(t *testing.T) {
// the ptr value for unitptr will change between tests
// set a mock value to use to confirm the convertToString function works on this uintptr.
var mockPtr uintptr = 1374390189136
tests := []struct {
value interface{}
expectedStr string
expectError bool
}{
{value: "test_string", expectedStr: "test_string", expectError: false},
{value: 123, expectedStr: "123", expectError: false},
{value: 123.456, expectedStr: "123.456000", expectError: false},
{value: int(1), expectedStr: "1", expectError: false},
{value: int8(-128), expectedStr: "-128", expectError: false},
{value: int8(127), expectedStr: "127", expectError: false},
{value: int16(-32768), expectedStr: "-32768", expectError: false},
{value: int16(32767), expectedStr: "32767", expectError: false},
{value: int32(-2147483648), expectedStr: "-2147483648", expectError: false},
{value: int32(2147483647), expectedStr: "2147483647", expectError: false},
{value: int64(-9223372036854775808), expectedStr: "-9223372036854775808", expectError: false},
{value: int64(9223372036854775807), expectedStr: "9223372036854775807", expectError: false},
{value: uint(1), expectedStr: "1", expectError: false},
{value: uint8(255), expectedStr: "255", expectError: false},
{value: uint16(65535), expectedStr: "65535", expectError: false},
{value: uint32(4294967295), expectedStr: "4294967295", expectError: false},
{value: uint64(18446744073709551615), expectedStr: "18446744073709551615", expectError: false},
{value: float32(123.456), expectedStr: "123.456001", expectError: false},
{value: float32(-10.543), expectedStr: "-10.543000", expectError: false},
{value: float64(3.1425), expectedStr: "3.142500", expectError: false},
{value: float64(-2.7182), expectedStr: "-2.718200", expectError: false},
{value: mockPtr, expectedStr: "1374390189136", expectError: false}, //uinttpr type
{value: true, expectedStr: "true", expectError: false},
{value: map[string]interface{}{"key": "value"}, expectedStr: `{"key":"value"}`, expectError: false},
{value: []int{1, 2, 3}, expectedStr: "[1, 2, 3]", expectError: false},
{value: []float64{1.1, 2.2, 3.3}, expectedStr: "[1.100000, 2.200000, 3.300000]", expectError: false},
{value: []string{"hello", "world"}, expectedStr: "[hello, world]", expectError: false},
{value: []bool{true, false, true}, expectedStr: "[true, false, true]", expectError: false},
}
for _, tt := range tests {
str, err := convertToString(tt.value)
if tt.expectError {
if err == nil {
t.Errorf("Expected error for value: %v, but got none", tt.value)
}
} else {
if err != nil {
t.Errorf("Unexpected error for value: %v, error: %v", tt.value, err)
} else if str != tt.expectedStr {
t.Errorf("Expected '%s', got '%s' for value: %v for type: '%s'", tt.expectedStr, str, tt.value, reflect.TypeOf(tt.value))
}
}
}
}
// Test EncryptedInt Deserialization Error
func TestEncryptedInt_Deserialize_Error(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "not_an_integer",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var ei EncryptedInt
_, err = ei.Deserialize(data)
if err == nil {
t.Errorf("Expected error during Deserialize, but got none")
}
}
// Test EncryptedBool Deserialization Error
func TestEncryptedBool_Deserialize_Error(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "not_a_boolean",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var eb EncryptedBool
_, err = eb.Deserialize(data)
if err == nil {
t.Errorf("Expected error during Deserialize, but got none")
}
}
// Test EncryptedJsonb Deserialization Error
func TestEncryptedJsonb_Deserialize_Error(t *testing.T) {
ec := EncryptedColumn{
K: "pt",
P: "invalid_json",
I: TableColumn{T: "test_table", C: "test_column"},
V: 1,
}
data, err := json.Marshal(ec)
if err != nil {
t.Fatalf("Error marshaling EncryptedColumn: %v", err)
}
var ej EncryptedJsonb
_, err = ej.Deserialize(data)
if err == nil {
t.Errorf("Expected error during Deserialize, but got none")
}
}