forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_join_test.go
177 lines (148 loc) · 5.33 KB
/
query_join_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
package gorethink
import (
test "gopkg.in/check.v1"
)
func (s *RethinkSuite) TestJoinInnerJoin(c *test.C) {
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("Join1").Exec(sess)
Db("test").TableCreate("Join2").Exec(sess)
// Insert rows
Db("test").Table("Join1").Insert(joinTable1).Exec(sess)
Db("test").Table("Join2").Insert(joinTable2).Exec(sess)
// Test query
var response []interface{}
query := Db("test").Table("Join1").InnerJoin(Db("test").Table("Join2"), func(a, b Term) Term {
return a.Field("id").Eq(b.Field("id"))
})
res, err := query.Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(response, jsonEquals, []interface{}{
map[string]interface{}{
"right": map[string]interface{}{"title": "goof", "id": 0},
"left": map[string]interface{}{"name": "bob", "id": 0},
},
map[string]interface{}{
"right": map[string]interface{}{"title": "lmoe", "id": 2},
"left": map[string]interface{}{"name": "joe", "id": 2},
},
})
}
func (s *RethinkSuite) TestJoinInnerJoinZip(c *test.C) {
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("Join1").Exec(sess)
Db("test").TableCreate("Join2").Exec(sess)
// Insert rows
Db("test").Table("Join1").Insert(joinTable1).Exec(sess)
Db("test").Table("Join2").Insert(joinTable2).Exec(sess)
// Test query
var response []interface{}
query := Db("test").Table("Join1").InnerJoin(Db("test").Table("Join2"), func(a, b Term) Term {
return a.Field("id").Eq(b.Field("id"))
}).Zip()
res, err := query.Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(err, test.IsNil)
c.Assert(response, jsonEquals, []interface{}{
map[string]interface{}{"title": "goof", "name": "bob", "id": 0},
map[string]interface{}{"title": "lmoe", "name": "joe", "id": 2},
})
}
func (s *RethinkSuite) TestJoinOuterJoinZip(c *test.C) {
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("Join1").Exec(sess)
Db("test").TableCreate("Join2").Exec(sess)
// Insert rows
Db("test").Table("Join1").Insert(joinTable1).Exec(sess)
Db("test").Table("Join2").Insert(joinTable2).Exec(sess)
// Test query
var response []interface{}
query := Db("test").Table("Join1").OuterJoin(Db("test").Table("Join2"), func(a, b Term) Term {
return a.Field("id").Eq(b.Field("id"))
}).Zip().OrderBy("id")
res, err := query.Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(err, test.IsNil)
c.Assert(response, jsonEquals, []interface{}{
map[string]interface{}{"title": "goof", "name": "bob", "id": 0},
map[string]interface{}{"name": "tom", "id": 1},
map[string]interface{}{"title": "lmoe", "name": "joe", "id": 2},
})
}
func (s *RethinkSuite) TestJoinEqJoinZip(c *test.C) {
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("Join1").Exec(sess)
Db("test").TableCreate("Join2").Exec(sess)
// Insert rows
Db("test").Table("Join1").Insert(joinTable1).Exec(sess)
Db("test").Table("Join2").Insert(joinTable2).Exec(sess)
// Test query
var response []interface{}
query := Db("test").Table("Join1").EqJoin("id", Db("test").Table("Join2")).Zip()
res, err := query.Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(response, jsonEquals, []interface{}{
map[string]interface{}{"title": "goof", "name": "bob", "id": 0},
map[string]interface{}{"title": "lmoe", "name": "joe", "id": 2},
})
}
func (s *RethinkSuite) TestJoinEqJoinDiffIdsZip(c *test.C) {
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("Join1").Exec(sess)
err := Db("test").TableCreate("Join3", TableCreateOpts{
PrimaryKey: "it",
}).Exec(sess)
c.Assert(err, test.IsNil)
Db("test").Table("Join3").IndexCreate("it").Exec(sess)
// Insert rows
Db("test").Table("Join1").Delete().Exec(sess)
Db("test").Table("Join3").Delete().Exec(sess)
Db("test").Table("Join1").Insert(joinTable1).Exec(sess)
Db("test").Table("Join3").Insert(joinTable3).Exec(sess)
// Test query
var response []interface{}
query := Db("test").Table("Join1").EqJoin("id", Db("test").Table("Join3"), EqJoinOpts{
Index: "it",
}).Zip()
res, err := query.Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(response, jsonEquals, []interface{}{
map[string]interface{}{"title": "goof", "name": "bob", "id": 0, "it": 0},
map[string]interface{}{"title": "lmoe", "name": "joe", "id": 2, "it": 2},
})
}
func (s *RethinkSuite) TestOrderByJoinEq(c *test.C) {
type Map map[string]interface{}
var err error
// Ensure table + database exist
DbCreate("test").Exec(sess)
Db("test").TableCreate("test").Exec(sess)
Db("test").TableCreate("test2").Exec(sess)
tab := Db("test").Table("test")
tab2 := Db("test").Table("test2")
// insert rows
err = tab.Insert(Map{"S": "s1", "T": 2}).Exec(sess)
err = tab.Insert(Map{"S": "s1", "T": 1}).Exec(sess)
err = tab.Insert(Map{"S": "s1", "T": 3}).Exec(sess)
err = tab.Insert(Map{"S": "s2", "T": 3}).Exec(sess)
c.Assert(err, test.IsNil)
err = tab2.Insert(Map{"id": "s1", "N": "Rob"}).Exec(sess)
err = tab2.Insert(Map{"id": "s2", "N": "Zar"}).Exec(sess)
c.Assert(err, test.IsNil)
// Test query
var response []Map
res, err := tab.OrderBy("T").EqJoin("S", tab2).Run(sess)
c.Assert(err, test.IsNil)
err = res.All(&response)
c.Assert(err, test.IsNil)
c.Check(len(response), test.Equals, 4, test.Commentf("%v", response))
}