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
/
Copy pathremap_test.go
80 lines (74 loc) · 1.75 KB
/
remap_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
package remap
import (
"reflect"
"testing"
"github.com/compose/transporter/function"
_ "github.com/compose/transporter/log"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/ops"
)
var initTests = []struct {
in map[string]interface{}
expect *remap
}{
{
map[string]interface{}{"ns_map": map[string]string{"test": "newtest"}},
&remap{SwapMap: map[string]string{"test": "newtest"}},
},
}
func TestInit(t *testing.T) {
for _, it := range initTests {
a, err := function.GetFunction("remap", 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 remapTests = []struct {
name string
nsMap map[string]string
in map[string]interface{}
inNs string
outNs string
err error
}{
{
"single field",
map[string]string{"foo": "bar"},
map[string]interface{}{"_id": "blah", "type": "good"},
"foo",
"bar",
nil,
},
{
"multiple fields",
map[string]string{"foo": "bar", "baz": "boo"},
map[string]interface{}{"_id": "blah", "type": "good", "name": "hello"},
"baz",
"boo",
nil,
},
{
"no matched fields",
map[string]string{"blah": "hey"},
map[string]interface{}{"_id": "blah", "type": "good"},
"foo",
"foo",
nil,
},
}
func TestApply(t *testing.T) {
for _, rt := range remapTests {
remap := &remap{rt.nsMap}
msg, err := remap.Apply(message.From(ops.Insert, rt.inNs, rt.in))
if !reflect.DeepEqual(err, rt.err) {
t.Errorf("[%s] error mismatch, expected %s, got %s", rt.name, rt.err, err)
}
if !reflect.DeepEqual(msg.Namespace(), rt.outNs) {
t.Errorf("[%s] wrong message, expected %+v, got %+v", rt.name, rt.outNs, msg.Namespace())
}
}
}