-
Notifications
You must be signed in to change notification settings - Fork 15
/
query_integration_test.go
315 lines (261 loc) · 6.76 KB
/
query_integration_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package dqlx_test
import (
"context"
_ "embed"
"encoding/json"
"testing"
"github.com/fenos/dqlx/testdata"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/fenos/dqlx"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
//go:embed testdata/dataset.json
var data string
type QueryIntegrationTest struct {
suite.Suite
db dqlx.DB
}
func (suite *QueryIntegrationTest) SetupTest() {
ctx := context.TODO()
db, err := dqlx.Connect("localhost:9080")
require.NoError(suite.T(), err)
suite.db = db
schema := testdata.TestSchema()
err = db.GetDgraph().Alter(ctx, &api.Operation{
Schema: schema,
//DropOp: api.Operation_ALL,
RunInBackground: false,
})
require.NoError(suite.T(), err)
var structure interface{}
err = json.Unmarshal([]byte(data), &structure)
require.NoError(suite.T(), err)
resp, err := suite.db.Mutation().Set(structure).Execute(ctx)
require.NoError(suite.T(), err)
println(resp.Raw.Uids)
}
func (suite *QueryIntegrationTest) TearDownTest() {
err := suite.db.GetDgraph().Alter(context.Background(), &api.Operation{
DropOp: api.Operation_ALL,
})
require.NoError(suite.T(), err)
}
func (suite *QueryIntegrationTest) TestFundamentals() {
ctx := context.TODO()
suite.Run("First Example", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.EqFn("name@en", "Blade Runner")).
Fields(`
uid
name@en
initial_release_date
netflix_id
`).
UnmarshalInto(&result).
Execute(ctx)
expected := []map[string]interface{}{
{
"uid": result[0]["uid"],
"name@en": "Blade Runner",
"initial_release_date": "1982-06-25T00:00:00Z",
"netflix_id": "70083726",
},
}
require.NoError(suite.T(), err)
require.ElementsMatch(suite.T(), result, expected)
})
suite.Run("By Id", func() {
var idRecords []map[string]interface{}
_, err := suite.db.Query(dqlx.EqFn("name@en", "Blade Runner")).
Select(`
uid
name@en
initial_release_date
netflix_id
`).
UnmarshalInto(&idRecords).
Execute(ctx)
require.NoError(suite.T(), err)
var result []map[string]interface{}
_, err = suite.db.
Query(dqlx.UIDFn(idRecords[0]["uid"])).
Fields(`
uid
name@en
initial_release_date
netflix_id
`).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
require.ElementsMatch(suite.T(), result, idRecords)
})
suite.Run("Anyofterms", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.AllOfTermsFn("name@en", "Blade Runner")).
Fields(`
name@en
initial_release_date
netflix_id
`).
UnmarshalInto(&result).
Execute(ctx)
expected := []map[string]interface{}{
{
"name@en": "Blade Runner",
"initial_release_date": "1982-06-25T00:00:00Z",
"netflix_id": "70083726",
},
}
require.NoError(suite.T(), err)
require.ElementsMatch(suite.T(), result, expected)
})
suite.Run("Expanding edge", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.EqFn("name@en", "Blade Runner")).
Fields(`
uid
name@en
initial_release_date
netflix_id
`).
Edge("starring").
Edge("starring->performance.actor", dqlx.Fields("name@en")).
Edge("starring->performance.character", dqlx.Fields("name@en")).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
starring := result[0]["starring"].([]interface{})
require.Len(suite.T(), starring, 15)
})
suite.Run("Applying filters", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.EqFn("name@en", "Ridley Scott")).
Fields(`
uid
name@en
initial_release_date
netflix_id
`).
Edge("director.film", dqlx.Fields(`
name@en
initial_release_date
`), dqlx.Le{"initial_release_date": "2000"}).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
require.Len(suite.T(), result, 2)
require.Len(suite.T(), result[0]["director.film"], 10)
suite.recordsContainsProperties(result[0]["director.film"].([]interface{}), []string{
"name@en",
"initial_release_date",
})
require.NotContains(suite.T(), result[1], "director.film")
})
suite.Run("Language support", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.AllOfTermsFn("name@en", "Farhan Akhtar")).
Fields(`
name@hi
name@en
`).
Edge("director.film", dqlx.Fields(`
name@ru:hi:en
name@en
name@hi
name@ru
`)).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
require.Len(suite.T(), result, 1)
suite.recordsContainsProperties(result, []string{
"name@en",
"name@hi",
})
suite.recordsContainsProperties(result[0]["director.film"], []string{
"name@ru:hi:en",
"name@en",
"name@hi",
"name@ru",
})
})
suite.Run("Count", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.EqFn("name@en", "Ridley Scott")).
Fields(
"uid",
"name@en",
"initial_release_date",
"netflix_id",
dqlx.Count("director.film"),
).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
require.Len(suite.T(), result, 2)
suite.containsProperties(result[0], []string{
"name@en",
"count(director.film)",
})
})
suite.Run("Aliases", func() {
var result []map[string]interface{}
_, err := suite.db.
Query(dqlx.EqFn("name@en", "Ridley Scott")).
Select(
"uid",
"name@en",
"alias_name:name@en",
"alias_id:uid",
":uid@en",
dqlx.Alias("count", dqlx.Count("director.film")),
dqlx.Alias("first_name_alias", "name@en"),
).
UnmarshalInto(&result).
Execute(ctx)
require.NoError(suite.T(), err)
require.Len(suite.T(), result, 2)
suite.containsProperties(result[0], []string{
"alias_id",
"name@en",
"count",
"alias_name",
"first_name_alias",
})
})
}
func (suite *QueryIntegrationTest) recordsContainsProperties(slice interface{}, properties []string) {
var records []interface{}
switch castSlices := slice.(type) {
case []interface{}:
records = castSlices
case []map[string]interface{}:
for _, item := range castSlices {
records = append(records, item)
}
default:
require.Fail(suite.T(), "contains properties invalid value %v", slice)
}
for _, record := range records {
for _, property := range properties {
require.Contains(suite.T(), record, property)
require.NotEmpty(suite.T(), record.(map[string]interface{})[property])
}
}
}
func (suite *QueryIntegrationTest) containsProperties(record interface{}, properties []string) {
for _, property := range properties {
require.Contains(suite.T(), record, property)
}
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(QueryIntegrationTest))
}