forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_executor_test.go
112 lines (93 loc) · 3.08 KB
/
ops_executor_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
package flashback
import (
"fmt"
"testing"
. "gopkg.in/check.v1"
"gopkg.in/mgo.v2"
)
// Hook up gocheck into the "go test" runner.
func TestOps(t *testing.T) {
TestingT(t)
}
type TestExecutorSuite struct{}
var _ = Suite(&TestExecutorSuite{})
func (s *TestExecutorSuite) TestExecution(c *C) {
test_db := "test_db_for_executor"
test_collection := "c1"
session, err := mgo.Dial("localhost")
c.Assert(err, IsNil)
c.Assert(session, NotNil)
defer session.Close()
err = session.DB(test_db).DropDatabase()
c.Assert(err, IsNil)
// insertion
insertCmd := fmt.Sprintf(`{"ns": "%s.%s", "ts": {"$date": 1396456709427}, `+
`"o": {"logType": "console", "message": "start", "_id": `+
`{"$oid": "533c3d03c23fffd217678ee8"}, "timestamp": `+
`{"$date": 1396456707977}}, "op": "insert"}`,
test_db, test_collection)
cmd, err := parseJson(insertCmd)
c.Assert(err, IsNil)
op := makeOp(cmd)
exec := NewOpsExecutor(session)
err = exec.Execute(op)
c.Assert(err, IsNil)
coll := session.DB(test_db).C(test_collection)
count, err := coll.Count()
c.Assert(count, Equals, 1)
// Find
findCmd := fmt.Sprintf(`{"ntoskip": 0, "ts": {"$date": 1396456709472}, `+
`"ntoreturn": 1, "query": {"$maxScan": 9000000, "$query": {"$or": `+
`[{"_id": {"$oid": "533c3d03c23fffd217678ee8"}}]}}, `+
`"ns": "%s.%s", "op": "query"}`, test_db, test_collection)
cmd, err = parseJson(findCmd)
c.Assert(err, IsNil)
findOp := makeOp(cmd)
err = exec.Execute(findOp)
c.Assert(err, IsNil)
findResult := exec.lastResult.(*[]Document)
c.Assert(len(*findResult), Equals, 1)
exec.lastResult = nil
// Update
updateCmd := fmt.Sprintf(`{"ts": {"$date": 1396456709472}, `+
`"query": {"_id": {"$oid": "533c3d03c23fffd217678ee8"}}, `+
`"updateobj": {"$set":{"logType": "hooo"}}, `+
`"ns": "%s.%s", "op": "update"}`, test_db, test_collection)
cmd, err = parseJson(updateCmd)
c.Assert(err, IsNil)
err = exec.Execute(makeOp(cmd))
c.Assert(err, IsNil)
err = exec.Execute(findOp)
c.Assert(err, IsNil)
findResult = exec.lastResult.(*[]Document)
c.Assert((*findResult)[0]["logType"].(string), Equals, "hooo")
findResult = nil
// findAndModify
famCmd := fmt.Sprintf(
`{"ts": {"$date": 1396456709472}, `+
`"ns": "%s.$cmd", "command": {"query": {"_id": {"$oid": "533c3d03c23fffd217678ee8"}}, `+
`"findandmodify": "%s", `+
`"update": {"$set": {"logType": "foobar"}}}, "op": "command"}`, test_db, test_collection)
cmd, err = parseJson(famCmd)
c.Assert(err, IsNil)
err = exec.Execute(makeOp(cmd))
c.Assert(err, IsNil)
err = exec.Execute(findOp)
c.Assert(err, IsNil)
findResult = exec.lastResult.(*[]Document)
c.Assert((*findResult)[0]["logType"].(string), Equals, "foobar")
findResult = nil
// Remove
removeCmd := fmt.Sprintf(
`{"query": {"_id": {"$oid": "533c3d03c23fffd217678ee8"}}, `+
`"ns": "%s.%s", "ts": {"$date": 1396456709432}, "op": "remove"}`,
test_db, test_collection)
cmd, err = parseJson(removeCmd)
c.Assert(err, IsNil)
err = exec.Execute(makeOp(cmd))
c.Assert(err, IsNil)
err = exec.Execute(findOp)
c.Assert(err, IsNil)
findResult = exec.lastResult.(*[]Document)
c.Assert(len(*findResult), Equals, 0)
}