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

     1  /*
     2  Phrase Strings API Reference
     3  
     4  Testing JobsApiService
     5  
     6  */
     7  
     8  // Code generated by OpenAPI Generator (https://openapi-generator.tech);
     9  
    10  package phrase
    11  
    12  import (
    13  	"context"
    14  	"encoding/json"
    15  	"net/http"
    16  	"net/http/httptest"
    17  	"testing"
    18  
    19  	"github.com/phrase/phrase-go/v3" // x-release-please-major
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func Test_phrase_JobsApiService(t *testing.T) {
    25  	t.Run("Test JobsApiService JobCreate", func(t *testing.T) {
    26  		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    27  			// assert request body
    28  			var requestBody map[string]interface{}
    29  			decoder := json.NewDecoder(r.Body)
    30  			if err := decoder.Decode(&requestBody); err != nil {
    31  				t.Fatal(err)
    32  			}
    33  
    34  			// Access the values from the JSON payload
    35  			assert.Equal(t, requestBody["name"], "createjobactionb1")
    36  			assert.Equal(t, requestBody["due_date"], nil)
    37  
    38  			// Send the mock response
    39  			response := `{"id": "1", "name": "createjobactionb1", "code": "123abc", "state": "draft", "user_id": "1" }`
    40  			w.Header().Set("Content-Type", "application/json")
    41  			w.WriteHeader(http.StatusOK)
    42  
    43  			w.Write([]byte(response))
    44  		}))
    45  
    46  		defer server.Close()
    47  
    48  		configuration := phrase.NewConfiguration()
    49  		configuration.BasePath = server.URL
    50  		apiClient := phrase.NewAPIClient(configuration)
    51  
    52  		translationKeyIds := []string{"1", "2", "3"}
    53  
    54  		localVarOptionals := phrase.JobCreateOpts{}
    55  		jobCreateParams := phrase.JobCreateParameters{Name: "createjobactionb1", TranslationKeyIds: translationKeyIds}
    56  		resp, httpRes, err := apiClient.JobsApi.JobCreate(context.Background(), "project_id", jobCreateParams, &localVarOptionals)
    57  
    58  		require.Nil(t, err)
    59  		require.NotNil(t, resp)
    60  		assert.Equal(t, 200, httpRes.StatusCode)
    61  	})
    62  
    63  }