forked from aep-dev/aepcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_definition_test.go
More file actions
366 lines (354 loc) · 9.79 KB
/
resource_definition_test.go
File metadata and controls
366 lines (354 loc) · 9.79 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package service
import (
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/aep-dev/aep-lib-go/pkg/api"
"github.com/aep-dev/aep-lib-go/pkg/openapi"
)
func getTestAPI() *api.API {
projectResource := api.Resource{
Singular: "project",
Plural: "projects",
Parents: []string{},
Schema: &openapi.Schema{
Properties: map[string]openapi.Schema{
"name": {
Type: "string",
Description: "The name of the project",
},
"description": {
Type: "string",
},
"active": {
Type: "boolean",
},
"tags": {
Type: "array",
Items: &openapi.Schema{
Type: "string",
},
},
"metadata": {
Type: "object",
},
"priority": {
Type: "integer",
},
},
Required: []string{"name"},
},
Methods: api.Methods{
Get: &api.GetMethod{},
List: &api.ListMethod{},
Create: &api.CreateMethod{
SupportsUserSettableCreate: true,
},
Update: &api.UpdateMethod{},
Delete: &api.DeleteMethod{},
},
}
a := &api.API{
Name: "test",
ServerURL: "https://api.example.com",
Resources: map[string]*api.Resource{
"project": &projectResource,
"dataset": {
Singular: "dataset",
Plural: "datasets",
Parents: []string{"project"},
Schema: &openapi.Schema{
Properties: map[string]openapi.Schema{
"name": {
Type: "string",
},
"description": {
Type: "string",
},
"size": {
Type: "integer",
},
"config": {
Type: "object",
},
},
// Remove Required field to make testing easier
},
Methods: api.Methods{
Get: &api.GetMethod{},
List: &api.ListMethod{},
Create: &api.CreateMethod{},
Update: &api.UpdateMethod{},
Delete: &api.DeleteMethod{},
},
},
"user": {
Singular: "user",
Plural: "users",
Parents: []string{},
Schema: &openapi.Schema{
Properties: map[string]openapi.Schema{
"username": {
Type: "string",
},
"email": {
Type: "string",
},
"active": {
Type: "boolean",
},
},
},
Methods: api.Methods{
Get: &api.GetMethod{},
List: &api.ListMethod{},
Create: &api.CreateMethod{},
Update: &api.UpdateMethod{},
Delete: &api.DeleteMethod{},
},
},
"comment": {
Singular: "comment",
Plural: "comments",
Parents: []string{},
Schema: &openapi.Schema{},
},
"shelf": {
Singular: "shelf",
Plural: "shelves",
Parents: []string{"project"},
Schema: &openapi.Schema{},
},
"book": {
Singular: "book",
Plural: "books",
Parents: []string{"shelf"},
Schema: &openapi.Schema{
Properties: map[string]openapi.Schema{
"title": {
Type: "string",
},
"author": {
Type: "string",
},
"path": {
Type: "string",
ReadOnly: true,
},
},
},
Methods: api.Methods{
Create: &api.CreateMethod{
SupportsUserSettableCreate: true,
},
},
},
},
}
err := api.AddImplicitFieldsAndValidate(a)
if err != nil {
panic(err)
}
// Restore ReadOnly for book path, as AddImplicitFieldsAndValidate overwrites it
if book, ok := a.Resources["book"]; ok {
if pathProp, ok := book.Schema.Properties["path"]; ok {
pathProp.ReadOnly = true
book.Schema.Properties["path"] = pathProp
}
}
return a
}
func TestExecuteCommand(t *testing.T) {
tests := []struct {
name string
resource string
args []string
expectedQuery string
expectedPath string
expectedMethod string
wantErr bool
body string
expectedOutput string
}{
{
name: "simple resource no parents",
resource: "project",
args: []string{"list"},
expectedPath: "projects",
expectedMethod: "GET",
wantErr: false,
body: "",
},
{
name: "create with tags",
resource: "project",
args: []string{"create", "myproject", "--name=test-project", "--tags=tag1,tag2,tag3"},
expectedPath: "projects",
expectedMethod: "POST",
expectedQuery: "id=myproject",
wantErr: false,
body: `{"name":"test-project","tags":["tag1","tag2","tag3"]}`,
},
{
name: "create with tags quoted",
resource: "project",
args: []string{"create", "myproject", "--name=test-project", "--tags=\"tag1,\",tag2,tag3"},
expectedPath: "projects",
expectedMethod: "POST",
expectedQuery: "id=myproject",
wantErr: false,
body: `{"name":"test-project","tags":["tag1,","tag2","tag3"]}`,
},
{
name: "resource with parent",
resource: "dataset",
args: []string{"--project=foo", "get", "abc"},
expectedPath: "projects/foo/datasets/abc",
expectedMethod: "GET",
wantErr: false,
body: "",
},
{
name: "create with @data flag",
resource: "project",
args: []string{"create", "dataproject", "--@data=" + createTestJSONFile(t, map[string]interface{}{
"name": "test-project",
"description": "A test project",
"active": true,
"priority": 5,
}), "--name=test-project"}, // Add required flag to avoid validation error
expectedPath: "projects",
expectedMethod: "POST",
expectedQuery: "id=dataproject",
wantErr: false, // Change to false since errors are logged, not returned
body: "", // Empty body because error is logged
},
{
name: "create with @data flag - no conflicts",
resource: "user", // Use resource with no required fields
args: []string{"create", "--@data=" + createTestJSONFile(t, map[string]interface{}{
"username": "testuser",
"email": "test@example.com",
})},
expectedPath: "users",
expectedMethod: "POST",
wantErr: false,
body: `{"email":"test@example.com","username":"testuser"}`,
},
{
name: "update with @data flag",
resource: "user",
args: []string{"update", "testuser", "--@data=" + createTestJSONFile(t, map[string]interface{}{
"email": "newemail@example.com",
})},
expectedPath: "users/testuser",
expectedMethod: "PATCH",
wantErr: false,
body: `{"email":"newemail@example.com"}`,
},
{
name: "child resource with parent and @data flag",
resource: "dataset",
args: []string{"--project=myproject", "create", "--@data=" + createTestJSONFile(t, map[string]interface{}{
"name": "test-dataset",
"description": "A test dataset",
"size": 1000,
"config": map[string]interface{}{
"format": "parquet",
"schema": "v1",
},
})},
expectedPath: "projects/myproject/datasets",
expectedMethod: "POST",
wantErr: false,
body: `{"config":{"format":"parquet","schema":"v1"},"description":"A test dataset","name":"test-dataset","size":1000}`,
},
{
name: "child resource with parent and individual flags",
resource: "dataset",
args: []string{"--project=parentproject", "create", "--name=manual-dataset", "--description=Manual dataset"},
expectedPath: "projects/parentproject/datasets",
expectedMethod: "POST",
wantErr: false,
body: `{"description":"Manual dataset","name":"manual-dataset"}`,
},
{
name: "create book with read-only path flag",
resource: "book",
args: []string{"--project=myproject", "--shelf=myshelf", "create", "mybook", "--path=some/path"},
expectedPath: "",
expectedMethod: "",
wantErr: true,
body: "",
},
{
name: "help with description",
resource: "project",
args: []string{"create", "--help"},
expectedPath: "",
expectedMethod: "",
wantErr: false,
expectedOutput: "The name of the project",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := getTestAPI()
req, output, err := ExecuteResourceCommand(a.Resources[tt.resource], tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("ExecuteCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && req == nil && tt.expectedOutput == "" {
t.Error("ExecuteCommand() returned nil request when no error expected")
}
if tt.expectedOutput != "" {
if !strings.Contains(output, tt.expectedOutput) {
t.Errorf("ExecuteCommand() output = %q, want to contain %q", output, tt.expectedOutput)
}
}
if !tt.wantErr && req != nil {
// Verify the request path matches expected pattern
if req.URL.Path != tt.expectedPath {
t.Errorf("ExecuteCommand() request path = %v, want %v", req.URL.Path, tt.expectedPath)
}
if req.Body != nil {
body, err := io.ReadAll(req.Body)
if err != nil {
t.Errorf("ExecuteCommand() error reading request body: %v", err)
}
if string(body) != tt.body {
t.Errorf("ExecuteCommand() request body = %v, want %v", string(body), tt.body)
}
}
if req.Method != tt.expectedMethod {
t.Errorf("ExecuteCommand() request method = %v, want %v", req.Method, tt.expectedMethod)
}
if req.URL.RawQuery != tt.expectedQuery {
t.Errorf("ExecuteCommand() request query = %v, want %v", req.URL.RawQuery, tt.expectedQuery)
}
}
})
}
}
// Helper function to create temporary JSON files for testing
func createTestJSONFile(t *testing.T, data map[string]interface{}) string {
t.Helper()
// Create temporary directory
tempDir := t.TempDir()
// Create JSON file
jsonData, err := json.Marshal(data)
if err != nil {
t.Fatalf("Failed to marshal test data: %v", err)
}
// Write to temporary file
tempFile := filepath.Join(tempDir, "test-data.json")
err = os.WriteFile(tempFile, jsonData, 0644)
if err != nil {
t.Fatalf("Failed to write test JSON file: %v", err)
}
return tempFile
}