From f917b302b082237d6f109196ed5acbdf4289b130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Ckimberlytangha=E2=80=9D?= <“kimberlytangha@gmail.com”> Date: Thu, 14 Nov 2024 17:51:09 -0500 Subject: [PATCH] WIP --- .../service/get_exception_test.go | 79 ++++++++++++ .../file/service/get_file_test.go | 116 ++++++++++++++++++ .../no-custom-config/mockwire/types.go | 34 +++++ .../no-custom-config/mockwire/wrapper.go | 17 +++ .../service/create_movie_test.go | 115 +++++++++++++++++ .../service/get_movie_test.go | 87 +++++++++++++ 6 files changed, 448 insertions(+) create mode 100644 seed/go-sdk/examples/no-custom-config/file/notification/service/get_exception_test.go create mode 100644 seed/go-sdk/examples/no-custom-config/file/service/get_file_test.go create mode 100644 seed/go-sdk/examples/no-custom-config/mockwire/types.go create mode 100644 seed/go-sdk/examples/no-custom-config/mockwire/wrapper.go create mode 100644 seed/go-sdk/examples/no-custom-config/service/create_movie_test.go create mode 100644 seed/go-sdk/examples/no-custom-config/service/get_movie_test.go diff --git a/seed/go-sdk/examples/no-custom-config/file/notification/service/get_exception_test.go b/seed/go-sdk/examples/no-custom-config/file/notification/service/get_exception_test.go new file mode 100644 index 00000000000..170b34592ba --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/file/notification/service/get_exception_test.go @@ -0,0 +1,79 @@ +package service + +import ( + context "context" + "net/http" + testing "testing" + + fern "github.com/examples/fern" + mockwire "github.com/examples/fern/mockwire" + option "github.com/examples/fern/option" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetException(t *testing.T) { + tests := []struct { + desc string + request *mockwire.Request + response *mockwire.Response + wantError string + }{ + { + desc: "get exception succeded", + request: &mockwire.Request{ + URL: "/file/notification/{notificationId}", + Method: http.MethodGet, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "notificationId": "notification-hsy129x", + }, + Auth: true, + Token: "a-token", + Fields: nil, + }, + response: &mockwire.Response{ + Body: &fern.Exception{ + Type: "test", + Generic: &fern.ExceptionInfo{}, + Timeout: nil, + }, + Status: http.StatusOK, + }, + wantError: "", + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + stub, err := mockwire.Run(&mockwire.TestCase{ + Request: tt.request, + Response: tt.response, + }) + require.NoError(t, err) + defer stub.Shutdown() + + client := NewClient( + option.WithToken( + tt.request.Token, + ), + option.WithBaseURL(stub.URL), + ) + + response, err := client.GetException( + context.TODO(), + tt.request.PathParameters["notificationId"], + ) + + if tt.wantError != "" { + assert.EqualError(t, err, tt.wantError) + return + } + require.NoError(t, err) + + assert.Equal(t, tt.response.Body, response) + }) + } +} diff --git a/seed/go-sdk/examples/no-custom-config/file/service/get_file_test.go b/seed/go-sdk/examples/no-custom-config/file/service/get_file_test.go new file mode 100644 index 00000000000..efc4c563558 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/file/service/get_file_test.go @@ -0,0 +1,116 @@ +package service + +import ( + context "context" + "net/http" + testing "testing" + + fern "github.com/examples/fern" + file "github.com/examples/fern/file" + mockwire "github.com/examples/fern/mockwire" + option "github.com/examples/fern/option" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetFile(t *testing.T) { + tests := []struct { + desc string + request *mockwire.Request + response *mockwire.Response + wantError string + }{ + { + desc: "get file succeded", + request: &mockwire.Request{ + URL: "/file/{filename}", + Method: http.MethodPost, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "filename": "file.txt", + }, + Auth: false, + Token: "", + Fields: map[string]interface{}{ + "request": &file.GetFileRequest{ + XFileApiVersion: "a-value", + }, + }, + }, + response: &mockwire.Response{ + Body: map[string]interface{}{ + "file": &fern.File{ + Name: "name", + Contents: "contents", + }, + }, + Status: http.StatusOK, + }, + wantError: "", + }, + { + desc: "get file with error", + request: &mockwire.Request{ + URL: "/file/{filename}", + Method: http.MethodPost, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "filename": "file.txt", + }, + Auth: false, + Token: "", + Fields: map[string]interface{}{ + "request": &file.GetFileRequest{ + XFileApiVersion: "a-value", + }, + }, + }, + response: &mockwire.Response{ + Body: nil, + Status: http.StatusOK, + }, + wantError: "an error should be returned", + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + stub, err := mockwire.Run(&mockwire.TestCase{ + Request: tt.request, + Response: tt.response, + }) + require.NoError(t, err) + defer stub.Shutdown() + + client := NewClient( + // option.WithToken( + // tt.request.Token, + // ), + option.WithBaseURL(stub.URL), + ) + + fileRequest, ok := tt.request.Fields["request"].(*file.GetFileRequest) + if !ok { + t.Fatal("unable to retrieve field") + } + + response, err := client.GetFile( + context.TODO(), + tt.request.PathParameters["filename"], + fileRequest, + ) + + if tt.wantError != "" { + assert.EqualError(t, err, tt.wantError) + return + } + require.NoError(t, err) + + assert.Equal(t, tt.response.Body, response) + }) + } +} diff --git a/seed/go-sdk/examples/no-custom-config/mockwire/types.go b/seed/go-sdk/examples/no-custom-config/mockwire/types.go new file mode 100644 index 00000000000..3d4114b4d20 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/mockwire/types.go @@ -0,0 +1,34 @@ +package mockwire + +type Request struct { + URL string + Method string + Headers map[string]string + BodyProperties map[string]interface{} + QueryParameters map[string]string + PathParameters map[string]string + Auth bool + Token string + + Fields map[string]interface{} +} + +type Response struct { + Body interface{} + Status int64 + Error interface{} +} + +type TestCase struct { + Request *Request + Response *Response +} + +type StubConfig struct{} + +type Stub struct { + URL string + Config StubConfig + + Shutdown func() +} diff --git a/seed/go-sdk/examples/no-custom-config/mockwire/wrapper.go b/seed/go-sdk/examples/no-custom-config/mockwire/wrapper.go new file mode 100644 index 00000000000..92618935cf2 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/mockwire/wrapper.go @@ -0,0 +1,17 @@ +package mockwire + +// Run starts a local server to test against +func Run(testCase *TestCase) (*Stub, error) { + return testCase.init() +} + +func (e *TestCase) init() (*Stub, error) { + + // implementation + + return &Stub{ + URL: "", + Config: StubConfig{}, + Shutdown: func() {}, + }, nil +} diff --git a/seed/go-sdk/examples/no-custom-config/service/create_movie_test.go b/seed/go-sdk/examples/no-custom-config/service/create_movie_test.go new file mode 100644 index 00000000000..fd2cf87d707 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/service/create_movie_test.go @@ -0,0 +1,115 @@ +package service + +import ( + context "context" + "net/http" + testing "testing" + + fern "github.com/examples/fern" + mockwire "github.com/examples/fern/mockwire" + option "github.com/examples/fern/option" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCreateMovie(t *testing.T) { + tests := []struct { + desc string + request *mockwire.Request + response *mockwire.Response + wantError string + }{ + { + desc: "create movie succeded", + request: &mockwire.Request{ + URL: "/movie", + Method: http.MethodPost, + Headers: nil, + BodyProperties: map[string]interface{}{ + "id": "movie-c06a4ad7", + "prequel": "movie-cv9b914f", + "title": "The Boy and the Heron", + "rating": 8, + "tag": "tag-wf9as23d", + "metadata": map[string]interface{}{ + "actors": []interface{}{ + "Christian Bale", + "Florence Pugh", + "Willem Dafoe", + }, + "releaseDate": "2023-12-08", + "ratings": map[string]interface{}{ + "rottenTomatoes": 97, + "imdb": 7.6, + }, + }, + "revenue": 1000000, + }, + QueryParameters: nil, + PathParameters: nil, + Auth: false, + Token: "", + }, + response: &mockwire.Response{ + Body: map[string]interface{}{ + "id": "movie-c06a4ad7", + }, + Status: http.StatusOK, + }, + wantError: "", + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + stub, err := mockwire.Run(&mockwire.TestCase{ + Request: tt.request, + Response: tt.response, + }) + require.NoError(t, err) + defer stub.Shutdown() + + client := NewClient( + // option.WithToken( + // tt.request.Token, + // ), + option.WithBaseURL(stub.URL), + ) + + response, err := client.CreateMovie( + context.TODO(), + &fern.Movie{ + Id: "movie-c06a4ad7", + Prequel: fern.String( + "movie-cv9b914f", + ), + Title: "The Boy and the Heron", + From: "Hayao Miyazaki", + Rating: 8, + Tag: "tag-wf9as23d", + Metadata: map[string]interface{}{ + "actors": []interface{}{ + "Christian Bale", + "Florence Pugh", + "Willem Dafoe", + }, + "releaseDate": "2023-12-08", + "ratings": map[string]interface{}{ + "rottenTomatoes": 97, + "imdb": 7.6, + }, + }, + Revenue: 1000000, + }, + ) + + if tt.wantError != "" { + assert.EqualError(t, err, tt.wantError) + return + } + require.NoError(t, err) + + assert.Equal(t, tt.response.Body, response) + }) + } +} diff --git a/seed/go-sdk/examples/no-custom-config/service/get_movie_test.go b/seed/go-sdk/examples/no-custom-config/service/get_movie_test.go new file mode 100644 index 00000000000..2030b4d1d93 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/service/get_movie_test.go @@ -0,0 +1,87 @@ +package service + +import ( + context "context" + "net/http" + testing "testing" + + mockwire "github.com/examples/fern/mockwire" + option "github.com/examples/fern/option" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetMovie(t *testing.T) { + tests := []struct { + desc string + request *mockwire.Request + response *mockwire.Response + wantError string + }{ + { + desc: "get movie succeded", + request: &mockwire.Request{ + URL: "/movie/{movieId}", + Method: http.MethodGet, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "movieId": "movie-c06a4ad7", + }, + Auth: false, + Token: "", + }, + response: &mockwire.Response{ + Body: map[string]interface{}{ + "id": "movie-c06a4ad7", + "rating": float64(8), + "tag": "tag-wf9as23d", + "metadata": map[string]interface{}{ + "actors": []interface{}{ + "Christian Bale", + }, + "releaseDate": "2023-12-08", + "ratings": map[string]interface{}{ + "rottenTomatoes": float64(97), + "imdb": float64(7.6), + }, + }, + }, + Status: http.StatusOK, + }, + wantError: "", + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + stub, err := mockwire.Run(&mockwire.TestCase{ + Request: tt.request, + Response: tt.response, + }) + require.NoError(t, err) + defer stub.Shutdown() + + client := NewClient( + // option.WithToken( + // tt.request.Token, + // ), + option.WithBaseURL(stub.URL), + ) + + response, err := client.GetMovie( + context.TODO(), + tt.request.PathParameters["movieId"], + ) + + if tt.wantError != "" { + assert.EqualError(t, err, tt.wantError) + return + } + require.NoError(t, err) + + assert.Equal(t, tt.response.Body, response) + }) + } +}