This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
goja_test.go
196 lines (186 loc) · 4.9 KB
/
goja_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
package gojajs
import (
"reflect"
"testing"
"github.com/compose/transporter/function"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/data"
"github.com/compose/transporter/message/ops"
"gopkg.in/mgo.v2/bson"
)
var initTests = []struct {
fnName string
in map[string]interface{}
expect *goja
}{
{
"goja",
map[string]interface{}{"filename": "testdata/transformer.js"},
&goja{Filename: "testdata/transformer.js"},
},
{
"js",
map[string]interface{}{"filename": "testdata/transformer.js"},
&goja{Filename: "testdata/transformer.js"},
},
}
func TestInit(t *testing.T) {
for _, it := range initTests {
a, err := function.GetFunction(it.fnName, it.in)
if err != nil {
t.Fatalf("unexpected GetFunction() error, %s", err)
}
if !reflect.DeepEqual(a, it.expect) {
t.Errorf("misconfigured Function, expected %+v, got %+v", it.expect, a)
}
}
}
var (
bsonID1 = bson.NewObjectId()
bsonID2 = bson.ObjectIdHex("54a4420502a14b9641000001")
writeTests = []struct {
name string
fn string
in message.Msg
out message.Msg
err error
}{
{
"just pass through",
"testdata/transformer.js",
message.From(ops.Insert, "collection", data.Data{"id": "id1", "name": "nick"}),
message.From(ops.Insert, "collection", data.Data{"id": "id1", "name": "nick"}),
nil,
},
{
"delete the 'name' property",
"testdata/delete_name.js",
message.From(ops.Insert, "collection", data.Data{"id": "id2", "name": "nick"}),
message.From(ops.Insert, "collection", data.Data{"id": "id2"}),
nil,
},
{
"delete's should be processed the same",
"testdata/delete_name.js",
message.From(ops.Delete, "collection", data.Data{"id": "id2", "name": "nick"}),
message.From(ops.Delete, "collection", data.Data{"id": "id2"}),
nil,
},
{
"bson should marshal and unmarshal properly",
"testdata/transformer.js",
message.From(ops.Insert, "collection", data.Data{"id": bsonID1, "name": "nick"}),
message.From(ops.Insert, "collection", data.Data{"id": bsonID1, "name": "nick"}),
nil,
},
{
"we should be able to change the bson",
"testdata/change_bson.js",
message.From(ops.Insert, "collection", data.Data{"id": bsonID1, "name": "nick"}),
message.From(ops.Insert, "collection", data.Data{"id": bsonID2, "name": "nick"}),
nil,
},
{
"we should be able to skip a message",
"testdata/skip.js",
message.From(ops.Insert, "collection", data.Data{"id": bsonID1, "name": "nick"}),
nil,
nil,
},
{
"we should be able to change the namespace",
"testdata/change_ns.js",
message.From(ops.Insert, "collection", data.Data{"id": bsonID1, "name": "nick"}),
message.From(ops.Insert, "table", data.Data{"id": bsonID1, "name": "nick"}),
nil,
}, {
"we should be able to add an object to the bson",
"testdata/add_data.js",
message.From(ops.Insert, "collection", data.Data{"name": "nick"}),
message.From(ops.Insert, "collection", data.Data{"name": "nick", "added": bson.M{"name": "batman", "villain": "joker"}}),
nil,
},
{
"Invalid data returned",
"testdata/invalid_data.js",
message.From(ops.Insert, "collection", data.Data{"id": "id1", "name": "nick"}),
nil,
ErrInvalidMessageType,
},
{
"empty filename",
"",
message.From(ops.Insert, "collection", data.Data{"id": "id1", "name": "nick"}),
nil,
ErrEmptyFilename,
},
}
)
func TestApply(t *testing.T) {
for _, v := range writeTests {
g := goja{Filename: v.fn}
msg, err := g.Apply(v.in)
if err != v.err {
t.Errorf("[%s] wrong error, expected: %+v, got; %v", v.name, v.err, err)
}
if !isEqual(msg, v.out) {
t.Errorf("[%s] expected:\n(%T) %+v\ngot:\n(%T) %+v with error (%v)\n", v.name, v.out, v.out, msg, msg, err)
}
}
}
func isEqual(m1 message.Msg, m2 message.Msg) bool {
if m1 == nil && m2 != nil {
return false
}
if m1 != nil && m2 == nil {
return false
}
if m1 == nil && m2 == nil {
return true
}
if m1.ID() != m2.ID() {
return false
}
if m1.Namespace() != m2.Namespace() {
return false
}
if m1.OP() != m2.OP() {
return false
}
if m1.Timestamp() != m2.Timestamp() {
return false
}
if reflect.TypeOf(m1.Data()) != reflect.TypeOf(m2.Data()) {
return false
}
return isEqualBSON(m1.Data(), m2.Data())
}
func isEqualBSON(m1 map[string]interface{}, m2 map[string]interface{}) bool {
for k, v := range m1 {
m2Val := m2[k]
if reflect.TypeOf(v) != reflect.TypeOf(m2Val) {
return false
}
switch v.(type) {
case map[string]interface{}, bson.M, data.Data:
eq := isEqualBSON(v.(bson.M), m2Val.(bson.M))
if !eq {
return false
}
default:
eq := reflect.DeepEqual(v, m2Val)
if !eq {
return false
}
}
}
return true
}
func BenchmarkTransformOne(b *testing.B) {
g := &goja{Filename: "testdata/transformer.js"}
msg := message.From(ops.Insert, "collection", map[string]interface{}{"id": bson.NewObjectId(), "name": "nick"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
g.Apply(msg)
}
}