From 7f4f16f25be389d37440daef0573e2f3318286b7 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 27 Oct 2021 14:04:57 +0300 Subject: [PATCH] better coverage, fix error with the unmarshalers --- log_test.go | 24 ++++++++++++++++++++++++ marshaling.go | 23 +++++++++++++++++++++-- marshaling_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 log_test.go diff --git a/log_test.go b/log_test.go new file mode 100644 index 0000000..8697dc9 --- /dev/null +++ b/log_test.go @@ -0,0 +1,24 @@ +package retailcrm + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +type wrappedLogger struct { + lastMessage string +} + +func (w *wrappedLogger) Debugf(msg string, v ...interface{}) { + w.lastMessage = fmt.Sprintf(msg, v...) +} + +func TestDebugLoggerAdapter_Printf(t *testing.T) { + wrapped := &wrappedLogger{} + logger := DebugLoggerAdapter(wrapped) + logger.Printf("Test message #%d", 1) + + assert.Equal(t, "Test message #1", wrapped.lastMessage) +} diff --git a/marshaling.go b/marshaling.go index 869742e..06841e2 100644 --- a/marshaling.go +++ b/marshaling.go @@ -69,15 +69,34 @@ func (p *OrderPayments) UnmarshalJSON(data []byte) error { case map[string]interface{}: m = make(OrderPayments, len(e)) for idx, val := range e { - m[idx] = val.(OrderPayment) + var res OrderPayment + err := unmarshalMap(val.(map[string]interface{}), &res) + if err != nil { + return err + } + m[idx] = res } case []interface{}: m = make(OrderPayments, len(e)) for idx, val := range e { - m[strconv.Itoa(idx)] = val.(OrderPayment) + var res OrderPayment + err := unmarshalMap(val.(map[string]interface{}), &res) + if err != nil { + return err + } + m[strconv.Itoa(idx)] = res } } *p = m return nil } + +func unmarshalMap(m map[string]interface{}, v interface{}) (err error) { + var data []byte + data, err = json.Marshal(m) + if err != nil { + return err + } + return json.Unmarshal(data, v) +} diff --git a/marshaling_test.go b/marshaling_test.go index 6a50e7f..256236e 100644 --- a/marshaling_test.go +++ b/marshaling_test.go @@ -4,6 +4,9 @@ import ( "encoding/json" "reflect" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTag_MarshalJSON(t *testing.T) { @@ -22,3 +25,45 @@ func TestTag_MarshalJSON(t *testing.T) { t.Errorf("Marshaled: %#v\nExpected: %#v\n", str, names) } } + +func TestAPIErrorsList_UnmarshalJSON(t *testing.T) { + var list APIErrorsList + + require.NoError(t, json.Unmarshal([]byte(`["first", "second"]`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["0"], "first") + assert.Equal(t, list["1"], "second") + + require.NoError(t, json.Unmarshal([]byte(`{"a": "first", "b": "second"}`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["a"], "first") + assert.Equal(t, list["b"], "second") +} + +func TestCustomFieldsList_UnmarshalJSON(t *testing.T) { + var list CustomFieldsList + + require.NoError(t, json.Unmarshal([]byte(`["first", "second"]`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["0"], "first") + assert.Equal(t, list["1"], "second") + + require.NoError(t, json.Unmarshal([]byte(`{"a": "first", "b": "second"}`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["a"], "first") + assert.Equal(t, list["b"], "second") +} + +func TestOrderPayments_UnmarshalJSON(t *testing.T) { + var list OrderPayments + + require.NoError(t, json.Unmarshal([]byte(`[{"id": 1}, {"id": 2}]`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["0"], OrderPayment{ID: 1}) + assert.Equal(t, list["1"], OrderPayment{ID: 2}) + + require.NoError(t, json.Unmarshal([]byte(`{"a": {"id": 1}, "b": {"id": 2}}`), &list)) + assert.Len(t, list, 2) + assert.Equal(t, list["a"], OrderPayment{ID: 1}) + assert.Equal(t, list["b"], OrderPayment{ID: 2}) +}