-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolution_test.go
More file actions
248 lines (229 loc) · 6.08 KB
/
Copy pathresolution_test.go
File metadata and controls
248 lines (229 loc) · 6.08 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package promptSDK
import (
"testing"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/prompt-edu/prompt-sdk/promptTypes"
"github.com/stretchr/testify/assert"
)
func TestCoursePhaseParticipationsWithResolutionsValidation(t *testing.T) {
// Get validator engine
v, ok := binding.Validator.Engine().(*validator.Validate)
if !ok {
t.Fatal("could not get validator engine")
}
// Create valid UUID for testing
validUUID := uuid.MustParse("f47ac10b-58cc-4372-a567-0e02b2c3d479")
// Create test cases
tests := []struct {
name string
resolutions []Resolution
wantError bool
errorField string
}{
{
name: "Valid resolutions",
resolutions: []Resolution{
{
DtoName: "TestDTO",
BaseURL: "https://example.com",
EndpointPath: "/api/endpoint",
CoursePhaseID: validUUID,
},
},
wantError: false,
},
{
name: "Missing DtoName",
resolutions: []Resolution{
{
DtoName: "", // Missing required field
BaseURL: "https://example.com",
EndpointPath: "/api/endpoint",
CoursePhaseID: validUUID,
},
},
wantError: true,
errorField: "DtoName",
},
{
name: "Invalid BaseURL",
resolutions: []Resolution{
{
DtoName: "TestDTO",
BaseURL: "invalid-url", // Not a valid URL
EndpointPath: "/api/endpoint",
CoursePhaseID: validUUID,
},
},
wantError: true,
errorField: "BaseURL",
},
{
name: "Missing EndpointPath",
resolutions: []Resolution{
{
DtoName: "TestDTO",
BaseURL: "https://example.com",
EndpointPath: "", // Missing required field
CoursePhaseID: validUUID,
},
},
wantError: true,
errorField: "EndpointPath",
},
{
name: "Invalid CoursePhaseID",
resolutions: []Resolution{
{
DtoName: "TestDTO",
BaseURL: "https://example.com",
EndpointPath: "/api/endpoint",
CoursePhaseID: uuid.UUID{}, // Zero UUID, invalid
},
},
wantError: true,
errorField: "CoursePhaseID",
},
{
name: "Multiple invalid fields",
resolutions: []Resolution{
{
DtoName: "", // Invalid
BaseURL: "invalid-url", // Invalid
EndpointPath: "", // Invalid
CoursePhaseID: uuid.UUID{}, // Invalid
},
},
wantError: true,
// Multiple errors expected
},
{
name: "Multiple resolutions with one invalid",
resolutions: []Resolution{
{
DtoName: "TestDTO1",
BaseURL: "https://example.com",
EndpointPath: "/api/endpoint1",
CoursePhaseID: validUUID,
},
{
DtoName: "", // Invalid
BaseURL: "https://example.com",
EndpointPath: "/api/endpoint2",
CoursePhaseID: validUUID,
},
},
wantError: true,
errorField: "DtoName",
},
}
// Run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Create a struct to validate
cpp := CoursePhaseParticipationsWithResolutions{
Participations: []promptTypes.CoursePhaseParticipationWithStudent{},
Resolutions: tt.resolutions,
}
// Validate the struct
err := v.Struct(cpp)
// Check if error matches expectation
if tt.wantError {
assert.Error(t, err)
if tt.errorField != "" {
// Check if the error contains the expected field
assert.Contains(t, err.Error(), tt.errorField)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestGetEndpointPath(t *testing.T) {
cases := []struct {
name string
input string
expected string
}{
{"simple path", "path", "path"},
{"leading slash", "/path", "path"},
{"trailing slash", "path/", "path"},
{"leading and trailing slash", "/path/", "path"},
{"nested with slashes", "//nested/path//", "nested/path"},
{"empty string", "", ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
got := getEndpointPath(c.input)
assert.Equal(t, c.expected, got)
})
}
}
func TestEmptyResolutions(t *testing.T) {
// Get validator engine
v, ok := binding.Validator.Engine().(*validator.Validate)
if !ok {
t.Fatal("could not get validator engine")
}
// Test with empty resolutions slice
cpp := CoursePhaseParticipationsWithResolutions{
Participations: []promptTypes.CoursePhaseParticipationWithStudent{},
Resolutions: []Resolution{},
}
// Empty slice should be valid since dive only applies to elements within the slice
err := v.Struct(cpp)
assert.NoError(t, err)
}
func TestNilResolutions(t *testing.T) {
// Get validator engine
v, ok := binding.Validator.Engine().(*validator.Validate)
if !ok {
t.Fatal("could not get validator engine")
}
// Test with nil resolutions slice
cpp := CoursePhaseParticipationsWithResolutions{
Participations: []promptTypes.CoursePhaseParticipationWithStudent{},
Resolutions: nil,
}
// Nil slice should be valid since dive only applies to elements within the slice
err := v.Struct(cpp)
assert.NoError(t, err)
}
func TestBuildURL_NoExtraPaths(t *testing.T) {
id := uuid.MustParse("123e4567-e89b-12d3-a456-426614174000")
res := Resolution{
BaseURL: "https://example-prompt.com/api",
CoursePhaseID: id,
EndpointPath: "/my-endpoint/",
}
got := buildURL(res)
want := "https://example-prompt.com/api/course_phase/123e4567-e89b-12d3-a456-426614174000/my-endpoint"
assert.Equal(t, want, got)
}
func TestBuildURL_WithExtraPaths(t *testing.T) {
id := uuid.MustParse("00000000-0000-0000-0000-000000000000")
res := Resolution{
BaseURL: "http://localhost:8080/v1",
CoursePhaseID: id,
EndpointPath: "endpoint",
}
got := buildURL(res, "p1", "details")
want := "http://localhost:8080/v1/course_phase/00000000-0000-0000-0000-000000000000/endpoint/p1/details"
assert.Equal(t, want, got)
}
func TestBuildURL_WithInvalidBaseURL(t *testing.T) {
// Test with an invalid URL that would cause issues
res := Resolution{
BaseURL: ":%invalid",
CoursePhaseID: uuid.New(),
EndpointPath: "endpoint",
}
got := buildURL(res)
// Verify the function gracefully handles invalid URLs
assert.Empty(t, got)
}