Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
506 lines (391 loc) · 11.4 KB
/
Copy pathcontext_test.go
File metadata and controls
506 lines (391 loc) · 11.4 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package result
import (
"context"
"testing"
)
// ============================================================================
// Context Setter Tests
// ============================================================================
func TestWithRequestID(t *testing.T) {
ctx := context.Background()
requestID := "req_123"
ctx = WithRequestID(ctx, requestID)
got := GetRequestID(ctx)
if got != requestID {
t.Errorf("GetRequestID() = %v, want %v", got, requestID)
}
}
func TestWithTraceID(t *testing.T) {
ctx := context.Background()
traceID := "trace_xyz"
ctx = WithTraceID(ctx, traceID)
got := GetTraceID(ctx)
if got != traceID {
t.Errorf("GetTraceID() = %v, want %v", got, traceID)
}
}
func TestWithSpanID(t *testing.T) {
ctx := context.Background()
spanID := "span_abc"
ctx = WithSpanID(ctx, spanID)
got := GetSpanID(ctx)
if got != spanID {
t.Errorf("GetSpanID() = %v, want %v", got, spanID)
}
}
func TestWithUserID(t *testing.T) {
ctx := context.Background()
userID := "user_456"
ctx = WithUserID(ctx, userID)
got := GetUserID(ctx)
if got != userID {
t.Errorf("GetUserID() = %v, want %v", got, userID)
}
}
func TestWithTenantID(t *testing.T) {
ctx := context.Background()
tenantID := "tenant_789"
ctx = WithTenantID(ctx, tenantID)
got := GetTenantID(ctx)
if got != tenantID {
t.Errorf("GetTenantID() = %v, want %v", got, tenantID)
}
}
// ============================================================================
// Context Getter Tests
// ============================================================================
func TestGetRequestID_NotPresent(t *testing.T) {
ctx := context.Background()
got := GetRequestID(ctx)
if got != "" {
t.Errorf("GetRequestID() = %v, want empty string", got)
}
}
func TestGetTraceID_NotPresent(t *testing.T) {
ctx := context.Background()
got := GetTraceID(ctx)
if got != "" {
t.Errorf("GetTraceID() = %v, want empty string", got)
}
}
func TestGetSpanID_NotPresent(t *testing.T) {
ctx := context.Background()
got := GetSpanID(ctx)
if got != "" {
t.Errorf("GetSpanID() = %v, want empty string", got)
}
}
func TestGetUserID_NotPresent(t *testing.T) {
ctx := context.Background()
got := GetUserID(ctx)
if got != "" {
t.Errorf("GetUserID() = %v, want empty string", got)
}
}
func TestGetTenantID_NotPresent(t *testing.T) {
ctx := context.Background()
got := GetTenantID(ctx)
if got != "" {
t.Errorf("GetTenantID() = %v, want empty string", got)
}
}
func TestGetRequestID_WrongType(t *testing.T) {
// Test that wrong type returns empty string (doesn't panic)
ctx := context.WithValue(context.Background(), requestIDKey, 123)
got := GetRequestID(ctx)
if got != "" {
t.Errorf("GetRequestID() with wrong type = %v, want empty string", got)
}
}
// ============================================================================
// Multiple Context Values
// ============================================================================
func TestMultipleContextValues(t *testing.T) {
ctx := context.Background()
ctx = WithRequestID(ctx, "req_123")
ctx = WithTraceID(ctx, "trace_xyz")
ctx = WithUserID(ctx, "user_456")
ctx = WithTenantID(ctx, "tenant_789")
if GetRequestID(ctx) != "req_123" {
t.Error("Failed to get request_id")
}
if GetTraceID(ctx) != "trace_xyz" {
t.Error("Failed to get trace_id")
}
if GetUserID(ctx) != "user_456" {
t.Error("Failed to get user_id")
}
if GetTenantID(ctx) != "tenant_789" {
t.Error("Failed to get tenant_id")
}
}
func TestContextChaining(t *testing.T) {
ctx := WithRequestID(
WithTraceID(
WithUserID(context.Background(), "user_1"),
"trace_1",
),
"req_1",
)
if GetRequestID(ctx) != "req_1" {
t.Error("Chained context should have request_id")
}
if GetTraceID(ctx) != "trace_1" {
t.Error("Chained context should have trace_id")
}
if GetUserID(ctx) != "user_1" {
t.Error("Chained context should have user_id")
}
}
// ============================================================================
// Result.WithContext Tests
// ============================================================================
func TestResultWithContext_Ok(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
r := Ok(42).WithContext(ctx)
if !r.IsOk() {
t.Error("WithContext should preserve Ok result")
}
// Ok result should not have metadata attached
if r.meta != nil {
t.Error("WithContext should not attach metadata to Ok result")
}
}
func TestResultWithContext_Err(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
r := Err[int](NotFound("test", "resource")).
WithContext(ctx)
if !r.IsErr() {
t.Error("WithContext should preserve Err result")
}
if r.meta == nil {
t.Fatal("WithContext should attach metadata to Err result")
}
if r.meta["request_id"] != "req_123" {
t.Errorf("metadata[request_id] = %v, want req_123", r.meta["request_id"])
}
}
func TestResultWithContext_MultipleIDs(t *testing.T) {
ctx := context.Background()
ctx = WithRequestID(ctx, "req_123")
ctx = WithTraceID(ctx, "trace_xyz")
ctx = WithUserID(ctx, "user_456")
ctx = WithTenantID(ctx, "tenant_789")
r := Err[int](NotFound("test", "resource")).
WithContext(ctx)
if r.meta["request_id"] != "req_123" {
t.Error("Should attach request_id")
}
if r.meta["trace_id"] != "trace_xyz" {
t.Error("Should attach trace_id")
}
if r.meta["user_id"] != "user_456" {
t.Error("Should attach user_id")
}
if r.meta["tenant_id"] != "tenant_789" {
t.Error("Should attach tenant_id")
}
}
func TestResultWithContext_EmptyContext(t *testing.T) {
ctx := context.Background()
r := Err[int](NotFound("test", "resource")).
WithContext(ctx)
if r.meta != nil {
hasValues := false
for range r.meta {
hasValues = true
break
}
if hasValues {
t.Error("WithContext should not add empty context values")
}
}
}
func TestResultWithContext_PartialContext(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
// Only request_id set, others empty
r := Err[int](NotFound("test", "resource")).
WithContext(ctx)
if r.meta["request_id"] != "req_123" {
t.Error("Should attach present request_id")
}
// Should not add empty keys
if _, exists := r.meta["trace_id"]; exists {
t.Error("Should not add empty trace_id")
}
if _, exists := r.meta["user_id"]; exists {
t.Error("Should not add empty user_id")
}
}
func TestResultWithContext_PreservesExistingMeta(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_from_context")
r := Err[int](NotFound("test", "resource")).
WithMeta("request_id", "req_explicit").
WithMeta("custom", "value").
WithContext(ctx)
// Should NOT override explicit request_id
if r.meta["request_id"] != "req_explicit" {
t.Error("WithContext should not override existing metadata")
}
// Should preserve other metadata
if r.meta["custom"] != "value" {
t.Error("WithContext should preserve existing metadata")
}
}
func TestResultWithContext_PreservesOp(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
r := Err[int](NotFound("test", "resource")).
WithOp("Service.GetUser").
WithContext(ctx)
if r.op != "Service.GetUser" {
t.Error("WithContext should preserve op")
}
if r.meta["request_id"] != "req_123" {
t.Error("WithContext should attach request_id")
}
}
// ============================================================================
// Result.WithContextOp Tests
// ============================================================================
func TestResultWithContextOp_Ok(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
r := Ok(42).WithContextOp(ctx, "Service.GetUser")
if !r.IsOk() {
t.Error("WithContextOp should preserve Ok result")
}
if r.op != "Service.GetUser" {
t.Error("WithContextOp should set op")
}
// Ok result should not have metadata
if r.meta != nil {
t.Error("WithContextOp should not attach metadata to Ok result")
}
}
func TestResultWithContextOp_Err(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_123")
r := Err[int](NotFound("test", "resource")).
WithContextOp(ctx, "Service.GetUser")
if !r.IsErr() {
t.Error("WithContextOp should preserve Err result")
}
if r.op != "Service.GetUser" {
t.Error("WithContextOp should set op")
}
if r.meta["request_id"] != "req_123" {
t.Error("WithContextOp should attach request_id")
}
}
func TestResultWithContextOp_CombinesOpAndContext(t *testing.T) {
ctx := context.Background()
ctx = WithRequestID(ctx, "req_123")
ctx = WithUserID(ctx, "user_456")
r := Err[int](NotFound("test", "resource")).
WithContextOp(ctx, "UserService.FindByID")
if r.op != "UserService.FindByID" {
t.Errorf("op = %v, want UserService.FindByID", r.op)
}
if r.meta["request_id"] != "req_123" {
t.Error("Should have request_id from context")
}
if r.meta["user_id"] != "user_456" {
t.Error("Should have user_id from context")
}
}
// ============================================================================
// Integration Tests
// ============================================================================
func TestContextPipeline(t *testing.T) {
ctx := context.Background()
ctx = WithRequestID(ctx, "req_pipeline")
ctx = WithUserID(ctx, "user_pipeline")
step1 := func(n int) Result[int] {
if n < 0 {
return Err[int](Validation("step1", "negative", nil)).
WithContext(ctx)
}
return Ok(n * 2)
}
r := Ok(10).
AndThen(step1).
AndThen(func(n int) Result[int] {
if n > 100 {
return Err[int](Domain("step2", "too large")).
WithContext(ctx)
}
return Ok(n + 5)
})
if !r.IsOk() {
t.Error("Pipeline should succeed")
}
if r.value != 25 {
t.Errorf("value = %v, want 25", r.value)
}
// Now test error path
r2 := Ok(-1).
AndThen(step1)
if !r2.IsErr() {
t.Error("Pipeline should fail on negative")
}
if r2.meta["request_id"] != "req_pipeline" {
t.Error("Error should have request_id from context")
}
if r2.meta["user_id"] != "user_pipeline" {
t.Error("Error should have user_id from context")
}
}
func TestContextWithRecovery(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_recovery")
metaChecked := false
r := Err[int](NotFound("test", "resource")).
WithContext(ctx).
InspectErr(func(err error) {
// We can check the Result's metadata before recovery
metaChecked = true
}).
Recover(KindNotFound, func(err error) int {
return 99
})
if !metaChecked {
t.Error("Should have inspected error")
}
if !r.IsOk() {
t.Error("Should recover from NotFound")
}
if r.value != 99 {
t.Errorf("value = %v, want 99", r.value)
}
}
func TestContextChainPreservation(t *testing.T) {
ctx := context.Background()
ctx = WithRequestID(ctx, "req_chain")
ctx = WithTraceID(ctx, "trace_chain")
r := Err[int](NotFound("test", "resource")).
WithContext(ctx).
Map(func(n int) int { return n * 2 }).
MapErr(func(err error) error {
return Wrap(err, "wrapper")
})
// Metadata should be preserved through transformations
if r.meta["request_id"] != "req_chain" {
t.Error("Metadata should be preserved through Map/MapErr")
}
if r.meta["trace_id"] != "trace_chain" {
t.Error("Metadata should be preserved through Map/MapErr")
}
}
func TestContextWithInspect(t *testing.T) {
ctx := WithRequestID(context.Background(), "req_inspect")
inspected := false
r := Err[int](NotFound("test", "resource")).
WithContext(ctx).
InspectErr(func(err error) {
inspected = true
})
if !inspected {
t.Error("InspectErr should be called")
}
// Check metadata on Result (not on the error)
if r.meta["request_id"] != "req_inspect" {
t.Error("Result should have context metadata")
}
}