Skip to content

Commit cce777b

Browse files
committed
chore: fixing tests
1 parent 037b313 commit cce777b

8 files changed

Lines changed: 124 additions & 48 deletions

File tree

cmd/aepcli/main.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ import (
1515
)
1616

1717
func main() {
18-
err := aepcli()
18+
err := aepcli(os.Args[1:])
1919
if err != nil {
2020
fmt.Println(err)
2121
os.Exit(1)
2222
}
2323
os.Exit(0)
2424
}
2525

26-
func aepcli() error {
26+
func aepcli(args []string) error {
2727
var logLevel string
2828
var fileOrAlias string
29-
var resource string
3029
var additionalArgs []string
3130
var s *service.Service
3231

3332
rootCmd := &cobra.Command{
34-
Use: "aepcli",
35-
Args: cobra.ArbitraryArgs,
33+
Use: "aepcli [host or api alias] [resource or --help]",
34+
Args: cobra.MinimumNArgs(1),
3635
Run: func(cmd *cobra.Command, args []string) {
3736
fileOrAlias = args[0]
38-
resource = args[1]
39-
additionalArgs = args[2:]
37+
if len(args) > 1 {
38+
additionalArgs = args[1:]
39+
}
4040
},
4141
}
4242

@@ -46,8 +46,8 @@ func aepcli() error {
4646
rootCmd.PersistentFlags().StringArrayVar(&rawHeaders, "header", []string{}, "Specify headers in the format key=value")
4747
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)")
4848
rootCmd.PersistentFlags().StringVar(&pathPrefix, "path-prefix", "", "Specify a path prefix that is prepended to all paths in the openapi schema. This will strip them when evaluating the resource hierarchy paths.")
49-
rootCmd.MarkPersistentFlagRequired("host")
5049

50+
rootCmd.SetArgs(args)
5151
if err := rootCmd.Execute(); err != nil {
5252
return err
5353
}
@@ -58,8 +58,7 @@ func aepcli() error {
5858

5959
c, err := config.ReadConfig()
6060
if err != nil {
61-
fmt.Println(fmt.Errorf("unable to read config: %v", err))
62-
os.Exit(1)
61+
return fmt.Errorf("unable to read config: %v", err)
6362
}
6463

6564
if api, ok := c.APIs[fileOrAlias]; ok {
@@ -77,26 +76,23 @@ func aepcli() error {
7776

7877
openapi, err := openapi.FetchOpenAPI(fileOrAlias)
7978
if err != nil {
80-
fmt.Println(err)
81-
os.Exit(1)
79+
return fmt.Errorf("unable to fetch openapi: %w", err)
8280
}
8381
serviceDefinition, err := service.GetServiceDefinition(openapi, pathPrefix)
8482
if err != nil {
85-
fmt.Println(err)
86-
os.Exit(1)
83+
return fmt.Errorf("unable to get service definition: %w", err)
8784
}
8885

8986
headers, err := parseHeaders(rawHeaders)
9087
if err != nil {
91-
fmt.Println(fmt.Errorf("unable to parse headers: %w", err))
92-
os.Exit(1)
88+
return fmt.Errorf("unable to parse headers: %w", err)
9389
}
9490

9591
s = service.NewService(*serviceDefinition, headers)
9692

97-
result, err := s.ExecuteCommand(resource, additionalArgs)
93+
result, err := s.ExecuteCommand(additionalArgs)
9894
if err != nil {
99-
return err
95+
return fmt.Errorf("unable to execute command: %w", err)
10096
}
10197
fmt.Println(result)
10298
return nil

cmd/aepcli/main_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestAepcli(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
args []string
12+
wantErr bool
13+
errMsg string
14+
}{
15+
{
16+
name: "no arguments",
17+
args: []string{},
18+
wantErr: true,
19+
errMsg: "requires at least 1 arg(s), only received 0",
20+
},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
err := aepcli(tt.args)
26+
27+
if tt.wantErr {
28+
if err == nil {
29+
t.Errorf("aepcli() error = nil, wantErr %v", tt.wantErr)
30+
return
31+
}
32+
if !strings.Contains(err.Error(), tt.errMsg) {
33+
t.Errorf("aepcli() error = %v, want error containing %v", err, tt.errMsg)
34+
}
35+
return
36+
}
37+
38+
if err != nil {
39+
t.Errorf("aepcli() unexpected error = %v", err)
40+
}
41+
})
42+
}
43+
}

internal/openapi/openapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type RequestBody struct {
7373
}
7474

7575
type MediaType struct {
76-
Schema Schema `json:"schema"`
76+
Schema *Schema `json:"schema,omitempty"`
7777
}
7878

7979
type Schema struct {

internal/service/resource_definition_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ var projectResource = Resource{
3838
},
3939
Required: []string{"name"},
4040
},
41+
GetMethod: &GetMethod{},
42+
ListMethod: &ListMethod{},
43+
CreateMethod: &CreateMethod{},
44+
UpdateMethod: &UpdateMethod{},
45+
DeleteMethod: &DeleteMethod{},
4146
}
4247

4348
func TestExecuteCommand(t *testing.T) {
@@ -83,10 +88,16 @@ func TestExecuteCommand(t *testing.T) {
8388
{
8489
name: "resource with parent",
8590
resource: Resource{
86-
Singular: "dataset",
87-
Plural: "datasets",
88-
Pattern: []string{"projects", "{project}", "datasets", "{dataset}"},
89-
Parents: []*Resource{&projectResource},
91+
Singular: "dataset",
92+
Plural: "datasets",
93+
Pattern: []string{"projects", "{project}", "datasets", "{dataset}"},
94+
Parents: []*Resource{&projectResource},
95+
Schema: &openapi.Schema{},
96+
GetMethod: &GetMethod{},
97+
ListMethod: &ListMethod{},
98+
CreateMethod: &CreateMethod{},
99+
UpdateMethod: &UpdateMethod{},
100+
DeleteMethod: &DeleteMethod{},
90101
},
91102
args: []string{"--project=foo", "get", "abc"},
92103
expectedPath: "projects/foo/datasets/abc",

internal/service/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ func NewService(serviceDefinition ServiceDefinition, headers map[string]string)
2626
}
2727
}
2828

29-
func (s *Service) ExecuteCommand(resource string, args []string) (string, error) {
30-
if resource == "--help" {
29+
func (s *Service) ExecuteCommand(args []string) (string, error) {
30+
if len(args) == 0 || args[0] == "--help" {
3131
return s.ListResources(), nil
3232
}
33+
resource := args[0]
3334
r, err := s.GetResource(resource)
3435
if err != nil {
3536
return "", fmt.Errorf("%v\n%v", err, s.ListResources())

internal/service/service_definition.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func GetServiceDefinition(api *openapi.OpenAPI, pathPrefix string) (*ServiceDefi
4141
if pathItem.Get != nil {
4242
if resp, ok := pathItem.Get.Responses["200"]; ok {
4343
ct := resp.Content[contentType]
44-
sRef = &ct.Schema
44+
sRef = ct.Schema
4545
r.GetMethod = &GetMethod{}
4646
}
4747
}
4848
if pathItem.Patch != nil {
4949
if resp, ok := pathItem.Patch.Responses["200"]; ok {
5050
ct := resp.Content[contentType]
51-
sRef = &ct.Schema
51+
sRef = ct.Schema
5252
r.UpdateMethod = &UpdateMethod{}
5353
}
5454
}
@@ -58,7 +58,7 @@ func GetServiceDefinition(api *openapi.OpenAPI, pathPrefix string) (*ServiceDefi
5858
// check if there is a query parameter "id"
5959
if resp, ok := pathItem.Post.Responses["200"]; ok {
6060
ct := resp.Content[contentType]
61-
sRef = &ct.Schema
61+
sRef = ct.Schema
6262
supportsUserSettableCreate := false
6363
for _, param := range pathItem.Post.Parameters {
6464
if param.Name == "id" {
@@ -73,7 +73,7 @@ func GetServiceDefinition(api *openapi.OpenAPI, pathPrefix string) (*ServiceDefi
7373
if pathItem.Get != nil {
7474
if resp, ok := pathItem.Get.Responses["200"]; ok {
7575
ct := resp.Content[contentType]
76-
resolvedSchema, err := dereferencedSchema(ct.Schema, api)
76+
resolvedSchema, err := dereferencedSchema(*ct.Schema, api)
7777
if err != nil {
7878
return nil, fmt.Errorf("error dereferencing schema %q: %v", ct.Schema.Ref, err)
7979
}
@@ -131,7 +131,7 @@ func GetServiceDefinition(api *openapi.OpenAPI, pathPrefix string) (*ServiceDefi
131131
func (s *ServiceDefinition) GetResource(resource string) (*Resource, error) {
132132
r, ok := (*s).Resources[resource]
133133
if !ok {
134-
return nil, fmt.Errorf("Resource %s not found.", resource, (*s).Resources)
134+
return nil, fmt.Errorf("resource %q not found", resource)
135135
}
136136
return r, nil
137137
}

internal/service/service_definition_test.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestGetServiceDefinition2(t *testing.T) {
10+
func TestGetServiceDefinition(t *testing.T) {
1111
tests := []struct {
1212
name string
1313
api *openapi.OpenAPI
@@ -23,12 +23,16 @@ func TestGetServiceDefinition2(t *testing.T) {
2323
Get: &openapi.Operation{
2424
Responses: map[string]openapi.Response{
2525
"200": {
26-
Schema: &openapi.Schema{
27-
Properties: map[string]openapi.Schema{
28-
"results": {
29-
Type: "array",
30-
Items: &openapi.Schema{
31-
Ref: "#/components/schemas/Widget",
26+
Content: map[string]openapi.MediaType{
27+
"application/json": {
28+
Schema: &openapi.Schema{
29+
Properties: map[string]openapi.Schema{
30+
"results": {
31+
Type: "array",
32+
Items: &openapi.Schema{
33+
Ref: "#/components/schemas/Widget",
34+
},
35+
},
3236
},
3337
},
3438
},
@@ -39,8 +43,12 @@ func TestGetServiceDefinition2(t *testing.T) {
3943
Post: &openapi.Operation{
4044
Responses: map[string]openapi.Response{
4145
"200": {
42-
Schema: &openapi.Schema{
43-
Ref: "#/components/schemas/Widget",
46+
Content: map[string]openapi.MediaType{
47+
"application/json": {
48+
Schema: &openapi.Schema{
49+
Ref: "#/components/schemas/Widget",
50+
},
51+
},
4452
},
4553
},
4654
},
@@ -50,8 +58,12 @@ func TestGetServiceDefinition2(t *testing.T) {
5058
Get: &openapi.Operation{
5159
Responses: map[string]openapi.Response{
5260
"200": {
53-
Schema: &openapi.Schema{
54-
Ref: "#/components/schemas/Widget",
61+
Content: map[string]openapi.MediaType{
62+
"application/json": {
63+
Schema: &openapi.Schema{
64+
Ref: "#/components/schemas/Widget",
65+
},
66+
},
5567
},
5668
},
5769
},
@@ -60,8 +72,12 @@ func TestGetServiceDefinition2(t *testing.T) {
6072
Patch: &openapi.Operation{
6173
Responses: map[string]openapi.Response{
6274
"200": {
63-
Schema: &openapi.Schema{
64-
Ref: "#/components/schemas/Widget",
75+
Content: map[string]openapi.MediaType{
76+
"application/json": {
77+
Schema: &openapi.Schema{
78+
Ref: "#/components/schemas/Widget",
79+
},
80+
},
6581
},
6682
},
6783
},
@@ -103,8 +119,12 @@ func TestGetServiceDefinition2(t *testing.T) {
103119
Get: &openapi.Operation{
104120
Responses: map[string]openapi.Response{
105121
"200": {
106-
Schema: &openapi.Schema{
107-
Ref: "#/components/schemas/widget",
122+
Content: map[string]openapi.MediaType{
123+
"application/json": {
124+
Schema: &openapi.Schema{
125+
Ref: "#/components/schemas/widget",
126+
},
127+
},
108128
},
109129
},
110130
},
@@ -155,8 +175,12 @@ func TestGetServiceDefinition2(t *testing.T) {
155175
},
156176
Responses: map[string]openapi.Response{
157177
"200": {
158-
Schema: &openapi.Schema{
159-
Ref: "#/components/schemas/Widget",
178+
Content: map[string]openapi.MediaType{
179+
"application/json": {
180+
Schema: &openapi.Schema{
181+
Ref: "#/components/schemas/Widget",
182+
},
183+
},
160184
},
161185
},
162186
},
@@ -182,7 +206,7 @@ func TestGetServiceDefinition2(t *testing.T) {
182206

183207
for _, tt := range tests {
184208
t.Run(tt.name, func(t *testing.T) {
185-
result, err := GetServiceDefinition(tt.api)
209+
result, err := GetServiceDefinition(tt.api, "")
186210

187211
if tt.expectedError != "" {
188212
assert.Error(t, err)

internal/service/service_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package service

0 commit comments

Comments
 (0)