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

     1  package phrase
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/phrase/phrase-go/v3" // x-release-please-major
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func Test_phrase_CustomMetadataApiService(t *testing.T) {
    16  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    17  		// Test request parameters
    18  		b, err := io.ReadAll(r.Body)
    19  		if err != nil {
    20  			panic(err)
    21  		}
    22  		assert.Equal(t, "{\"name\":\"my_property\",\"data_type\":\"string\",\"project_ids\":[\"project_id\",\"project_id2\"],\"description\":\"my description\"}\n", string(b))
    23  
    24  		// Send the mock response
    25  		w.Header().Set("Content-Type", "application/json")
    26  		w.WriteHeader(http.StatusOK)
    27  
    28  		response := `{"id": "1", "data_type": "string", "name": "my_property" }`
    29  		w.Write([]byte(response))
    30  	}))
    31  
    32  	defer server.Close()
    33  
    34  	configuration := phrase.NewConfiguration()
    35  	configuration.BasePath = server.URL
    36  	apiClient := phrase.NewAPIClient(configuration)
    37  
    38  	t.Run("Test CustomMetadataService CustomMetadataCreate", func(t *testing.T) {
    39  		customMetadataPropertiesCreateParameters := phrase.CustomMetadataPropertiesCreateParameters{
    40  			Name:        "my_property",
    41  			DataType:    phrase.STRING,
    42  			Description: "my description",
    43  			ProjectIds:  []string{"project_id", "project_id2"},
    44  		}
    45  		localVarOptionals := phrase.CustomMetadataPropertyCreateOpts{}
    46  		resp, httpRes, err := apiClient.CustomMetadataApi.CustomMetadataPropertyCreate(context.Background(), "account_id", customMetadataPropertiesCreateParameters, &localVarOptionals)
    47  		requestUrl := httpRes.Request.URL
    48  
    49  		require.Nil(t, err)
    50  		require.NotNil(t, resp)
    51  		assert.Equal(t, 200, httpRes.StatusCode)
    52  		assert.Equal(t, "1", resp.Id)
    53  		assert.Equal(t, "my_property", resp.Name)
    54  		assert.Equal(t, phrase.STRING, resp.DataType)
    55  		assert.Equal(t, "/accounts/account_id/custom_metadata/properties", requestUrl.Path)
    56  		assert.Equal(t, "", requestUrl.RawQuery)
    57  		assert.Equal(t, "POST", httpRes.Request.Method)
    58  	})
    59  
    60  }