From 98579186a4e8080844738afc87202d70dceb8636 Mon Sep 17 00:00:00 2001 From: kk-no Date: Wed, 27 Dec 2023 14:40:33 +0900 Subject: [PATCH 1/3] Implements Unmarshaler and Stringer interface for HsInt --- company_test.go | 4 +++- type.go | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/company_test.go b/company_test.go index 0ee2eb2..4304eb4 100644 --- a/company_test.go +++ b/company_test.go @@ -229,7 +229,7 @@ func TestCompanyServiceOp_Get(t *testing.T) { client: hubspot.NewMockClient(&hubspot.MockConfig{ Status: http.StatusOK, Header: http.Header{}, - Body: []byte(`{"id":"company001","properties":{"city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z"},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), + Body: []byte(`{"id":"company001","properties":{"annualrevenue":"1000000","city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":""},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), }), }, args: args{ @@ -247,6 +247,7 @@ func TestCompanyServiceOp_Get(t *testing.T) { Archived: false, Properties: &CustomFields{ Company: hubspot.Company{ + Annualrevenue: hubspot.NewInt(1000000), City: hubspot.NewString("Cambridge"), Createdate: &createdAt, Domain: hubspot.NewString("biglytics.net"), @@ -255,6 +256,7 @@ func TestCompanyServiceOp_Get(t *testing.T) { Name: hubspot.NewString("Biglytics"), Phone: hubspot.NewString("(877)929-0687"), State: hubspot.NewString("Massachusetts"), + HsCreatedByUserId: hubspot.NewInt(0), }, CustomName: hubspot.NewString("biglytics"), CustomDate: &createdAt, diff --git a/type.go b/type.go index fda3d7d..c7df15b 100644 --- a/type.go +++ b/type.go @@ -2,6 +2,8 @@ package hubspot import ( "encoding/json" + "strconv" + "strings" "time" ) @@ -57,9 +59,8 @@ func NewTime(t time.Time) *HsTime { // This is because there are cases where the Time value returned by HubSpot is null or empty string. // The time.Time does not support Parse with empty string. func (ht *HsTime) UnmarshalJSON(b []byte) error { - // FIXME: Initialization is performed on empty string. if len(b) == 0 || string(b) == `""` { - return nil + return nil // NOTE: Initialization is performed on empty string. } v := &time.Time{} if err := json.Unmarshal(b, v); err != nil { @@ -100,3 +101,22 @@ func NewInt(v int) *HsInt { val := HsInt(v) return &val } + +// UnmarshalJSON implemented json.Unmarshaler. +// This is because there are cases where the Int value returned by HubSpot is "" or "12345". +func (hi *HsInt) UnmarshalJSON(b []byte) error { + if len(b) == 0 || string(b) == `""` { + return nil // NOTE: Initialization is performed on 0. + } + i, err := strconv.Atoi(strings.Replace(string(b), `"`, ``, -1)) + if err != nil { + return err + } + *hi = HsInt(i) + return nil +} + +// String implemented Stringer. +func (hi *HsInt) String() string { + return strconv.Itoa(int(*hi)) +} From f63eee7adde8a013e6eeba15986717f2a5bb0f97 Mon Sep 17 00:00:00 2001 From: kk-no Date: Thu, 28 Dec 2023 10:59:26 +0900 Subject: [PATCH 2/3] add test case of number field includes in response --- company_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/company_test.go b/company_test.go index 4304eb4..abdacec 100644 --- a/company_test.go +++ b/company_test.go @@ -229,7 +229,7 @@ func TestCompanyServiceOp_Get(t *testing.T) { client: hubspot.NewMockClient(&hubspot.MockConfig{ Status: http.StatusOK, Header: http.Header{}, - Body: []byte(`{"id":"company001","properties":{"annualrevenue":"1000000","city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":""},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), + Body: []byte(`{"id":"company001","properties":{"annualrevenue":"1000000","city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":"","twitterfollowers":1000},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), }), }, args: args{ @@ -257,6 +257,7 @@ func TestCompanyServiceOp_Get(t *testing.T) { Phone: hubspot.NewString("(877)929-0687"), State: hubspot.NewString("Massachusetts"), HsCreatedByUserId: hubspot.NewInt(0), + Twitterfollowers: hubspot.NewInt(1000), }, CustomName: hubspot.NewString("biglytics"), CustomDate: &createdAt, From 954c673d276a9432c5d27d2a6c80dd47cc82c5bf Mon Sep 17 00:00:00 2001 From: kk-no Date: Thu, 28 Dec 2023 12:06:00 +0900 Subject: [PATCH 3/3] Split test cases to check different type format in api response --- company_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/company_test.go b/company_test.go index abdacec..894f463 100644 --- a/company_test.go +++ b/company_test.go @@ -223,13 +223,58 @@ func TestCompanyServiceOp_Get(t *testing.T) { wantErr error }{ { - name: "Successfully get a company", + name: "Get a company", fields: fields{ companyPath: hubspot.ExportCompanyBasePath, client: hubspot.NewMockClient(&hubspot.MockConfig{ Status: http.StatusOK, Header: http.Header{}, - Body: []byte(`{"id":"company001","properties":{"annualrevenue":"1000000","city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":"","twitterfollowers":1000},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), + Body: []byte(`{"id":"company001","properties":{"annualrevenue":"1000000","city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":""},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), + }), + }, + args: args{ + companyID: "company001", + company: &CustomFields{}, + option: &hubspot.RequestQueryOption{ + Properties: []string{ + "custom_name", + "custom_date", + }, + }, + }, + want: &hubspot.ResponseResource{ + ID: "company001", + Archived: false, + Properties: &CustomFields{ + Company: hubspot.Company{ + Annualrevenue: hubspot.NewInt(1000000), + City: hubspot.NewString("Cambridge"), + Createdate: &createdAt, + Domain: hubspot.NewString("biglytics.net"), + HsLastmodifieddate: &updatedAt, + Industry: hubspot.NewString("Technology"), + Name: hubspot.NewString("Biglytics"), + Phone: hubspot.NewString("(877)929-0687"), + State: hubspot.NewString("Massachusetts"), + HsCreatedByUserId: hubspot.NewInt(0), + }, + CustomName: hubspot.NewString("biglytics"), + CustomDate: &createdAt, + }, + CreatedAt: &createdAt, + UpdatedAt: &updatedAt, + }, + wantErr: nil, + }, + { + name: "If the API returns a numeric value when getting a company", + fields: fields{ + companyPath: hubspot.ExportCompanyBasePath, + client: hubspot.NewMockClient(&hubspot.MockConfig{ + Status: http.StatusOK, + Header: http.Header{}, + // NOTE: Normally, numeric types in the HubSpot API are returned as strings (e.g. "", "12345"), but this is to confirm when they are returned as numbers. + Body: []byte(`{"id":"company001","properties":{"annualrevenue":1000000,"city":"Cambridge","createdate":"2019-10-30T03:30:17.883Z","domain":"biglytics.net","hs_lastmodifieddate":"2019-12-07T16:50:06.678Z","industry":"Technology","name":"Biglytics","phone":"(877)929-0687","state":"Massachusetts","custom_name":"biglytics","custom_date":"2019-10-30T03:30:17.883Z","hs_created_by_user_id":0},"createdAt":"2019-10-30T03:30:17.883Z","updatedAt":"2019-12-07T16:50:06.678Z"}`), }), }, args: args{ @@ -257,7 +302,6 @@ func TestCompanyServiceOp_Get(t *testing.T) { Phone: hubspot.NewString("(877)929-0687"), State: hubspot.NewString("Massachusetts"), HsCreatedByUserId: hubspot.NewInt(0), - Twitterfollowers: hubspot.NewInt(1000), }, CustomName: hubspot.NewString("biglytics"), CustomDate: &createdAt,