55 "io"
66 "os"
77 "path/filepath"
8+ "strings"
89 "testing"
910
1011 "github.com/aep-dev/aep-lib-go/pkg/api"
@@ -19,7 +20,8 @@ func getTestAPI() *api.API {
1920 Schema : & openapi.Schema {
2021 Properties : map [string ]openapi.Schema {
2122 "name" : {
22- Type : "string" ,
23+ Type : "string" ,
24+ Description : "The name of the project" ,
2325 },
2426 "description" : {
2527 Type : "string" ,
@@ -118,12 +120,49 @@ func getTestAPI() *api.API {
118120 Parents : []string {},
119121 Schema : & openapi.Schema {},
120122 },
123+ "shelf" : {
124+ Singular : "shelf" ,
125+ Plural : "shelves" ,
126+ Parents : []string {"project" },
127+ Schema : & openapi.Schema {},
128+ },
129+ "book" : {
130+ Singular : "book" ,
131+ Plural : "books" ,
132+ Parents : []string {"shelf" },
133+ Schema : & openapi.Schema {
134+ Properties : map [string ]openapi.Schema {
135+ "title" : {
136+ Type : "string" ,
137+ },
138+ "author" : {
139+ Type : "string" ,
140+ },
141+ "path" : {
142+ Type : "string" ,
143+ ReadOnly : true ,
144+ },
145+ },
146+ },
147+ Methods : api.Methods {
148+ Create : & api.CreateMethod {
149+ SupportsUserSettableCreate : true ,
150+ },
151+ },
152+ },
121153 },
122154 }
123155 err := api .AddImplicitFieldsAndValidate (a )
124156 if err != nil {
125157 panic (err )
126158 }
159+ // Restore ReadOnly for book path, as AddImplicitFieldsAndValidate overwrites it
160+ if book , ok := a .Resources ["book" ]; ok {
161+ if pathProp , ok := book .Schema .Properties ["path" ]; ok {
162+ pathProp .ReadOnly = true
163+ book .Schema .Properties ["path" ] = pathProp
164+ }
165+ }
127166 return a
128167}
129168
@@ -135,8 +174,10 @@ func TestExecuteCommand(t *testing.T) {
135174 expectedQuery string
136175 expectedPath string
137176 expectedMethod string
177+
138178 wantErr bool
139179 body string
180+ expectedOutput string
140181 }{
141182 {
142183 name : "simple resource no parents" ,
@@ -240,20 +281,43 @@ func TestExecuteCommand(t *testing.T) {
240281 wantErr : false ,
241282 body : `{"description":"Manual dataset","name":"manual-dataset"}` ,
242283 },
284+ {
285+ name : "create book with read-only path flag" ,
286+ resource : "book" ,
287+ args : []string {"--project=myproject" , "--shelf=myshelf" , "create" , "mybook" , "--path=some/path" },
288+ expectedPath : "" ,
289+ expectedMethod : "" ,
290+ wantErr : true ,
291+ body : "" ,
292+ },
293+ {
294+ name : "help with description" ,
295+ resource : "project" ,
296+ args : []string {"create" , "--help" },
297+ expectedPath : "" ,
298+ expectedMethod : "" ,
299+ wantErr : false ,
300+ expectedOutput : "The name of the project" ,
301+ },
243302 }
244303
245304 for _ , tt := range tests {
246305 t .Run (tt .name , func (t * testing.T ) {
247306 a := getTestAPI ()
248- req , _ , err := ExecuteResourceCommand (a .Resources [tt .resource ], tt .args )
307+ req , output , err := ExecuteResourceCommand (a .Resources [tt .resource ], tt .args )
249308 if (err != nil ) != tt .wantErr {
250309 t .Errorf ("ExecuteCommand() error = %v, wantErr %v" , err , tt .wantErr )
251310 return
252311 }
253- if ! tt .wantErr && req == nil {
312+ if ! tt .wantErr && req == nil && tt . expectedOutput == "" {
254313 t .Error ("ExecuteCommand() returned nil request when no error expected" )
255314 }
256- if ! tt .wantErr {
315+ if tt .expectedOutput != "" {
316+ if ! strings .Contains (output , tt .expectedOutput ) {
317+ t .Errorf ("ExecuteCommand() output = %q, want to contain %q" , output , tt .expectedOutput )
318+ }
319+ }
320+ if ! tt .wantErr && req != nil {
257321 // Verify the request path matches expected pattern
258322 if req .URL .Path != tt .expectedPath {
259323 t .Errorf ("ExecuteCommand() request path = %v, want %v" , req .URL .Path , tt .expectedPath )
0 commit comments