-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmock_context_test.go
197 lines (170 loc) · 5.1 KB
/
mock_context_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
package fuego_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/option"
"github.com/go-fuego/fuego/param"
)
// UserSearchRequest represents the search criteria for users
type UserSearchRequest struct {
MinAge int `json:"minAge" validate:"gte=0,lte=150"`
MaxAge int `json:"maxAge" validate:"gte=0,lte=150"`
NameQuery string `json:"nameQuery" validate:"required"`
}
// UserSearchResponse represents the search results
type UserSearchResponse struct {
Users []UserProfile `json:"users"`
TotalCount int `json:"totalCount"`
CurrentPage int `json:"currentPage"`
}
// UserProfile represents a user in the system
type UserProfile struct {
ID string `json:"id"`
Name string `json:"name" validate:"required"`
Age int `json:"age" validate:"gte=0,lte=150"`
Email string `json:"email" validate:"required,email"`
}
// SearchUsersController is an example of a real controller that would be used in a Fuego app
func SearchUsersController(c fuego.ContextWithBody[UserSearchRequest]) (UserSearchResponse, error) {
// Get and validate the request body
body, err := c.Body()
if err != nil {
return UserSearchResponse{}, err
}
// Get pagination parameters from query
page := c.QueryParamInt("page")
if page < 1 {
page = 1
}
perPage := c.QueryParamInt("perPage")
if perPage < 1 || perPage > 100 {
perPage = 20
}
// Example validation beyond struct tags
if body.MinAge > body.MaxAge {
return UserSearchResponse{}, errors.New("minAge cannot be greater than maxAge")
}
// In a real app, this would query a database
// Here we just return mock data that matches the criteria
users := []UserProfile{
{ID: "user_1", Name: "John Doe", Age: 25, Email: "[email protected]"},
{ID: "user_2", Name: "Jane Smith", Age: 30, Email: "[email protected]"},
}
// Filter users based on criteria (simplified example)
var filteredUsers []UserProfile
for _, user := range users {
if user.Age >= body.MinAge && user.Age <= body.MaxAge {
filteredUsers = append(filteredUsers, user)
}
}
return UserSearchResponse{
Users: filteredUsers,
TotalCount: len(filteredUsers),
CurrentPage: page,
}, nil
}
func TestSearchUsersController(t *testing.T) {
tests := []struct {
name string
body UserSearchRequest
setupContext func(*fuego.MockContext[UserSearchRequest])
expectedError string
expected UserSearchResponse
}{
{
name: "successful search with age range",
body: UserSearchRequest{
MinAge: 20,
MaxAge: 35,
NameQuery: "John",
},
setupContext: func(ctx *fuego.MockContext[UserSearchRequest]) {
ctx.SetQueryParamInt("page", 1)
ctx.SetQueryParamInt("perPage", 20)
},
expected: UserSearchResponse{
Users: []UserProfile{
{ID: "user_1", Name: "John Doe", Age: 25, Email: "[email protected]"},
{ID: "user_2", Name: "Jane Smith", Age: 30, Email: "[email protected]"},
},
TotalCount: 2,
CurrentPage: 1,
},
},
{
name: "invalid age range",
body: UserSearchRequest{
MinAge: 40,
MaxAge: 20,
NameQuery: "John",
},
expectedError: "minAge cannot be greater than maxAge",
},
{
name: "default pagination values",
body: UserSearchRequest{
MinAge: 20,
MaxAge: 35,
NameQuery: "John",
},
expected: UserSearchResponse{
Users: []UserProfile{
{ID: "user_1", Name: "John Doe", Age: 25, Email: "[email protected]"},
{ID: "user_2", Name: "Jane Smith", Age: 30, Email: "[email protected]"},
},
TotalCount: 2,
CurrentPage: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create mock context with the test body
ctx := fuego.NewMockContext(tt.body)
// Set up context with query parameters if provided
if tt.setupContext != nil {
tt.setupContext(ctx)
}
// Call the controller
response, err := SearchUsersController(ctx)
// Check error cases
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
return
}
// Check success cases
assert.NoError(t, err)
assert.Equal(t, tt.expected, response)
})
}
}
func TestMockContextNoBody(t *testing.T) {
myController := func(c fuego.ContextNoBody) (string, error) {
return "Hello, " + c.QueryParam("name"), nil
}
// Just check that `myController` is indeed an acceptable Fuego controller
s := fuego.NewServer()
fuego.Get(s, "/route", myController,
option.Query("name", "Name given to be greeted", param.Default("World")),
)
t.Run("TestMockContextNoBody", func(t *testing.T) {
ctx := fuego.NewMockContextNoBody()
assert.NotNil(t, ctx)
ctx.SetQueryParam("name", "You")
// Call the controller
response, err := myController(ctx)
require.NoError(t, err)
require.Equal(t, "Hello, You", response)
})
t.Run("Does not use the default params from the route declaration", func(t *testing.T) {
ctx := fuego.NewMockContextNoBody()
assert.NotNil(t, ctx)
// Call the controller
response, err := myController(ctx)
require.NoError(t, err)
require.Equal(t, "Hello, ", response)
})
}