ozon-api-client

Форк
0
/
core_test.go 
152 строки · 4.4 Кб
1
package core
2

3
import (
4
	"encoding/json"
5
	"reflect"
6
	"testing"
7
	"time"
8

9
	"github.com/stretchr/testify/assert"
10
)
11

12
type DefaultStructure struct {
13
	EmptyField string `json:"empty_field" default:"empty_string"`
14
	Field      string `json:"field" default:"string"`
15
}
16

17
type DefaultRequest struct {
18
	Field                  int                `json:"field" default:"100"`
19
	EmptyField             int                `json:"empty_field" default:"14"`
20
	Structure              DefaultStructure   `json:"structure"`
21
	Slice                  []DefaultStructure `json:"slice"`
22
	OptionalStructure      *DefaultStructure  `json:"optional_structure"`
23
	EmptyOptionalStructure *DefaultStructure  `json:"empty_optional_structure"`
24
}
25

26
func TestDefaultValues(t *testing.T) {
27
	req := &DefaultRequest{
28
		Field: 50,
29
		Structure: DefaultStructure{
30
			Field: "something",
31
		},
32
		Slice: []DefaultStructure{
33
			{
34
				Field: "something",
35
			},
36
			{
37
				Field: "something",
38
			},
39
		},
40
		OptionalStructure: &DefaultStructure{
41
			Field: "something",
42
		},
43
	}
44
	err := getDefaultValues(reflect.ValueOf(req))
45
	assert.Nil(t, err)
46

47
	assert.Equal(t, 50, req.Field)
48
	assert.Equal(t, 14, req.EmptyField)
49
	assert.Equal(t, "something", req.Structure.Field)
50
	assert.Equal(t, "empty_string", req.Structure.EmptyField)
51
	assert.Equal(t, "something", req.Slice[0].Field)
52
	assert.Equal(t, "something", req.Slice[1].Field)
53
	assert.Equal(t, "empty_string", req.Slice[1].EmptyField)
54
	assert.Equal(t, "empty_string", req.Slice[1].EmptyField)
55
	assert.Equal(t, "something", req.OptionalStructure.Field)
56
	assert.Equal(t, "empty_string", req.OptionalStructure.EmptyField)
57
	assert.Equal(t, (*DefaultStructure)(nil), req.EmptyOptionalStructure)
58
}
59

60
func TestTimeFormat(t *testing.T) {
61
	t.Run("Time Format Marshalling", func(t *testing.T) {
62
		tests := []struct {
63
			ft           *TimeFormat
64
			layout       string
65
			expectedJSON string
66
			diff         time.Duration
67
		}{
68
			{
69
				ft:           NewTimeFormat(time.Date(2024, 4, 30, 15, 42, 12, 55, time.FixedZone("Test Zone", 0)), ShortDateLayout),
70
				layout:       ShortDateLayout,
71
				expectedJSON: `"2024-04-30"`,
72
				diff:         time.Hour * 24,
73
			},
74
			{
75
				ft:           NewTimeFormat(time.Date(2024, 4, 30, 0, 0, 0, 0, time.FixedZone("Test Zone", 0)), ShortDateLayout),
76
				layout:       ShortDateLayout,
77
				expectedJSON: `"2024-04-30"`,
78
				diff:         time.Hour * 24,
79
			},
80
			{
81
				ft:           NewTimeFormat(time.Time{}, ShortDateLayout),
82
				layout:       ShortDateLayout,
83
				expectedJSON: `"0001-01-01"`,
84
				diff:         time.Hour * 24,
85
			},
86
			{
87
				ft:           nil,
88
				layout:       ShortDateLayout,
89
				expectedJSON: `null`,
90
				diff:         time.Hour * 24,
91
			},
92
		}
93

94
		for _, tc := range tests {
95
			marshaled, err := json.Marshal(tc.ft)
96
			assert.Equal(t, nil, err)
97
			assert.Equal(t, tc.expectedJSON, string(marshaled))
98

99
			unmarshaled := newTimeLayout(tc.layout)
100
			err = json.Unmarshal(marshaled, unmarshaled)
101
			assert.Equal(t, nil, err)
102

103
			if tc.ft != nil {
104
				diffedTime := tc.ft.Add(-tc.diff)
105
				assert.Equal(t, true, diffedTime.Before(unmarshaled.Time) || diffedTime.Equal(unmarshaled.Time))
106
				assert.Equal(t, true, tc.ft.After(unmarshaled.Time) || tc.ft.Equal(unmarshaled.Time))
107
			}
108
		}
109
	})
110

111
	t.Run("Time Format in structure Marshalling", func(t *testing.T) {
112
		type test struct {
113
			Date *TimeFormat `json:"date"`
114
		}
115

116
		tests := []struct {
117
			structure    *test
118
			layout       string
119
			expectedJSON string
120
			diff         time.Duration
121
		}{
122
			{
123
				structure:    &test{Date: NewTimeFormat(time.Date(2024, 4, 30, 5, 4, 7, 20, time.FixedZone("Test Zone", 0)), ShortDateLayout)},
124
				layout:       ShortDateLayout,
125
				expectedJSON: `{"date":"2024-04-30"}`,
126
				diff:         time.Hour * 24,
127
			},
128
			{
129
				structure:    &test{Date: nil},
130
				layout:       ShortDateLayout,
131
				expectedJSON: `{"date":null}`,
132
				diff:         time.Hour * 24,
133
			},
134
		}
135

136
		for _, tc := range tests {
137
			marshaled, err := json.Marshal(tc.structure)
138
			assert.Equal(t, nil, err)
139
			assert.Equal(t, tc.expectedJSON, string(marshaled))
140

141
			unmarshaled := &test{Date: newTimeLayout(tc.layout)}
142
			err = json.Unmarshal(marshaled, unmarshaled)
143
			assert.Equal(t, nil, err)
144

145
			if tc.structure != nil && tc.structure.Date != nil {
146
				diffedTime := tc.structure.Date.Add(-tc.diff)
147
				assert.Equal(t, true, diffedTime.Before(unmarshaled.Date.Time) || diffedTime.Equal(unmarshaled.Date.Time))
148
				assert.Equal(t, true, tc.structure.Date.After(unmarshaled.Date.Time) || tc.structure.Date.Equal(unmarshaled.Date.Time))
149
			}
150
		}
151
	})
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.