-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathns_query_no_acl_test.go
More file actions
87 lines (70 loc) · 2.76 KB
/
ns_query_no_acl_test.go
File metadata and controls
87 lines (70 loc) · 2.76 KB
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
//go:build integration2
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package edgraph
import (
"context"
"testing"
"github.com/dgraph-io/dgo/v250"
"github.com/stretchr/testify/require"
"github.com/hypermodeinc/dgraph/v25/dgraphtest"
)
func TestNamespaces(t *testing.T) {
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1)
c, err := dgraphtest.NewLocalCluster(conf)
require.NoError(t, err)
defer func() { c.Cleanup(t.Failed()) }()
require.NoError(t, c.Start())
// ensure that Open works with no ACL
alphaGrpcPort, err := c.GetAlphaGrpcPublicPort(0)
require.NoError(t, err)
_, err = dgo.Open("dgraph://localhost:" + alphaGrpcPort)
require.NoError(t, err)
client, cleanup, err := c.Client()
require.NoError(t, err)
defer cleanup()
// Drop all data
require.NoError(t, client.DropAll())
// Create two namespaces
ctx := context.Background()
require.NoError(t, client.CreateNamespace(ctx, "ns1"))
require.NoError(t, client.CreateNamespace(ctx, "ns2"))
// namespace 1
require.NoError(t, client.SetSchema(ctx, "ns1", `name: string @index(exact) .`))
resp, err := client.RunDQL(ctx, "ns1", `{ set {_:a <name> "Alice" .}}`)
require.NoError(t, err)
require.Equal(t, 1, len(resp.BlankUids))
resp, err = client.RunDQL(ctx, "ns1", `{ q(func: has(name)) { name } }`)
require.NoError(t, err)
require.JSONEq(t, `{"q":[{"name":"Alice"}]}`, string(resp.GetQueryResult()))
// namespace 2
require.NoError(t, client.SetSchema(ctx, "ns2", `name: string @index(exact) .`))
_, err = client.RunDQL(ctx, "ns2", `{ set {_:a <name> "Bob" .}}`)
require.NoError(t, err)
resp, err = client.RunDQL(ctx, "ns2", `{ q(func: has(name)) { name } }`)
require.NoError(t, err)
require.JSONEq(t, `{"q":[{"name":"Bob"}]}`, string(resp.GetQueryResult()))
// rename ns2 namespace
require.NoError(t, client.RenameNamespace(ctx, "ns2", "ns2-new"))
// check if the data is still there
resp, err = client.RunDQL(ctx, "ns2-new", `{ q(func: has(name)) { name } }`)
require.NoError(t, err)
require.JSONEq(t, `{"q":[{"name":"Bob"}]}`, string(resp.GetQueryResult()))
// List Namespaces
nsMaps, err := client.ListNamespaces(ctx)
require.NoError(t, err)
require.Len(t, nsMaps, 3)
// drop ns2-new namespace
require.NoError(t, client.DropNamespace(ctx, "ns2-new"))
_, err = client.RunDQL(ctx, "ns2-new", `{ q(func: has(name)) { name } }`)
require.ErrorContains(t, err, "namespace \"ns2-new\" not found")
nsMaps, err = client.ListNamespaces(ctx)
require.NoError(t, err)
require.Len(t, nsMaps, 2)
// drop ns1 namespace
require.NoError(t, client.DropNamespace(ctx, "ns1"))
_, err = client.RunDQL(ctx, "ns1", `{ q(func: has(name)) { name } }`)
require.ErrorContains(t, err, "namespace \"ns1\" not found")
}