-
Notifications
You must be signed in to change notification settings - Fork 2
/
record_test.go
91 lines (86 loc) · 2.12 KB
/
record_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
package turbine_test
import (
"github.com/meroxa/turbine-go"
"github.com/tidwall/gjson"
"log"
"reflect"
"testing"
)
func TestPayload_Set(t *testing.T) {
type args struct {
path string
value interface{}
}
tests := []struct {
name string
p turbine.Payload
args args
wantErr bool
schemaFieldsNum int
}{
{"add new", recWithSchema(), args{"email", "[email protected]"}, false, 3},
{"update", recWithSchema(), args{"id", 16}, false, 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.p.Set(tt.args.path, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
}
schemaFields := gjson.Get(string(tt.p), "schema.fields")
if l := len(schemaFields.Array()); l != tt.schemaFieldsNum {
log.Printf("p: %+v", string(tt.p))
t.Errorf("Set() fields len = %v, want %v", l, tt.schemaFieldsNum)
}
})
}
}
func recWithSchema() turbine.Payload {
return []byte(`
{
"schema": {
"type": "struct",
"fields": [{
"type": "int32",
"optional": false,
"field": "id"
}, {
"type": "string",
"optional": false,
"field": "username"
}],
"optional": false,
"name": "users"
},
"payload": {
"id": 15,
"username": "test"
}
}
`)
}
func TestPayload_Delete(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
p turbine.Payload
args args
want turbine.Payload
wantErr bool
}{
{"delete existing", []byte(`{"user":{"id":16,"name": "alice"}}`), args{"user.name"}, []byte(`{"user":{"id":16}}`), false},
{"delete non-existent", []byte(`{"user":{"id":16,"name": "alice"}}`), args{"user.email"}, []byte(`{"user":{"id":16,"name": "alice"}}`), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.p.Delete(tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.p, tt.want) {
//log.Printf("p: %+v", string(tt.p))
t.Errorf("Delete() got = %v, want %v", string(tt.p), string(tt.want))
}
})
}
}