-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson_reader_test.go
143 lines (130 loc) · 3.47 KB
/
json_reader_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
package jsonconv
import (
"strings"
"testing"
)
func TestJsonReader_InvalidJson(t *testing.T) {
// Prepare
raw := `"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6" }`
obj := make(JsonObject)
re := NewJsonReader(strings.NewReader(raw))
// Process
err := re.Read(&obj)
// Check
if err == nil {
t.Fatalf("Should throw an error for invalid JSON")
}
}
func TestJsonReader_JsonObject(t *testing.T) {
// Prepare
raw := `
{
"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6",
"user": "Jon Doe",
"score": "-100",
"is active": "false"
}`
obj := make(JsonObject)
re := NewJsonReader(strings.NewReader(raw))
// Process
err := re.Read(&obj)
if err != nil {
t.Fatalf("failed to read json, err: %v", err)
}
// Check
if obj["id"] != "b042ab5c-ca73-4460-b739-96410ea9d3a6" ||
obj["user"] != "Jon Doe" ||
obj["score"] != "-100" ||
obj["is active"] != "false" {
t.Fatalf("failed to read json object")
}
}
func TestJsonReader_JsonArray(t *testing.T) {
// Prepare
raw := `
[
{
"id": "ce06f5b1-5721-42c0-91e1-9f72a09c250a",
"user": "Tuấn",
"score": "1.5",
"is active": "true"
},
{
"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6",
"user": "Jon Doe",
"score": "-100",
"is active": "false"
},
{
"id": "4e01b638-44e5-4079-8043-baabbff21cc8",
"user": "高橋",
"score": "100000000000000000000000",
"is active": "true"
}
]`
arr := make(JsonArray, 0)
re := NewJsonReader(strings.NewReader(raw))
// Process
err := re.Read(&arr)
if err != nil {
t.Fatalf("failed to read file %v", err)
}
// Check
if len(arr) != 3 {
t.Fatalf("failed to read json array")
}
if arr[0]["id"] != "ce06f5b1-5721-42c0-91e1-9f72a09c250a" ||
arr[0]["user"] != "Tuấn" ||
arr[0]["score"] != "1.5" ||
arr[0]["is active"] != "true" {
t.Fatalf("failed to read json array")
}
if arr[1]["id"] != "b042ab5c-ca73-4460-b739-96410ea9d3a6" ||
arr[1]["user"] != "Jon Doe" ||
arr[1]["score"] != "-100" ||
arr[1]["is active"] != "false" {
t.Fatalf("failed to read json array")
}
if arr[2]["id"] != "4e01b638-44e5-4079-8043-baabbff21cc8" ||
arr[2]["user"] != "高橋" ||
arr[2]["score"] != "100000000000000000000000" ||
arr[2]["is active"] != "true" {
t.Fatalf("failed to read json array")
}
}
func TestJsonReader_JsonArray_NewlineDelimited(t *testing.T) {
// Prepare
raw := `
{"id": "ce06f5b1-5721-42c0-91e1-9f72a09c250a","user": "Tuấn","score": "1.5","is active": "true"}
{"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6","user": "Jon Doe","score": "-100","is active": "false"}
{"id": "4e01b638-44e5-4079-8043-baabbff21cc8","user": "高橋","score": "100000000000000000000000","is active": "true"}`
arr := make(JsonArray, 0)
re := NewJsonReader(strings.NewReader(raw))
// Process
err := re.Read(&arr)
if err != nil {
t.Fatalf("failed to read file %v", err)
}
// Check
if len(arr) != 3 {
t.Fatalf("failed to read json array")
}
if arr[0]["id"] != "ce06f5b1-5721-42c0-91e1-9f72a09c250a" ||
arr[0]["user"] != "Tuấn" ||
arr[0]["score"] != "1.5" ||
arr[0]["is active"] != "true" {
t.Fatalf("failed to read json array")
}
if arr[1]["id"] != "b042ab5c-ca73-4460-b739-96410ea9d3a6" ||
arr[1]["user"] != "Jon Doe" ||
arr[1]["score"] != "-100" ||
arr[1]["is active"] != "false" {
t.Fatalf("failed to read json array")
}
if arr[2]["id"] != "4e01b638-44e5-4079-8043-baabbff21cc8" ||
arr[2]["user"] != "高橋" ||
arr[2]["score"] != "100000000000000000000000" ||
arr[2]["is active"] != "true" {
t.Fatalf("failed to read json array")
}
}