-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindices_test.go
278 lines (243 loc) · 9.52 KB
/
indices_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package moexiss
import (
"context"
"github.com/buger/jsonparser"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestIndicesGetUrl(t *testing.T) {
var income *IndicesRequestOptions = nil
c := NewClient(nil)
gotURL, err := c.Indices.getUrl("sberp", income)
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := gotURL, `https://iss.moex.com/iss/securities/sberp/indices.json?iss.json=extended&iss.meta=off`; got != expected {
t.Fatalf("Error: expecting url :\n`%s` \ngot \n`%s` \ninstead", expected, got)
}
}
func TestIndicesGetUrlBadSecurity(t *testing.T) {
var income *IndicesRequestOptions = nil
c := NewClient(nil)
_, err := c.Indices.getUrl("", income)
if err != ErrBadSecurityParameter {
t.Fatalf("Error: expecting error: %v \ngot %v \ninstead", ErrBadSecurityParameter, err)
}
}
func TestParseIndicesItem(t *testing.T) {
expectedStruct := Indices{
IndexId: "IMOEX",
IndexName: "Индекс МосБиржи",
From: "2007-04-16",
Till: "2022-01-26",
}
var incomeJSON = `
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"}
`
indicesItem := Indices{}
err := parseIndicesItem([]byte(incomeJSON), &indicesItem)
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := indicesItem, expectedStruct; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestParseIndicesItemErrCases(t *testing.T) {
type Case struct {
incomeJSON string
}
cases := []Case{
// no SECID
{`{"SECID1": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"}`},
// no SHORTNAME
{`{"SECID": "IMOEX", "SHORTNAME1": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"}`},
// no FROM
{`{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM1": "2007-04-16", "TILL": "2022-01-26"}`},
// no TILL
{`{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL1": "2022-01-26"}`},
}
for i, c := range cases {
indices := Indices{}
if got, expected := parseIndicesItem([]byte(c.incomeJSON), &indices), jsonparser.KeyPathNotFoundError; got != expected {
t.Fatalf("Error: expecting error: \n %v \ngot:\n %v \ninstead in %d case", expected, got, i)
}
}
}
func TestParseIndices(t *testing.T) {
var incomeJSON = `
[
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"},
{"SECID": "IMOEX2", "SHORTNAME": "Индекс МосБиржи (все сессии)", "FROM": "2020-06-22", "TILL": "2022-01-25"},
{"SECID": "MCXSM", "SHORTNAME": "Индекс МосБиржи SMID", "FROM": "2014-01-06", "TILL": "2014-12-30"},
{"SECID": "RUCGI", "SHORTNAME": "Нац. индекс корп. Управления", "FROM": "2021-06-18", "TILL": "2022-01-26"}
]
`
indices := make([]Indices, 0)
err := parseIndices([]byte(incomeJSON), &indices)
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := len(indices), 4; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestParseIndicesUnexpectedDataTypeError(t *testing.T) {
var incomeJSON = `
[
[]
]`
indices := make([]Indices, 0)
if got, expected := parseIndices([]byte(incomeJSON), &indices), ErrUnexpectedDataType; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestParseIndicesError(t *testing.T) {
var incomeJSON = `
[
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"},
{"SECID": "RUCGI", "SHORTNAME1": "Нац. индекс корп. Управления", "FROM": "2021-06-18", "TILL": "2022-01-26"},
{"SECID": "RUCGI", "SHORTNAME": "Нац. индекс корп. Управления", "FROM": "2021-06-18", "TILL": "2022-01-26"}
]`
indices := make([]Indices, 0)
if got, expected := parseIndices([]byte(incomeJSON), &indices), jsonparser.KeyPathNotFoundError; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestParseIndicesResponse(t *testing.T) {
var incomeJSON = `
[
{"charsetinfo": {"name": "utf-8"}},
{
"indices": [
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"},
{"SECID": "RTSI", "SHORTNAME": "Индекс РТС", "FROM": "2009-09-30", "TILL": "2022-01-26"}
]}
]
`
expectedResponse := IndicesResponse{
Indices: []Indices{
{
IndexId: "IMOEX",
IndexName: "Индекс МосБиржи",
From: "2007-04-16",
Till: "2022-01-26",
},
{
IndexId: "RTSI",
IndexName: "Индекс РТС",
From: "2009-09-30",
Till: "2022-01-26"},
},
}
indicesR := IndicesResponse{}
var err error = nil
if got, expected := parseIndicesResponse([]byte(incomeJSON), &indicesR), err; got != expected {
t.Fatalf("Error: expecting error: \n %v \ngot:\n %v \ninstead", expected, got)
}
if got, expected := len(indicesR.Indices), len(expectedResponse.Indices); got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
for i, gotItem := range indicesR.Indices {
if got, expected := gotItem, expectedResponse.Indices[i]; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
}
func TestParseIndicesResponseNilError(t *testing.T) {
var incomeJSON = ``
var indicesResponse *IndicesResponse = nil
if got, expected := parseIndicesResponse([]byte(incomeJSON), indicesResponse), ErrNilPointer; got != expected {
t.Fatalf("Error: expecting error: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestParseIndicesResponseError(t *testing.T) {
var incomeJSON = `
[
{"charsetinfo": {"name": "utf-8"}},
{
"indices": [
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"},
{"SECID1": "RUCGI", "SHORTNAME": "Нац. индекс корп. Управления", "FROM": "2021-06-18", "TILL": "2022-01-26"}]}
]
`
var indicesResponse = &IndicesResponse{}
if got, expected := parseIndicesResponse([]byte(incomeJSON), indicesResponse), jsonparser.KeyPathNotFoundError; got != expected {
t.Fatalf("Error: expecting error: \n %v \ngot:\n %v \ninstead", expected, got)
}
}
func TestIndicesService_BadSecurityParam(t *testing.T) {
srv := getEmptySrv()
defer srv.Close()
httpClient := srv.Client()
c := NewClient(httpClient)
c.BaseURL, _ = url.Parse(srv.URL)
_, err := c.Indices.GetIndices(context.Background(), "", nil)
if got, expected := err, ErrBadSecurityParameter; got == nil || got != expected {
t.Fatalf("Error: expecting %v error \ngot %v \ninstead", expected, got)
}
}
func TestIndicesService_BadUrl(t *testing.T) {
srv := getEmptySrv()
defer srv.Close()
httpClient := srv.Client()
c := NewClient(httpClient)
c.BaseURL, _ = url.Parse(srv.URL)
_, err := c.Indices.GetIndices(context.Background(), "sber", nil)
if got, expected := err, "BaseURL must have a trailing slash, but \""+srv.URL+"\" does not"; got == nil || got.Error() != expected {
t.Fatalf("Error: expecting %v error \ngot %v \ninstead", expected, got)
}
}
func TestIndicesKeyPathNotFound(t *testing.T) {
srv := getEmptySrv()
defer srv.Close()
httpClient := srv.Client()
c := NewClient(httpClient)
c.BaseURL, _ = url.Parse(srv.URL + "/")
_, err := c.Indices.GetIndices(context.Background(), "jhgsd", nil)
if got, expected := err, jsonparser.KeyPathNotFoundError; got == nil || got != expected {
t.Fatalf("Error: expecting %v error \ngot %v \ninstead", expected, got)
}
}
func TestIndicesNilContextError(t *testing.T) {
c := NewClient(nil)
var ctx context.Context = nil
_, err := c.Indices.GetIndices(ctx, "SBERP", nil)
if got, expected := err, ErrNonNilContext; got == nil || got != expected {
t.Fatalf("Error: expecting %v error \ngot %v \ninstead", expected, got)
}
}
func TestIndicesService_Indices(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
str := `
[
{
"indices": [
{"SECID": "IMOEX", "SHORTNAME": "Индекс МосБиржи", "FROM": "2007-04-16", "TILL": "2022-01-26"},
{"SECID": "IMOEX2", "SHORTNAME": "Индекс МосБиржи (все сессии)", "FROM": "2020-06-22", "TILL": "2022-01-25"},
{"SECID": "MICEXLC", "SHORTNAME": "MICEX LC", "FROM": "2006-07-03", "TILL": "2013-05-17"},
{"SECID": "MICEXMC", "SHORTNAME": "MICEX MC", "FROM": "2006-09-04", "TILL": "2007-07-13"},
{"SECID": "MOEXBC", "SHORTNAME": "Индекс голубых фишек", "FROM": "2019-09-20", "TILL": "2022-01-26"},
{"SECID": "RUCGI", "SHORTNAME": "Нац. индекс корп. Управления", "FROM": "2021-06-18", "TILL": "2022-01-26"}]}
]
`
_, _ = w.Write([]byte(str))
}))
defer srv.Close()
httpClient := srv.Client()
secId := "SBERP"
c := NewClient(httpClient)
c.BaseURL, _ = url.Parse(srv.URL + "/")
result, err := c.Indices.GetIndices(context.Background(), secId, nil)
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := len(result.Indices), 6; got != expected {
t.Fatalf("Error: expecting: \n %v items\ngot:\n %v items\ninstead", expected, got)
}
if got, expected := result.SecurityId, secId; got != expected {
t.Fatalf("Error: expecting: \n %v \ngot:\n %v \ninstead", expected, got)
}
}