MCPcopy
hub / github.com/dgraph-io/dgraph / TestNamespaces

Function TestNamespaces

edgraph/namespace_test.go:47–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

45}
46
47func TestNamespaces(t *testing.T) {
48 dc := dgraphtest.NewComposeCluster()
49 client, cleanup, err := dc.Client()
50 require.NoError(t, err)
51 defer cleanup()
52
53 // Drop all data
54 require.NoError(t, client.Login(context.Background(),
55 dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
56 require.NoError(t, client.Dgraph.DropAll(context.Background()))
57
58 // Create two namespaces
59 ctx := context.Background()
60 ns1, err := client.CreateNamespace(ctx)
61 require.NoError(t, err)
62 ns2, err := client.CreateNamespace(ctx)
63 require.NoError(t, err)
64
65 // namespace 1
66 require.NoError(t, client.LoginIntoNamespace(ctx,
67 dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns1))
68 require.NoError(t, client.SetSchema(ctx, `name: string @index(exact) .`))
69 resp, err := client.RunDQL(ctx, `{ set {_:a <name> "Alice" .}}`)
70 require.NoError(t, err)
71 require.Equal(t, 1, len(resp.Uids))
72 resp, err = client.RunDQL(ctx, `{ q(func: has(name)) { name } }`)
73 require.NoError(t, err)
74 require.JSONEq(t, `{"q":[{"name":"Alice"}]}`, string(resp.GetJson()))
75
76 // namespace 2
77 require.NoError(t, client.LoginIntoNamespace(ctx,
78 dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns2))
79 require.NoError(t, client.SetSchema(ctx, `name: string @index(exact) .`))
80 _, err = client.RunDQL(ctx, `{ set {_:a <name> "Bob" .}}`)
81 require.NoError(t, err)
82 resp, err = client.RunDQL(ctx, `{ q(func: has(name)) { name } }`)
83 require.NoError(t, err)
84 require.JSONEq(t, `{"q":[{"name":"Bob"}]}`, string(resp.GetJson()))
85
86 // List Namespaces
87 require.NoError(t, client.Login(context.Background(),
88 dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
89 nsMaps, err := client.ListNamespaces(ctx)
90 require.NoError(t, err)
91 require.Len(t, nsMaps, 3)
92
93 // drop ns2-new namespace
94 require.NoError(t, client.DropNamespace(ctx, ns2))
95 require.ErrorContains(t, client.LoginIntoNamespace(ctx,
96 dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns2),
97 "invalid username or password")
98
99 nsMaps, err = client.ListNamespaces(ctx)
100 require.NoError(t, err)
101 require.Len(t, nsMaps, 2)
102}
103
104func TestDropNamespaceErr(t *testing.T) {

Callers

nothing calls this directly

Calls 11

ClientMethod · 0.95
NewComposeClusterFunction · 0.92
cleanupFunction · 0.85
LoginMethod · 0.80
DropAllMethod · 0.80
CreateNamespaceMethod · 0.80
LoginIntoNamespaceMethod · 0.80
RunDQLMethod · 0.80
DropNamespaceMethod · 0.80
LenMethod · 0.65
ListNamespacesMethod · 0.45

Tested by

no test coverage detected