This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathop_test.go
82 lines (70 loc) · 1.5 KB
/
op_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
package flashback
import (
"testing"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/facebookgo/ensure"
)
const (
testTime = int64(1450208315)
)
func TestOpUnmarshal(t *testing.T) {
t.Parallel()
testCmd := bson.D{{"z", 1}, {"a", 1}}
testQuery := bson.D{{"a", 1}, {"z", 1}}
testCmdDoc := bson.D{
{"ts", time.Unix(testTime, 0)},
{"ns", "foo"},
{"op", "command"},
{"command", testCmd},
}
testQueryDoc := bson.D{
{"ts", time.Unix(testTime, 0)},
{"ns", "foo"},
{"op", "query"},
{"query", testQuery},
{"ntoskip", 1},
{"ntoreturn", 2},
}
// marshal to byte form so we can unmarshal into struct
testCmdDocBytes, err := bson.Marshal(testCmdDoc)
ensure.Nil(t, err)
testQueryDocBytes, err := bson.Marshal(testQueryDoc)
ensure.Nil(t, err)
var testCmdOp, testQueryOp Op
err = bson.Unmarshal(testCmdDocBytes, &testCmdOp)
ensure.Nil(t, err)
err = bson.Unmarshal(testQueryDocBytes, &testQueryOp)
ensure.Nil(t, err)
ensure.Subset(
t,
testCmdOp,
Op{
Timestamp: time.Unix(testTime, 0),
Ns: "foo",
Type: Command,
CommandDoc: testCmd,
},
)
ensure.Subset(
t,
testQueryOp,
Op{
Timestamp: time.Unix(testTime, 0),
Ns: "foo",
Type: Query,
QueryDoc: testQuery,
NToSkip: 1,
NToReturn: 2,
},
)
}
func TestGetElem(t *testing.T) {
doc := bson.D{{"a", 1}}
value, exists := GetElem(doc, "a")
ensure.True(t, exists)
ensure.DeepEqual(t, value.(int), 1)
value, exists = GetElem(doc, "b")
ensure.False(t, exists)
ensure.Nil(t, value)
}