|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aep-dev/aep-lib-go/pkg/api" |
| 8 | + "github.com/aep-dev/aep-lib-go/pkg/openapi" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRequiredFieldDescription(t *testing.T) { |
| 12 | + resource := &api.Resource{ |
| 13 | + Singular: "book", |
| 14 | + Plural: "books", |
| 15 | + Schema: &openapi.Schema{ |
| 16 | + Properties: map[string]openapi.Schema{ |
| 17 | + "title": { |
| 18 | + Type: "string", |
| 19 | + Description: "The title of the book", |
| 20 | + }, |
| 21 | + "author": { |
| 22 | + Type: "string", |
| 23 | + Description: "The author of the book", |
| 24 | + }, |
| 25 | + }, |
| 26 | + Required: []string{"title"}, |
| 27 | + }, |
| 28 | + Methods: api.Methods{ |
| 29 | + Create: &api.CreateMethod{}, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + // We can use the internal function `ExecuteResourceCommand` to get the command, |
| 34 | + // but better yet, let's just inspect the flags created by `addSchemaFlags` if possible, |
| 35 | + // or run `ExecuteResourceCommand` and check the help output implicitly or by inspecting the command structure. |
| 36 | + |
| 37 | + // Since `ExecuteResourceCommand` returns a request, output, and error, |
| 38 | + // and helping is handled by cobra's execution, let's try to invoke help or inspect the command. |
| 39 | + |
| 40 | + // Actually, `ExecuteResourceCommand` creates the command internally and executes it. |
| 41 | + // It does not return the cobra command. |
| 42 | + // However, it sets the output to a strings.Builder. |
| 43 | + |
| 44 | + args := []string{"create", "--help"} |
| 45 | + _, output, _ := ExecuteResourceCommand(resource, args) |
| 46 | + |
| 47 | + if !strings.Contains(output, "The title of the book (required)") { |
| 48 | + t.Errorf("Expected description for required field 'title' to contain '(required)', but got:\n%s", output) |
| 49 | + } |
| 50 | + |
| 51 | + if strings.Contains(output, "The author of the book (required)") { |
| 52 | + t.Errorf("Expected description for optional field 'author' NOT to contain '(required)', but got:\n%s", output) |
| 53 | + } |
| 54 | +} |
0 commit comments