-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmutation_test.go
103 lines (87 loc) · 2.99 KB
/
mutation_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
//go:build integration || upgrade
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package query
import (
"context"
"encoding/json"
"fmt"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/hypermodeinc/dgraph/v25/dgraphtest"
)
func TestReservedPredicateForMutation(t *testing.T) {
err := addTriplesToCluster(`_:x <dgraph.graphql.schema> "df"`)
require.Error(t, err, "Cannot mutate graphql reserved predicate dgraph.graphql.schema")
}
func TestAlteringReservedTypesAndPredicatesShouldFail(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
op := &api.Operation{Schema: `
type dgraph.Person {
name: string
age: int
}
name: string .
age: int .
`}
err := client.Alter(context.Background(), op)
require.Error(t, err, "altering type in dgraph namespace shouldn't have succeeded")
require.Contains(t, err.Error(), "Can't alter type `dgraph.Person` as it is prefixed with "+
"`dgraph.` which is reserved as the namespace for dgraph's internal types/predicates.")
op = &api.Operation{Schema: `
type Person {
dgraph.name
age
}
dgraph.name: string .
age: int .
`}
err = client.Alter(ctx, op)
require.Error(t, err, "altering predicate in dgraph namespace shouldn't have succeeded")
require.Contains(t, err.Error(), "Can't alter predicate `dgraph.name` as it is prefixed with "+
"`dgraph.` which is reserved as the namespace for dgraph's internal types/predicates.")
_, err = client.NewTxn().Mutate(ctx, &api.Mutation{
SetNquads: []byte(`_:new <dgraph.name> "Alice" .`),
})
require.Error(t, err, "storing predicate in dgraph namespace shouldn't have succeeded")
require.Contains(t, err.Error(), "Can't store predicate `dgraph.name` as it is prefixed with "+
"`dgraph.` which is reserved as the namespace for dgraph's internal types/predicates.")
}
func TestUnreservedPredicateForDeletion(t *testing.T) {
// This bug was fixed in commit 8631dab37c951b288f839789bbabac5e7088b58f
dgraphtest.ShouldSkipTest(t, "8631dab37c951b288f839789bbabac5e7088b58f", dc.GetVersion())
grootUserQuery := `
{
grootUser(func:eq(dgraph.xid, "groot")){
uid
}
}`
type userQryResp struct {
GrootUser []struct {
Uid string `json:"uid"`
} `json:"grootUser"`
}
resp, err := client.Query(grootUserQuery)
require.NoError(t, err, "groot user query failed")
var userResp userQryResp
require.NoError(t, json.Unmarshal(resp.GetJson(), &userResp))
grootsUidStr := userResp.GrootUser[0].Uid
grootsUidInt, err := strconv.ParseUint(grootsUidStr, 0, 64)
require.NoError(t, err)
require.Greater(t, uint64(grootsUidInt), uint64(0))
const testSchema = `
type Star {
name
}
name : string @index(term, exact, trigram) @count @lang .`
setSchema(testSchema)
triple := fmt.Sprintf(`<%s> <dgraphcore_355> "Betelgeuse" .`, grootsUidStr)
require.NoError(t, addTriplesToCluster(triple))
deleteTriplesInCluster(triple)
}