forked from dgraph-io/dgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_system_test.go
295 lines (246 loc) · 6.73 KB
/
type_system_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
/*
* Copyright (C) 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dgo_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/stretchr/testify/require"
)
var (
alicename = "alice"
personType = `
type person {
name: string
age: int
}`
empType = `
type employee {
name: string
email: string
works_at: string
}`
)
func addType(t *testing.T, dg *dgo.Dgraph, newType string) {
op := &api.Operation{
Schema: newType,
}
err := dg.Alter(context.Background(), op)
require.NoError(t, err, "error while creating type: %s", newType)
}
func initializeDBTypeSystem(t *testing.T, dg *dgo.Dgraph) {
op := &api.Operation{
Schema: `
email: string @index(exact) .
name: string @index(exact) .
works_at: string @index(exact) .
age: int .`,
}
err := dg.Alter(context.Background(), op)
require.NoError(t, err)
mu := &api.Mutation{
CommitNow: true,
SetNquads: []byte(`
_:user <email> "[email protected]" .
_:user <name> "alice" .
_:user <empid> "1" .
_:user <works_at> "company1" .
_:user <age> "20" .`),
}
_, err = dg.NewTxn().Mutate(context.Background(), mu)
require.NoError(t, err, "unable to insert record")
}
func TestNoType(t *testing.T) {
dg, cancel := getDgraphClient()
defer cancel()
ctx := context.Background()
err := dg.Alter(ctx, &api.Operation{DropAll: true})
require.NoError(t, err)
initializeDBTypeSystem(t, dg)
q := `{
q(func: eq(name, "%s")) {
dgraph.type
}
}`
res, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q, alicename))
var ts struct {
Q []struct {
DgraphType []string `json:"dgraph.type"`
} `json:"q"`
}
err = json.Unmarshal(res.Json, &ts)
require.NoError(t, err, "unable to parse type response")
require.Zero(t, len(ts.Q), "no dgraph type has been assigned")
}
func TestSingleType(t *testing.T) {
// Setup.
dg, cancel := getDgraphClient()
defer cancel()
ctx := context.Background()
err := dg.Alter(ctx, &api.Operation{DropAll: true})
require.NoError(t, err)
initializeDBTypeSystem(t, dg)
addType(t, dg, personType)
// Update type of user to person.
q1 := `{
v as var(func: eq(name, "%s"))
}`
req := &api.Request{
CommitNow: true,
Query: fmt.Sprintf(q1, alicename),
Mutations: []*api.Mutation{
{SetNquads: []byte(`uid(v) <dgraph.type> "person" .`)},
},
}
_, err = dg.NewTxn().Do(context.Background(), req)
require.NoError(t, err, "unable to add type person to user")
// Verify if type of user is person.
q2 := `{
q(func: eq(name, "%s")) {
dgraph.type
}
}`
res1, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q2, alicename))
var ts struct {
Q []struct {
DgraphType []string `json:"dgraph.type"`
} `json:"q"`
}
err = json.Unmarshal(res1.Json, &ts)
require.NoError(t, err, "unable to parse type response")
require.Equal(t, 1, len(ts.Q[0].DgraphType), "one dgraph type has been assigned")
// Perform expand(_all_) on
q3 := `{
q(func: eq(name, "%s")) {
expand(_all_)
}
}`
res2, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q3, alicename))
require.NoError(t, err, "unable to expand for type user")
var ts2 struct {
Q []struct {
Name string `json:"name"`
Age int `json:"age"`
} `json:"q"`
}
err = json.Unmarshal(res2.Json, &ts2)
require.NoError(t, err, "unable to parse json in expand response")
require.Equal(t, len(ts2.Q), 1)
require.Equal(t, ts2.Q[0].Name, alicename)
require.Equal(t, ts2.Q[0].Age, 20)
// Delete S * *
q4 := `{
v as var(func: eq(name, "%s"))
}`
req2 := &api.Request{
CommitNow: true,
Query: fmt.Sprintf(q4, alicename),
Mutations: []*api.Mutation{
{DelNquads: []byte(`uid(v) * * .`)},
},
}
_, err = dg.NewTxn().Do(context.Background(), req2)
require.NoError(t, err, "unable to execute S * *")
q5 := `{
q(func: eq(name, %s)) {
name
age
email
works_at
}
}`
res3, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q5, alicename))
require.NoError(t, err, "error while querying after delete S * *")
var ts3 struct {
Q []struct {
Name string `json:"name"`
Age int `json:"age"`
Email string `json:"email"`
WorksAt string `json:"works_at"`
} `json:"q"`
}
require.NoError(t, json.Unmarshal(res3.Json, &ts3))
require.Zero(t, len(ts3.Q))
}
func TestMultipleType(t *testing.T) {
// Setup.
dg, cancel := getDgraphClient()
defer cancel()
ctx := context.Background()
err := dg.Alter(ctx, &api.Operation{DropAll: true})
require.NoError(t, err)
initializeDBTypeSystem(t, dg)
addType(t, dg, personType)
addType(t, dg, empType)
// Add person and employee to user.
q1 := `{
v as var(func: eq(name, "%s"))
}`
mu := `
uid(v) <dgraph.type> "person" .
uid(v) <dgraph.type> "employee" .
`
req := &api.Request{
CommitNow: true,
Query: fmt.Sprintf(q1, alicename),
Mutations: []*api.Mutation{
{SetNquads: []byte(mu)},
},
}
_, err = dg.NewTxn().Do(context.Background(), req)
require.NoError(t, err, "unable to add type person to user")
// Read Types for user.
q2 := `{
q(func: eq(name, "%s")) {
dgraph.type
}
}`
res1, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q2, alicename))
var ts struct {
Q []struct {
DgraphType []string `json:"dgraph.type"`
} `json:"q"`
}
err = json.Unmarshal(res1.Json, &ts)
require.NoError(t, err, "unable to parse type response")
require.Equal(t, 2, len(ts.Q[0].DgraphType), "one dgraph type has been assigned")
// Test expand(_all_) for user.
q3 := `{
q(func: eq(name, "%s")) {
expand(_all_)
}
}`
res2, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q3, alicename))
require.NoError(t, err, "unable to expand for type user")
var ts2 struct {
Q []struct {
Name string `json:"name"`
Age int `json:"age"`
Email string `json:"email"`
WorksAt string `json:"works_at"`
} `json:"q"`
}
err = json.Unmarshal(res2.Json, &ts2)
require.NoError(t, err, "unable to parse json in expand response")
require.Equal(t, len(ts2.Q), 1)
require.Equal(t, ts2.Q[0].Name, alicename)
require.Equal(t, ts2.Q[0].Age, 20)
require.Equal(t, ts2.Q[0].Email, "[email protected]")
require.Equal(t, ts2.Q[0].WorksAt, "company1")
}