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..caba4db94f2 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/file/notification/service/get_exception_test.go @@ -0,0 +1,78 @@ +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", + }, + 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..a1f4b8599ae --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/file/service/get_file_test.go @@ -0,0 +1,107 @@ +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: map[string]string{ + "X-File-API-Version": "0.0.2", + }, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "filename": "file.txt", + }, + Auth: false, + Token: "", + }, + 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: map[string]string{ + "X-File-API-Version": "0.0.2", + }, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "filename": "file.txt", + }, + Auth: false, + Token: "", + }, + 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), + ) + + response, err := client.GetFile( + context.TODO(), + tt.request.PathParameters["filename"], + &file.GetFileRequest{ + XFileApiVersion: tt.request.Headers["XFileApiVersion"], + }, + ) + + 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/health/service/check_test.go b/seed/go-sdk/examples/no-custom-config/health/service/check_test.go new file mode 100644 index 00000000000..2d88034c600 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/health/service/check_test.go @@ -0,0 +1,85 @@ +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 TestCheck(t *testing.T) { + tests := []struct { + desc string + request *mockwire.Request + response *mockwire.Response + wantError string + }{ + { + desc: "get exception succeded", + request: &mockwire.Request{ + URL: "/check/{id}", + Method: http.MethodGet, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "id": "id-2sdx82h", + }, + Auth: true, + Token: "a-token", + }, + response: nil, + wantError: "", + }, + { + desc: "get exception with error", + request: &mockwire.Request{ + URL: "/check/{id}", + Method: http.MethodGet, + Headers: nil, + BodyProperties: nil, + QueryParameters: nil, + PathParameters: map[string]string{ + "id": "id-2sdx82h", + }, + Auth: true, + Token: "a-token", + }, + response: nil, + wantError: "an-error", + }, + } + + 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), + ) + + err = client.Check( + context.TODO(), + tt.request.PathParameters["id"], + ) + + if tt.wantError != "" { + assert.EqualError(t, err, tt.wantError) + return + } + require.NoError(t, err) + }) + } +} 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..424833da1c9 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/mockwire/types.go @@ -0,0 +1,28 @@ +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 +} + +type TestCase struct { + Request *Request + Response *Response +} + +type Stub struct { + URL string + 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..97b094745d2 --- /dev/null +++ b/seed/go-sdk/examples/no-custom-config/mockwire/wrapper.go @@ -0,0 +1,16 @@ +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: "", + 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..bfc1847884b --- /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": 8, + "tag": "tag-wf9as23d", + "metadata": map[string]interface{}{ + "actors": []interface{}{ + "Christian Bale", + }, + "releaseDate": "2023-12-08", + "ratings": map[string]interface{}{ + "rottenTomatoes": 97, + "imdb": 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) + }) + } +}