-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_test.go
More file actions
205 lines (176 loc) · 5.34 KB
/
response_test.go
File metadata and controls
205 lines (176 loc) · 5.34 KB
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
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"encoding/json"
"testing"
api "dappco.re/go/core/api"
)
// ── OK ──────────────────────────────────────────────────────────────────
func TestOK_Good(t *testing.T) {
r := api.OK("hello")
if !r.Success {
t.Fatal("expected Success=true")
}
if r.Data != "hello" {
t.Fatalf("expected Data=%q, got %q", "hello", r.Data)
}
if r.Error != nil {
t.Fatal("expected Error to be nil")
}
if r.Meta != nil {
t.Fatal("expected Meta to be nil")
}
}
func TestOK_Good_StructData(t *testing.T) {
type user struct {
Name string `json:"name"`
}
r := api.OK(user{Name: "Ada"})
if !r.Success {
t.Fatal("expected Success=true")
}
if r.Data.Name != "Ada" {
t.Fatalf("expected Data.Name=%q, got %q", "Ada", r.Data.Name)
}
}
func TestOK_Good_JSONOmitsErrorAndMeta(t *testing.T) {
r := api.OK("data")
b, err := json.Marshal(r)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var raw map[string]any
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if _, ok := raw["error"]; ok {
t.Fatal("expected 'error' field to be omitted from JSON")
}
if _, ok := raw["meta"]; ok {
t.Fatal("expected 'meta' field to be omitted from JSON")
}
if _, ok := raw["success"]; !ok {
t.Fatal("expected 'success' field to be present")
}
if _, ok := raw["data"]; !ok {
t.Fatal("expected 'data' field to be present")
}
}
// ── Fail ────────────────────────────────────────────────────────────────
func TestFail_Good(t *testing.T) {
r := api.Fail("NOT_FOUND", "resource not found")
if r.Success {
t.Fatal("expected Success=false")
}
if r.Error == nil {
t.Fatal("expected Error to be non-nil")
}
if r.Error.Code != "NOT_FOUND" {
t.Fatalf("expected Code=%q, got %q", "NOT_FOUND", r.Error.Code)
}
if r.Error.Message != "resource not found" {
t.Fatalf("expected Message=%q, got %q", "resource not found", r.Error.Message)
}
if r.Error.Details != nil {
t.Fatal("expected Details to be nil")
}
}
func TestFail_Good_JSONOmitsData(t *testing.T) {
r := api.Fail("ERR", "something went wrong")
b, err := json.Marshal(r)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var raw map[string]any
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if _, ok := raw["data"]; ok {
t.Fatal("expected 'data' field to be omitted from JSON")
}
if _, ok := raw["error"]; !ok {
t.Fatal("expected 'error' field to be present")
}
}
// ── FailWithDetails ─────────────────────────────────────────────────────
func TestFailWithDetails_Good(t *testing.T) {
details := map[string]string{"field": "email", "reason": "invalid format"}
r := api.FailWithDetails("VALIDATION", "validation failed", details)
if r.Success {
t.Fatal("expected Success=false")
}
if r.Error == nil {
t.Fatal("expected Error to be non-nil")
}
if r.Error.Code != "VALIDATION" {
t.Fatalf("expected Code=%q, got %q", "VALIDATION", r.Error.Code)
}
if r.Error.Details == nil {
t.Fatal("expected Details to be non-nil")
}
}
func TestFailWithDetails_Good_JSONIncludesDetails(t *testing.T) {
r := api.FailWithDetails("ERR", "bad", "extra info")
b, err := json.Marshal(r)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var raw map[string]any
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
errObj, ok := raw["error"].(map[string]any)
if !ok {
t.Fatal("expected 'error' to be an object")
}
if _, ok := errObj["details"]; !ok {
t.Fatal("expected 'details' field to be present in error")
}
}
// ── Paginated ───────────────────────────────────────────────────────────
func TestPaginated_Good(t *testing.T) {
items := []string{"a", "b", "c"}
r := api.Paginated(items, 2, 25, 100)
if !r.Success {
t.Fatal("expected Success=true")
}
if len(r.Data) != 3 {
t.Fatalf("expected 3 items, got %d", len(r.Data))
}
if r.Meta == nil {
t.Fatal("expected Meta to be non-nil")
}
if r.Meta.Page != 2 {
t.Fatalf("expected Page=2, got %d", r.Meta.Page)
}
if r.Meta.PerPage != 25 {
t.Fatalf("expected PerPage=25, got %d", r.Meta.PerPage)
}
if r.Meta.Total != 100 {
t.Fatalf("expected Total=100, got %d", r.Meta.Total)
}
}
func TestPaginated_Good_JSONIncludesMeta(t *testing.T) {
r := api.Paginated([]int{1}, 1, 10, 50)
b, err := json.Marshal(r)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var raw map[string]any
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if _, ok := raw["meta"]; !ok {
t.Fatal("expected 'meta' field to be present")
}
meta := raw["meta"].(map[string]any)
if meta["page"].(float64) != 1 {
t.Fatalf("expected page=1, got %v", meta["page"])
}
if meta["per_page"].(float64) != 10 {
t.Fatalf("expected per_page=10, got %v", meta["per_page"])
}
if meta["total"].(float64) != 50 {
t.Fatalf("expected total=50, got %v", meta["total"])
}
}