-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonwrapper_test.go
216 lines (152 loc) · 4.63 KB
/
jsonwrapper_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
package jsonwrapper
import (
"errors"
"reflect"
"testing"
)
func TestConstants(t *testing.T) {
if StatusUnclassified != "unclassified" {
t.Error("Expected status unclassified to be «unclassified», was", StatusUnclassified)
}
if StatusSuccess != "success" {
t.Error("Expected status success to be «success», was", StatusSuccess)
}
if StatusError != "error" {
t.Error("Expected status error to be «error», was", StatusError)
}
if StatusFail != "fail" {
t.Error("Expected status fail to be «fail», was", StatusFail)
}
}
func TestWrap(t *testing.T) {
// --- status unclassified
statusCode := 700
data := []string{"test one", "test two", "test three", "test four"}
message := "a custom message"
response := Wrap(statusCode, data, message)
if response.Code != 700 {
t.Error("Expected status code to be 700, was", response.Code)
}
if response.Status != StatusUnclassified {
t.Error("Expected the status to be unclassified, was", response.Status)
}
if response.Message != message {
t.Error("Expected the message to equal our custom message")
}
if reflect.DeepEqual(response.Data, data) != true {
t.Error("Expeted the data to contain our data")
}
// --- status success ---
statusCode = 222
response = Wrap(statusCode, data, message)
if response.Code != 222 {
t.Error("Expected status code to be 222, was", response.Code)
}
if response.Status != StatusSuccess {
t.Error("Expected the status to be success, was", response.Status)
}
// --- status fail ---
statusCode = 555
response = Wrap(statusCode, data, message)
if response.Code != 555 {
t.Error("Expected status code to be 555, was", response.Code)
}
if response.Status != StatusFail {
t.Error("Expected the status to be fail, was", response.Status)
}
// --- status error ---
statusCode = 444
response = Wrap(statusCode, data, message)
if response.Code != 444 {
t.Error("Expected status code to be 444, was", response.Code)
}
if response.Status != StatusError {
t.Error("Expected the status to be error, was", response.Status)
}
}
func TestWrapManually(t *testing.T) {
statusCode := 700
status := "undefined status"
data := []string{"test one", "test two", "test three", "test four"}
message := "a custom message"
response := WrapManually(statusCode, status, data, message)
if response.Code != 700 {
t.Error("Expected status code to be 700")
}
if response.Status != status {
t.Error("Expected the status to equal our custom status")
}
if response.Message != message {
t.Error("Expected the message to equal our custom message")
}
if reflect.DeepEqual(response.Data, data) != true {
t.Error("Expeted the data to contain our data")
}
}
func TestSuccess(t *testing.T) {
data := []string{"test one", "test two", "test three", "test four"}
response := Success(data)
if response.Code != 200 {
t.Error("Expected status code to be 200")
}
if response.Status != StatusSuccess {
t.Error("Expected the status to be success")
}
if response.Message != "" {
t.Error("Expected the message to be empty")
}
if reflect.DeepEqual(response.Data, data) != true {
t.Error("Expeted the data to contain our data")
}
}
func TestError(t *testing.T) {
var message = "The request was faulty"
var errorData = "This is a error"
err := errors.New(errorData)
response := Error(message, err)
if response.Code != 400 {
t.Error("Expected status code to be 400")
}
if response.Status != StatusError {
t.Error("Expected the status to be error")
}
if response.Message != message {
t.Error("Expected the message to equal our message")
}
if response.Data != err {
t.Error("Expeted the data to contain the error-information")
}
}
func TestFail(t *testing.T) {
var message = "The request failed"
var errorData = "This is a fail error"
err := errors.New(errorData)
response := Fail(message, err)
if response.Code != 500 {
t.Error("Expected status code to be 500")
}
if response.Status != StatusFail {
t.Error("Expected the status to be fail")
}
if response.Message != message {
t.Error("Expected the message to equal our message")
}
if response.Data != err {
t.Error("Expeted the data to contain the error-information")
}
}
func TestBetween(t *testing.T) {
// test if our between function is returning the expected results
if between(100, 50, 200) != true {
t.Error("Expected the result of between() to be true")
}
if between(100, 100, 101) != true {
t.Error("Expected the result of between() to be true")
}
if between(101, 100, 101) != true {
t.Error("Expected the result of between() to be true")
}
if between(10, 100, 101) != false {
t.Error("Expected the result of between() to be false")
}
}