forked from google/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
152 lines (136 loc) · 3.6 KB
/
errors_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
package jsonapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"testing"
)
func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
err := &ErrorObject{Title: "Title test.", Detail: "Detail test."}
var input error = err
output := input.Error()
if output != fmt.Sprintf("Error: %s %s\n", err.Title, err.Detail) {
t.Fatal("Unexpected output.")
}
}
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var marshalErrorsTableTasts = []struct {
Title string
In []*ErrorObject
Out map[string]interface{}
}{
{
Title: "TestFieldsAreSerializedAsNeeded",
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
}},
},
{
Title: "TestMetaFieldIsSerializedProperly",
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
}},
},
}
for _, testRow := range marshalErrorsTableTasts {
t.Run(testRow.Title, func(t *testing.T) {
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
var writer io.Writer = buffer
_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)
if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
}
})
}
}
func TestMarshalErrors(t *testing.T) {
firstMeta := map[string]interface{}{
"custom": "info",
"custom two": "more info",
}
secondMeta := map[string]interface{}{
"foo": "foo info",
"bar": "bar info",
}
sample := map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
"id": "1",
"title": "first title",
"detail": "first detail",
"status": "400",
"code": "first code",
"meta": firstMeta,
},
map[string]interface{}{
"id": "2",
"title": "second title",
"detail": "second detail",
"status": "404",
"code": "second code",
"meta": secondMeta,
},
},
}
data, err := json.Marshal(sample)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(data)
errorsPayload, err := UnmarshalErrors(in)
if err != nil {
t.Fatal(err)
}
expectedPayload := ErrorsPayload{
Errors: []*ErrorObject{
{
ID: "1",
Title: "first title",
Detail: "first detail",
Status: "400",
Code: "first code",
Meta: &firstMeta,
}, {
ID: "2",
Title: "second title",
Detail: "second detail",
Status: "404",
Code: "second code",
Meta: &secondMeta,
},
},
}
if !reflect.DeepEqual(*errorsPayload, expectedPayload) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload)
}
}
func TestMarshalErrorsPartialData(t *testing.T) {
sample := map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
"status": "400",
},
map[string]interface{}{
"status": "404",
},
},
}
data, err := json.Marshal(sample)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(data)
errorsPayload, err := UnmarshalErrors(in)
if err != nil {
t.Fatal(err)
}
expectedPayload := ErrorsPayload{Errors: []*ErrorObject{{Status: "400"}, {Status: "404"}}}
if !reflect.DeepEqual(*errorsPayload, expectedPayload) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload)
}
}