ozon-api-client
/
core_test.go
152 строки · 4.4 Кб
1package core
2
3import (
4"encoding/json"
5"reflect"
6"testing"
7"time"
8
9"github.com/stretchr/testify/assert"
10)
11
12type DefaultStructure struct {
13EmptyField string `json:"empty_field" default:"empty_string"`
14Field string `json:"field" default:"string"`
15}
16
17type DefaultRequest struct {
18Field int `json:"field" default:"100"`
19EmptyField int `json:"empty_field" default:"14"`
20Structure DefaultStructure `json:"structure"`
21Slice []DefaultStructure `json:"slice"`
22OptionalStructure *DefaultStructure `json:"optional_structure"`
23EmptyOptionalStructure *DefaultStructure `json:"empty_optional_structure"`
24}
25
26func TestDefaultValues(t *testing.T) {
27req := &DefaultRequest{
28Field: 50,
29Structure: DefaultStructure{
30Field: "something",
31},
32Slice: []DefaultStructure{
33{
34Field: "something",
35},
36{
37Field: "something",
38},
39},
40OptionalStructure: &DefaultStructure{
41Field: "something",
42},
43}
44err := getDefaultValues(reflect.ValueOf(req))
45assert.Nil(t, err)
46
47assert.Equal(t, 50, req.Field)
48assert.Equal(t, 14, req.EmptyField)
49assert.Equal(t, "something", req.Structure.Field)
50assert.Equal(t, "empty_string", req.Structure.EmptyField)
51assert.Equal(t, "something", req.Slice[0].Field)
52assert.Equal(t, "something", req.Slice[1].Field)
53assert.Equal(t, "empty_string", req.Slice[1].EmptyField)
54assert.Equal(t, "empty_string", req.Slice[1].EmptyField)
55assert.Equal(t, "something", req.OptionalStructure.Field)
56assert.Equal(t, "empty_string", req.OptionalStructure.EmptyField)
57assert.Equal(t, (*DefaultStructure)(nil), req.EmptyOptionalStructure)
58}
59
60func TestTimeFormat(t *testing.T) {
61t.Run("Time Format Marshalling", func(t *testing.T) {
62tests := []struct {
63ft *TimeFormat
64layout string
65expectedJSON string
66diff time.Duration
67}{
68{
69ft: NewTimeFormat(time.Date(2024, 4, 30, 15, 42, 12, 55, time.FixedZone("Test Zone", 0)), ShortDateLayout),
70layout: ShortDateLayout,
71expectedJSON: `"2024-04-30"`,
72diff: time.Hour * 24,
73},
74{
75ft: NewTimeFormat(time.Date(2024, 4, 30, 0, 0, 0, 0, time.FixedZone("Test Zone", 0)), ShortDateLayout),
76layout: ShortDateLayout,
77expectedJSON: `"2024-04-30"`,
78diff: time.Hour * 24,
79},
80{
81ft: NewTimeFormat(time.Time{}, ShortDateLayout),
82layout: ShortDateLayout,
83expectedJSON: `"0001-01-01"`,
84diff: time.Hour * 24,
85},
86{
87ft: nil,
88layout: ShortDateLayout,
89expectedJSON: `null`,
90diff: time.Hour * 24,
91},
92}
93
94for _, tc := range tests {
95marshaled, err := json.Marshal(tc.ft)
96assert.Equal(t, nil, err)
97assert.Equal(t, tc.expectedJSON, string(marshaled))
98
99unmarshaled := newTimeLayout(tc.layout)
100err = json.Unmarshal(marshaled, unmarshaled)
101assert.Equal(t, nil, err)
102
103if tc.ft != nil {
104diffedTime := tc.ft.Add(-tc.diff)
105assert.Equal(t, true, diffedTime.Before(unmarshaled.Time) || diffedTime.Equal(unmarshaled.Time))
106assert.Equal(t, true, tc.ft.After(unmarshaled.Time) || tc.ft.Equal(unmarshaled.Time))
107}
108}
109})
110
111t.Run("Time Format in structure Marshalling", func(t *testing.T) {
112type test struct {
113Date *TimeFormat `json:"date"`
114}
115
116tests := []struct {
117structure *test
118layout string
119expectedJSON string
120diff time.Duration
121}{
122{
123structure: &test{Date: NewTimeFormat(time.Date(2024, 4, 30, 5, 4, 7, 20, time.FixedZone("Test Zone", 0)), ShortDateLayout)},
124layout: ShortDateLayout,
125expectedJSON: `{"date":"2024-04-30"}`,
126diff: time.Hour * 24,
127},
128{
129structure: &test{Date: nil},
130layout: ShortDateLayout,
131expectedJSON: `{"date":null}`,
132diff: time.Hour * 24,
133},
134}
135
136for _, tc := range tests {
137marshaled, err := json.Marshal(tc.structure)
138assert.Equal(t, nil, err)
139assert.Equal(t, tc.expectedJSON, string(marshaled))
140
141unmarshaled := &test{Date: newTimeLayout(tc.layout)}
142err = json.Unmarshal(marshaled, unmarshaled)
143assert.Equal(t, nil, err)
144
145if tc.structure != nil && tc.structure.Date != nil {
146diffedTime := tc.structure.Date.Add(-tc.diff)
147assert.Equal(t, true, diffedTime.Before(unmarshaled.Date.Time) || diffedTime.Equal(unmarshaled.Date.Time))
148assert.Equal(t, true, tc.structure.Date.After(unmarshaled.Date.Time) || tc.structure.Date.Equal(unmarshaled.Date.Time))
149}
150}
151})
152}
153