@@ -8,50 +8,91 @@ import (
88 "github.com/aep-dev/aep-lib-go/pkg/openapi"
99)
1010
11- var projectResource = api.Resource {
12- Singular : "project" ,
13- Plural : "projects" ,
14- PatternElems : []string {"projects" , "{project}" },
15- Parents : []* api.Resource {},
16- Schema : & openapi.Schema {
17- Properties : map [string ]openapi.Schema {
18- "name" : {
19- Type : "string" ,
20- },
21- "description" : {
22- Type : "string" ,
11+ func getTestAPI () * api.API {
12+ projectResource := api.Resource {
13+ Singular : "project" ,
14+ Plural : "projects" ,
15+ Parents : []string {},
16+ Schema : & openapi.Schema {
17+ Properties : map [string ]openapi.Schema {
18+ "name" : {
19+ Type : "string" ,
20+ },
21+ "description" : {
22+ Type : "string" ,
23+ },
24+ "active" : {
25+ Type : "boolean" ,
26+ },
27+ "tags" : {
28+ Type : "array" ,
29+ Items : & openapi.Schema {
30+ Type : "string" ,
31+ },
32+ },
33+ "metadata" : {
34+ Type : "object" ,
35+ },
36+ "priority" : {
37+ Type : "integer" ,
38+ },
2339 },
24- "active" : {
25- Type : "boolean" ,
40+ Required : []string {"name" },
41+ },
42+ Methods : api.Methods {
43+ Get : & api.GetMethod {},
44+ List : & api.ListMethod {},
45+ Create : & api.CreateMethod {
46+ SupportsUserSettableCreate : true ,
2647 },
27- "tags" : {
28- Type : "array" ,
29- Items : & openapi.Schema {
30- Type : "string" ,
48+ Update : & api.UpdateMethod {},
49+ Delete : & api.DeleteMethod {},
50+ },
51+ }
52+
53+ a := & api.API {
54+ Name : "test" ,
55+ ServerURL : "https://api.example.com" ,
56+ Resources : map [string ]* api.Resource {
57+ "project" : & projectResource ,
58+ "dataset" : & api.Resource {
59+ Singular : "dataset" ,
60+ Plural : "datasets" ,
61+ Parents : []string {"project" },
62+ Schema : & openapi.Schema {},
63+ Methods : api.Methods {
64+ Get : & api.GetMethod {},
65+ List : & api.ListMethod {},
66+ Create : & api.CreateMethod {},
67+ Update : & api.UpdateMethod {},
68+ Delete : & api.DeleteMethod {},
3169 },
3270 },
33- "metadata" : {
34- Type : "object" ,
71+ "user" : & api.Resource {
72+ Singular : "user" ,
73+ Plural : "users" ,
74+ Parents : []string {},
75+ Schema : & openapi.Schema {},
3576 },
36- "priority" : {
37- Type : "integer" ,
77+ "comment" : & api.Resource {
78+ Singular : "comment" ,
79+ Plural : "comments" ,
80+ Parents : []string {},
81+ Schema : & openapi.Schema {},
3882 },
3983 },
40- Required : []string {"name" },
41- },
42- GetMethod : & api.GetMethod {},
43- ListMethod : & api.ListMethod {},
44- CreateMethod : & api.CreateMethod {
45- SupportsUserSettableCreate : true ,
46- },
47- UpdateMethod : & api.UpdateMethod {},
48- DeleteMethod : & api.DeleteMethod {},
84+ }
85+ err := api .AddImplicitFieldsAndValidate (a )
86+ if err != nil {
87+ panic (err )
88+ }
89+ return a
4990}
5091
5192func TestExecuteCommand (t * testing.T ) {
5293 tests := []struct {
5394 name string
54- resource api. Resource
95+ resource string
5596 args []string
5697 expectedQuery string
5798 expectedPath string
@@ -61,7 +102,7 @@ func TestExecuteCommand(t *testing.T) {
61102 }{
62103 {
63104 name : "simple resource no parents" ,
64- resource : projectResource ,
105+ resource : "project" ,
65106 args : []string {"list" },
66107 expectedPath : "projects" ,
67108 expectedMethod : "GET" ,
@@ -70,7 +111,7 @@ func TestExecuteCommand(t *testing.T) {
70111 },
71112 {
72113 name : "create with tags" ,
73- resource : projectResource ,
114+ resource : "project" ,
74115 args : []string {"create" , "myproject" , "--name=test-project" , "--tags=tag1,tag2,tag3" },
75116 expectedPath : "projects" ,
76117 expectedMethod : "POST" ,
@@ -80,7 +121,7 @@ func TestExecuteCommand(t *testing.T) {
80121 },
81122 {
82123 name : "create with tags quoted" ,
83- resource : projectResource ,
124+ resource : "project" ,
84125 args : []string {"create" , "myproject" , "--name=test-project" , "--tags=\" tag1,\" ,tag2,tag3" },
85126 expectedPath : "projects" ,
86127 expectedMethod : "POST" ,
@@ -89,19 +130,8 @@ func TestExecuteCommand(t *testing.T) {
89130 body : `{"name":"test-project","tags":["tag1,","tag2","tag3"]}` ,
90131 },
91132 {
92- name : "resource with parent" ,
93- resource : api.Resource {
94- Singular : "dataset" ,
95- Plural : "datasets" ,
96- PatternElems : []string {"projects" , "{project}" , "datasets" , "{dataset}" },
97- Parents : []* api.Resource {& projectResource },
98- Schema : & openapi.Schema {},
99- GetMethod : & api.GetMethod {},
100- ListMethod : & api.ListMethod {},
101- CreateMethod : & api.CreateMethod {},
102- UpdateMethod : & api.UpdateMethod {},
103- DeleteMethod : & api.DeleteMethod {},
104- },
133+ name : "resource with parent" ,
134+ resource : "dataset" ,
105135 args : []string {"--project=foo" , "get" , "abc" },
106136 expectedPath : "projects/foo/datasets/abc" ,
107137 expectedMethod : "GET" ,
@@ -112,7 +142,8 @@ func TestExecuteCommand(t *testing.T) {
112142
113143 for _ , tt := range tests {
114144 t .Run (tt .name , func (t * testing.T ) {
115- req , _ , err := ExecuteResourceCommand (& tt .resource , tt .args )
145+ a := getTestAPI ()
146+ req , _ , err := ExecuteResourceCommand (a .Resources [tt .resource ], tt .args )
116147 if (err != nil ) != tt .wantErr {
117148 t .Errorf ("ExecuteCommand() error = %v, wantErr %v" , err , tt .wantErr )
118149 return
0 commit comments