Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
janelletavares committed Apr 3, 2023
1 parent 56639a6 commit d92b67d
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cmd/meroxa/root/resources/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/global"
Expand Down Expand Up @@ -358,3 +360,64 @@ Sign up for the Beta here: https://share.hsforms.com/1Uq6UYoL8Q6eV5QzSiyIQkAc2sm
t.Fatalf("expected output:\n%s\ngot:\n%s", wantError, gotError)
}
}

func TestCreateResourceURLFlag(t *testing.T) {
tests := []struct {
description string
resourceType string
client func(*gomock.Controller) *mock.MockClient
wantErr error
}{
{
description: "Do not require URL for Notion",
resourceType: string(meroxa.ResourceTypeNotion),
client: func(ctrl *gomock.Controller) *mock.MockClient {
client := mock.NewMockClient(ctrl)
client.EXPECT().CreateResource(gomock.Any(), gomock.Any()).Return(&meroxa.Resource{}, nil).Times(1)
return client
},
},
{
description: "Do not require URL for Spire Maritime AIS",
resourceType: string(meroxa.ResourceTypeSpireMaritimeAIS),
client: func(ctrl *gomock.Controller) *mock.MockClient {
client := mock.NewMockClient(ctrl)
client.EXPECT().CreateResource(gomock.Any(), gomock.Any()).Return(&meroxa.Resource{}, nil).Times(1)
return client
},
},
{
description: "Require URL for one of the rest of the types",
resourceType: string(meroxa.ResourceTypePostgres),
client: func(ctrl *gomock.Controller) *mock.MockClient {
client := mock.NewMockClient(ctrl)
return client
},
wantErr: fmt.Errorf(`required flag(s) "url" not set`),
},
}

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
logger := log.NewTestLogger()
c := &Create{
client: tc.client(ctrl),
logger: logger,
}
c.args.Name = "my-resource"
c.flags.Type = tc.resourceType

err := c.Execute(ctx)
if err != nil {
if tc.wantErr == nil {
t.Fatalf("unexpected error: %v", err)
}
assert.Equal(t, tc.wantErr.Error(), err.Error())
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit d92b67d

Please sign in to comment.