From 3c2715bc36ae831da58a06f67b77f8d5449ec782 Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Mon, 14 Apr 2025 14:27:55 +0300 Subject: [PATCH] set json tag in struct instead url --- client_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++---- constant/date.go | 3 ++ request.go | 16 +++++----- types.go | 4 +-- 4 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 constant/date.go diff --git a/client_test.go b/client_test.go index d3f0d38..bb6abbc 100644 --- a/client_test.go +++ b/client_test.go @@ -1,9 +1,13 @@ package retailcrm import ( + "bytes" "encoding/json" + "errors" "fmt" + "github.com/retailcrm/api-client-go/v2/constant" "github.com/stretchr/testify/require" + "io" "io/ioutil" "log" "math/rand" @@ -1880,15 +1884,18 @@ func TestClient_CorporateCustomerEdit(t *testing.T) { func TestClient_ClearCart(t *testing.T) { c := client() + tm := "2025-04-14 15:50:00" + clearedAt, err := time.Parse("2006-01-02 15:04:05", tm) + require.NoError(t, err) + site := "site_id" filter := SiteFilter{SiteBy: "id"} request := ClearCartRequest{ - ClearedAt: time.Now().String(), + ClearedAt: clearedAt.UTC().Format(constant.DateTimeWithZoneFormat), Customer: CartCustomer{ ID: 1, ExternalID: "ext_id", Site: "site", - BrowserID: "browser_id", GaClientID: "ga_client_id", }, Order: ClearCartOrder{ @@ -1898,9 +1905,40 @@ func TestClient_ClearCart(t *testing.T) { }, } + expectedJSON := `{ + "clearedAt": "2025-04-14 15:50:00+00:00", + "customer": { + "id": 1, + "externalId": "ext_id", + "site": "site", + "gaClientId": "ga_client_id" + }, + "order": { + "id": 1, + "externalId": "ext_id", + "number": "abc123" + } +}` + defer gock.Off() gock.New(crmURL). Post(fmt.Sprintf("/customer-interaction/%s/cart/clear", site)). + AddMatcher(func(request *http.Request, _ *gock.Request) (bool, error) { + body, err := io.ReadAll(request.Body) + require.NoError(t, err) + request.Body = io.NopCloser(bytes.NewBuffer(body)) + + val, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + val.Get("cart") + + if !assert.JSONEq(t, expectedJSON, val.Get("cart")) { + return false, errors.New("unequal values") + } + + return true, nil + }). MatchParam("siteBy", filter.SiteBy). Reply(200). BodyString(`{"success":true}`) @@ -1926,7 +1964,7 @@ func TestClient_SetCart(t *testing.T) { filter := SiteFilter{SiteBy: "id"} request := SetCartRequest{ ExternalID: "ext_id", - DroppedAt: time.Now().String(), + DroppedAt: time.Now().UTC().Format(constant.DateTimeWithZoneFormat), Link: "link", Customer: CartCustomer{ ID: 1, @@ -1951,6 +1989,36 @@ func TestClient_SetCart(t *testing.T) { defer gock.Off() gock.New(crmURL). Post(fmt.Sprintf("/customer-interaction/%s/cart/set", site)). + AddMatcher(func(req *http.Request, _ *gock.Request) (bool, error) { + body, err := io.ReadAll(req.Body) + require.NoError(t, err) + req.Body = io.NopCloser(bytes.NewBuffer(body)) + + val, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + cartJSON := val.Get("cart") + var cart SetCartRequest + require.NoError(t, json.Unmarshal([]byte(cartJSON), &cart)) + + equal := assert.Equal(t, "ext_id", cart.ExternalID) && + assert.NotEmpty(t, cart.DroppedAt) && + assert.Equal(t, 1, cart.Customer.ID) && + assert.Equal(t, "ext_id", cart.Customer.ExternalID) && + assert.Equal(t, "site", cart.Customer.Site) && + assert.Equal(t, "ga_client_id", cart.Customer.GaClientID) && + assert.Equal(t, float64(1), cart.Items[0].Quantity) && + assert.Equal(t, float64(1), cart.Items[0].Price) && + assert.Equal(t, 1, cart.Items[0].Offer.ID) && + assert.Equal(t, "ext_id", cart.Items[0].Offer.ExternalID) && + assert.Equal(t, "xml_id", cart.Items[0].Offer.XMLID) + + if !equal { + return false, errors.New("unequal values") + } + + return true, nil + }). MatchParam("siteBy", filter.SiteBy). Reply(200). BodyString(`{"success":true}`) @@ -1982,8 +2050,8 @@ func TestClient_GetCart(t *testing.T) { expCart := Cart{ Currency: "currency", ExternalID: "ext_id", - DroppedAt: time.Now().String(), - ClearedAt: time.Now().String(), + DroppedAt: "2025-04-14 14:32:14+03:00", + ClearedAt: "2025-04-14 14:52:14+03:00", Link: "link", Items: []CartItem{ { diff --git a/constant/date.go b/constant/date.go new file mode 100644 index 0000000..ea2f0e2 --- /dev/null +++ b/constant/date.go @@ -0,0 +1,3 @@ +package constant + +const DateTimeWithZoneFormat = "2006-01-02 15:04:05-07:00" diff --git a/request.go b/request.go index 2d69c07..96872f3 100644 --- a/request.go +++ b/request.go @@ -196,18 +196,18 @@ type DeliveryShipmentsRequest struct { // ClearCartRequest type. type ClearCartRequest struct { - ClearedAt string `url:"clearedAt,omitempty"` - Customer CartCustomer `url:"customer,omitempty"` - Order ClearCartOrder `url:"order,omitempty"` + ClearedAt string `json:"clearedAt,omitempty"` + Customer CartCustomer `json:"customer,omitempty"` + Order ClearCartOrder `json:"order,omitempty"` } // SetCartRequest type. type SetCartRequest struct { - ExternalID string `url:"externalId,omitempty"` - DroppedAt string `url:"droppedAt,omitempty"` - Link string `url:"link,omitempty"` - Customer CartCustomer `url:"customer,omitempty"` - Items []SetCartItem `url:"items,omitempty"` + ExternalID string `json:"externalId,omitempty"` + DroppedAt string `json:"droppedAt,omitempty"` + Link string `json:"link,omitempty"` + Customer CartCustomer `json:"customer,omitempty"` + Items []SetCartItem `json:"items,omitempty"` } // CostsRequest type. diff --git a/types.go b/types.go index 7649911..1ce97bc 100644 --- a/types.go +++ b/types.go @@ -411,7 +411,7 @@ type SerializedOrderLink struct { // ClearCartOrder type. type ClearCartOrder struct { ID int `json:"id,omitempty"` - ExternalID string `json:"externalID,omitempty"` + ExternalID string `json:"externalId,omitempty"` Number string `json:"number,omitempty"` } @@ -495,7 +495,7 @@ type SetCartItem struct { // SetCartOffer type. type SetCartOffer struct { ID int `json:"id,omitempty"` - ExternalID string `json:"externalID,omitempty"` + ExternalID string `json:"externalId,omitempty"` XMLID string `json:"xmlId,omitempty"` }