-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_test.go
More file actions
226 lines (186 loc) · 5.88 KB
/
group_test.go
File metadata and controls
226 lines (186 loc) · 5.88 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// ── Stub implementations ────────────────────────────────────────────────
type stubGroup struct{}
func (s *stubGroup) Name() string { return "stub" }
func (s *stubGroup) BasePath() string { return "/stub" }
func (s *stubGroup) RegisterRoutes(rg *gin.RouterGroup) {
rg.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, api.OK("pong"))
})
}
type stubStreamGroup struct {
stubGroup
}
func (s *stubStreamGroup) Channels() []string {
return []string{"events", "updates"}
}
// ── RouteGroup interface ────────────────────────────────────────────────
func TestRouteGroup_Good_InterfaceSatisfaction(t *testing.T) {
var g api.RouteGroup = &stubGroup{}
if g.Name() != "stub" {
t.Fatalf("expected Name=%q, got %q", "stub", g.Name())
}
if g.BasePath() != "/stub" {
t.Fatalf("expected BasePath=%q, got %q", "/stub", g.BasePath())
}
}
func TestRouteGroup_Good_RegisterRoutes(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
g := &stubGroup{}
rg := engine.Group(g.BasePath())
g.RegisterRoutes(rg)
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
engine.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", w.Code)
}
}
// ── StreamGroup interface ───────────────────────────────────────────────
func TestStreamGroup_Good_InterfaceSatisfaction(t *testing.T) {
var g api.StreamGroup = &stubStreamGroup{}
channels := g.Channels()
if len(channels) != 2 {
t.Fatalf("expected 2 channels, got %d", len(channels))
}
if channels[0] != "events" {
t.Fatalf("expected channels[0]=%q, got %q", "events", channels[0])
}
if channels[1] != "updates" {
t.Fatalf("expected channels[1]=%q, got %q", "updates", channels[1])
}
}
func TestStreamGroup_Good_AlsoSatisfiesRouteGroup(t *testing.T) {
sg := &stubStreamGroup{}
// A StreamGroup's embedded stubGroup should also satisfy RouteGroup.
var rg api.RouteGroup = sg
if rg.Name() != "stub" {
t.Fatalf("expected Name=%q, got %q", "stub", rg.Name())
}
}
// ── DescribableGroup interface ────────────────────────────────────────
// describableStub implements DescribableGroup for testing.
type describableStub struct {
stubGroup
descriptions []api.RouteDescription
}
func (d *describableStub) Describe() []api.RouteDescription {
return d.descriptions
}
func TestDescribableGroup_Good_ImplementsRouteGroup(t *testing.T) {
stub := &describableStub{}
// Must satisfy DescribableGroup.
var dg api.DescribableGroup = stub
if dg.Name() != "stub" {
t.Fatalf("expected Name=%q, got %q", "stub", dg.Name())
}
// Must also satisfy RouteGroup since DescribableGroup embeds it.
var rg api.RouteGroup = stub
if rg.BasePath() != "/stub" {
t.Fatalf("expected BasePath=%q, got %q", "/stub", rg.BasePath())
}
}
func TestDescribableGroup_Good_DescribeReturnsRoutes(t *testing.T) {
stub := &describableStub{
descriptions: []api.RouteDescription{
{
Method: "GET",
Path: "/items",
Summary: "List items",
Tags: []string{"items"},
},
{
Method: "POST",
Path: "/items",
Summary: "Create item",
Tags: []string{"items"},
RequestBody: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{"type": "string"},
},
},
},
},
}
var dg api.DescribableGroup = stub
descs := dg.Describe()
if len(descs) != 2 {
t.Fatalf("expected 2 descriptions, got %d", len(descs))
}
if descs[0].Method != "GET" {
t.Fatalf("expected descs[0].Method=%q, got %q", "GET", descs[0].Method)
}
if descs[0].Summary != "List items" {
t.Fatalf("expected descs[0].Summary=%q, got %q", "List items", descs[0].Summary)
}
if descs[1].Method != "POST" {
t.Fatalf("expected descs[1].Method=%q, got %q", "POST", descs[1].Method)
}
if descs[1].RequestBody == nil {
t.Fatal("expected descs[1].RequestBody to be non-nil")
}
}
func TestDescribableGroup_Good_EmptyDescribe(t *testing.T) {
stub := &describableStub{
descriptions: nil,
}
var dg api.DescribableGroup = stub
descs := dg.Describe()
if descs != nil {
t.Fatalf("expected nil descriptions, got %v", descs)
}
}
func TestDescribableGroup_Good_MultipleVerbs(t *testing.T) {
stub := &describableStub{
descriptions: []api.RouteDescription{
{Method: "GET", Path: "/resources", Summary: "List resources"},
{Method: "POST", Path: "/resources", Summary: "Create resource"},
{Method: "DELETE", Path: "/resources/:id", Summary: "Delete resource"},
},
}
var dg api.DescribableGroup = stub
descs := dg.Describe()
if len(descs) != 3 {
t.Fatalf("expected 3 descriptions, got %d", len(descs))
}
expected := []string{"GET", "POST", "DELETE"}
for i, want := range expected {
if descs[i].Method != want {
t.Fatalf("expected descs[%d].Method=%q, got %q", i, want, descs[i].Method)
}
}
}
func TestDescribableGroup_Bad_NilSchemas(t *testing.T) {
stub := &describableStub{
descriptions: []api.RouteDescription{
{
Method: "GET",
Path: "/health",
Summary: "Health check",
RequestBody: nil,
Response: nil,
},
},
}
var dg api.DescribableGroup = stub
descs := dg.Describe()
if len(descs) != 1 {
t.Fatalf("expected 1 description, got %d", len(descs))
}
if descs[0].RequestBody != nil {
t.Fatalf("expected nil RequestBody, got %v", descs[0].RequestBody)
}
if descs[0].Response != nil {
t.Fatalf("expected nil Response, got %v", descs[0].Response)
}
}