-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_test.go
151 lines (133 loc) · 4.78 KB
/
security_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
package moexiss
import (
"github.com/buger/jsonparser"
"testing"
)
var secArray = []string{
`[5444, "SBERP", "Сбербанк-п", "20301481B", "Сбербанк России ПАО ап", "RU0009029557", 1, 1199, "Публичное акционерное общество \"Сбербанк России\"", "7707083893", "00032537", "20301481B", "preferred_share", "stock_shares", "TQBR", "TQBR"]`,
`[414220919, "AA-RM", "Alcoa", null, "Alcoa Corporation", "US0138721065", 1, 1375753, "Alcoa Corporation (Алкоа Корпорэйшн)", "0000005270", null, null, "common_share", "stock_shares", "FQBR", "FQBR"]`,
}
var dataField = `{ "data": [ ` +
secArray[0] +
`,` +
secArray[1] +
`]}`
var omittedSecResp = `{ "securities":` +
dataField +
`}`
func TestParseSecurityItem(t *testing.T) {
expected := Security{
Id: 5444,
SecId: "SBERP",
ShortName: "Сбербанк-п",
RegNumber: "20301481B",
Name: "Сбербанк России ПАО ап",
Isin: "RU0009029557",
IsTraded: true,
EmitentId: "1199",
EmitentTitle: "Публичное акционерное общество \"Сбербанк России\"",
EmitentInn: "7707083893",
EmitentOkpo: "00032537",
GosReg: "20301481B",
Type: "preferred_share",
Group: "stock_shares",
PrimaryBoardId: "TQBR",
MarketPriceBoardId: "TQBR",
}
got := Security{}
err := parseSecurityItem(&got, []byte(secArray[0]))
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got != expected {
t.Fatalf("Error: expected \n%v : \ngot \n%v \ninstead", expected, got)
}
}
func TestParseSecurityItemBadBytes(t *testing.T) {
expected := jsonparser.UnknownValueTypeError
got := Security{}
err := parseSecurityItem(&got, []byte("[s]"))
if err != expected {
t.Fatalf("Error: expecting %v error: \ngot %v \ninstead", expected, err)
}
}
func TestParseSecurityItemBadInt(t *testing.T) {
expected := jsonparser.OverflowIntegerError
got := Security{}
err := parseSecurityItem(&got, []byte("[-9923372036854775809]"))
if err != expected {
t.Fatalf("Error: expecting %v error: \ngot %v \ninstead", expected, err)
}
}
func TestParseSecurityItemNil(t *testing.T) {
expectedText := "<nil> pointer passed instead of *Security"
var nilSecurity *Security = nil
err := parseSecurityItem(nilSecurity, []byte(""))
if err == nil {
t.Fatalf("Error: expecting error: \n%v \ngot %v \ninstead", expectedText, err)
}
if err.Error() != expectedText {
t.Fatalf("Error: expecting %v error: \ngot %v \ninstead", expectedText, err)
}
}
func TestParseSecurities(t *testing.T) {
income := dataField
securities := make([]Security, 0, 2)
err := parseSecurities(&securities, []byte(income))
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := len(securities), len(secArray); got != expected {
t.Fatalf("Error: expecting %d items \ngot %d items \ninstead", expected, got)
}
}
func TestParseSecuritiesResponse(t *testing.T) {
income := omittedSecResp
securities := make([]Security, 0, 2)
err := parseSecuritiesResponse(&securities, []byte(income))
if err != nil {
t.Fatalf("Error: expecting <nil> error: \ngot %v \ninstead", err)
}
if got, expected := len(securities), len(secArray); got != expected {
t.Fatalf("Error: expecting %d items \ngot %d items \ninstead", expected, got)
}
}
func TestParseSecuritiesResponseKeyPathNotFoundError(t *testing.T) {
income := `{ "securities; }`
securities := make([]Security, 0, 2)
if got, expected := parseSecuritiesResponse(&securities, []byte(income)), jsonparser.KeyPathNotFoundError; got != expected {
t.Fatalf("Error: expecting %v error: got %v instead", got, expected)
}
}
func TestParseSecuritiesWrongTypeJson(t *testing.T) {
income := `{ "securities": [] }`
securities := make([]Security, 0, 2)
err := parseSecuritiesResponse(&securities, []byte(income))
if err == nil {
t.Fatalf("Error: expecting non-nil error: got <nil> instead")
}
}
func TestIndexParseSecurityMalformedArrayError(t *testing.T) {
var incomeJSON = `{
"data": [
[5444,
]
}
`
securities := make([]Security, 0, 2)
if got, expected := parseSecurities(&securities, []byte(incomeJSON)), jsonparser.MalformedArrayError; got == nil || got != expected {
t.Fatalf("Error: expecting:\n'%v'\ngot:\n'%v'\ninstead", expected, got)
}
}
func TestIndexParseSecurityUnknownValueTypeError(t *testing.T) {
var incomeJSON = `{
"data": [
[5444,]
]
}
`
securities := make([]Security, 0, 2)
if got, expected := parseSecurities(&securities, []byte(incomeJSON)), jsonparser.UnknownValueTypeError; got == nil || got != expected {
t.Fatalf("Error: expecting:\n'%v'\ngot:\n'%v'\ninstead", expected, got)
}
}