mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-22 04:46:03 +03:00
Neur0toxine
97b1abf470
* refactor library and upgrade version * remove useless environment variable * remove unsupported versions from go.mod * fixes for error handling & data types * different improvements for errors * fixes for types and tests * better coverage, fix error with the unmarshalers
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package retailcrm
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTag_MarshalJSON(t *testing.T) {
|
|
tags := []Tag{
|
|
{"first", "#3e89b6", false},
|
|
{"second", "#ffa654", false},
|
|
}
|
|
names := []byte(`["first","second"]`)
|
|
str, err := json.Marshal(tags)
|
|
|
|
if err != nil {
|
|
t.Errorf("%v", err.Error())
|
|
}
|
|
|
|
if !reflect.DeepEqual(str, names) {
|
|
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})
|
|
}
|