This repository was archived by the owner on Sep 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadmin_test.go
More file actions
203 lines (168 loc) · 5.25 KB
/
admin_test.go
File metadata and controls
203 lines (168 loc) · 5.25 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
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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestDropData(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "DropDataWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "DropDataWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
entity := TestEntity{
Name: "Test Entity",
Description: "This is a test entity for the Insert method",
CreatedAt: time.Now(),
}
ctx := context.Background()
err := client.Insert(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
uid := entity.UID
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, entity.Name, "Test Entity", "Name should match")
require.Equal(t, entity.Description, "This is a test entity for the Insert method", "Description should match")
err = client.DropData(ctx)
require.NoError(t, err, "DropData should succeed")
err = client.Get(ctx, &entity, uid)
require.Error(t, err, "Get should fail after DropData")
schema, err := client.GetSchema(ctx)
require.NoError(t, err, "GetSchema should succeed")
require.Contains(t, schema, "type TestEntity")
})
}
}
func TestDropAll(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "DropAllWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "DropAllWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
entity := TestEntity{
Name: "Test Entity",
Description: "This is a test entity for the Insert method",
CreatedAt: time.Now(),
}
ctx := context.Background()
err := client.Insert(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
uid := entity.UID
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, entity.Name, "Test Entity", "Name should match")
require.Equal(t, entity.Description, "This is a test entity for the Insert method", "Description should match")
err = client.DropAll(ctx)
require.NoError(t, err, "DropAll should succeed")
err = client.Get(ctx, &entity, uid)
require.Error(t, err, "Get should fail after DropAll")
schema, err := client.GetSchema(ctx)
require.NoError(t, err, "GetSchema should succeed")
require.NotContains(t, schema, "type TestEntity")
})
}
}
type Struct1 struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty" dgraph:"index=term"`
DType []string `json:"dgraph.type,omitempty"`
}
type Struct2 struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty" dgraph:"index=term"`
DType []string `json:"dgraph.type,omitempty"`
}
type Struct3 struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty" dgraph:"index=term"`
DType []string `json:"dgraph.type,omitempty"`
Struct1 *Struct1 `json:"struct1,omitempty"`
Struct2 *Struct2 `json:"struct2,omitempty"`
}
func TestCreateSchema(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "CreateSchemaWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "CreateSchemaWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
err := client.UpdateSchema(context.Background(), &Struct1{}, &Struct2{})
require.NoError(t, err, "UpdateSchema should succeed")
schema, err := client.GetSchema(context.Background())
require.NoError(t, err, "GetSchema should succeed")
require.Contains(t, schema, "type Struct1")
require.Contains(t, schema, "type Struct2")
err = client.DropAll(context.Background())
require.NoError(t, err, "DropAll should succeed")
// Test updating schema with nested types
err = client.UpdateSchema(context.Background(), &Struct3{})
require.NoError(t, err, "UpdateSchema should succeed")
schema, err = client.GetSchema(context.Background())
require.NoError(t, err, "GetSchema should succeed")
require.Contains(t, schema, "type Struct1")
require.Contains(t, schema, "type Struct2")
require.Contains(t, schema, "type Struct3")
})
}
}