github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/clients/go/test/api_uploads_test.go (about)

     1  /*
     2  Phrase Strings API Reference
     3  
     4  Testing UploadsApiService
     5  
     6  */
     7  
     8  // Code generated by OpenAPI Generator (https://openapi-generator.tech);
     9  
    10  package phrase
    11  
    12  import (
    13  	"context"
    14  	"net/http"
    15  	"net/http/httptest"
    16  	"os"
    17  	"testing"
    18  
    19  	"github.com/antihax/optional"
    20  	"github.com/phrase/phrase-go/v3"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func Test_phrase_UploadsApiService(t *testing.T) {
    26  	t.Run("Test UploadsApiService UploadCreate", func(t *testing.T) {
    27  		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    28  			// assert request body for proper serialization of tested format_options
    29  			r.ParseMultipartForm(0)
    30  			assert.Equal(t, r.FormValue("format_options[translation_columns][en]"), "f")
    31  			assert.Equal(t, r.FormValue("format_options[translation_columns][de]"), "e")
    32  			assert.Equal(t, r.FormValue("format_options[top_level_key]"), "100")
    33  
    34  			// Send the mock response
    35  			response := `{"id": "1", "filename": "test.json", "format": "json", "state": "valid" }`
    36  			w.Header().Set("Content-Type", "application/json")
    37  			w.WriteHeader(http.StatusOK)
    38  
    39  			w.Write([]byte(response))
    40  		}))
    41  
    42  		defer server.Close()
    43  
    44  		configuration := phrase.NewConfiguration()
    45  		configuration.BasePath = server.URL
    46  		apiClient := phrase.NewAPIClient(configuration)
    47  
    48  		file, _ := os.Create("testfile.json")
    49  		fileFormat := "json"
    50  		localeId := "99"
    51  
    52  		// setting format_options
    53  		formatOptions := make(map[string]interface{})
    54  		nestedOptions := make(map[interface{}]interface{})
    55  
    56  		nestedOptions["en"] = "f"
    57  		nestedOptions["de"] = "e"
    58  
    59  		formatOptions["top_level_key"] = 100
    60  		formatOptions["translation_columns"] = nestedOptions
    61  
    62  		formatOptionsMap := optional.NewInterface(formatOptions)
    63  
    64  		localVarOptionals := phrase.UploadCreateOpts{FormatOptions: formatOptionsMap}
    65  		resp, httpRes, err := apiClient.UploadsApi.UploadCreate(context.Background(), "project_id", file, fileFormat, localeId, &localVarOptionals)
    66  		requestUrl := httpRes.Request.URL
    67  
    68  		require.Nil(t, err)
    69  		require.NotNil(t, resp)
    70  		assert.Equal(t, 200, httpRes.StatusCode)
    71  		assert.Equal(t, "1", resp.Id)
    72  		assert.Equal(t, "test.json", resp.Filename)
    73  		assert.Equal(t, "json", resp.Format)
    74  		assert.Equal(t, "/projects/project_id/uploads", requestUrl.Path)
    75  		assert.Equal(t, "POST", httpRes.Request.Method)
    76  
    77  		defer os.Remove("testfile.json")
    78  	})
    79  
    80  	t.Run("Test UploadsApiService UploadShow", func(t *testing.T) {
    81  		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    82  			// Send the mock response
    83  			response := `{"id": "1", "filename": "test.json", "format": "json", "state": "valid" }`
    84  			w.Header().Set("Content-Type", "application/json")
    85  			w.WriteHeader(http.StatusOK)
    86  
    87  			w.Write([]byte(response))
    88  		}))
    89  
    90  		defer server.Close()
    91  
    92  		configuration := phrase.NewConfiguration()
    93  		configuration.BasePath = server.URL
    94  		apiClient := phrase.NewAPIClient(configuration)
    95  
    96  		localVarOptionals := phrase.UploadShowOpts{}
    97  		resp, httpRes, err := apiClient.UploadsApi.UploadShow(context.Background(), "project_id", "upload_id", &localVarOptionals)
    98  		requestUrl := httpRes.Request.URL
    99  
   100  		require.Nil(t, err)
   101  		require.NotNil(t, resp)
   102  		assert.Equal(t, 200, httpRes.StatusCode)
   103  		assert.Equal(t, "1", resp.Id)
   104  		assert.Equal(t, "test.json", resp.Filename)
   105  		assert.Equal(t, "json", resp.Format)
   106  		assert.Equal(t, "/projects/project_id/uploads/upload_id", requestUrl.Path)
   107  		assert.Equal(t, "GET", httpRes.Request.Method)
   108  	})
   109  
   110  }