Skip to content

Commit 2427ac1

Browse files
committed
increase test coverage of ContextProvider
1 parent 6e93f9d commit 2427ac1

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

platform/data/contextProvider_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package data
22

33
import (
44
"context"
5+
"net/http"
56
"testing"
67

78
"github.com/robbyt/go-polyscript/platform/constants"
@@ -316,6 +317,130 @@ func TestContextProvider_AddDataToContext(t *testing.T) {
316317
})
317318
}
318319

320+
// TestContextProvider_ProcessValue tests the processValue function directly
321+
func TestContextProvider_ProcessValue(t *testing.T) {
322+
t.Parallel()
323+
324+
t.Run("nil value", func(t *testing.T) {
325+
provider := NewContextProvider(constants.EvalData)
326+
result, err := provider.processValue(nil)
327+
328+
assert.NoError(t, err, "Should not error for nil value")
329+
assert.Nil(t, result, "Result should be nil")
330+
})
331+
332+
t.Run("primitive types", func(t *testing.T) {
333+
provider := NewContextProvider(constants.EvalData)
334+
335+
// Test string
336+
result, err := provider.processValue("test string")
337+
assert.NoError(t, err)
338+
assert.Equal(t, "test string", result)
339+
340+
// Test number
341+
result, err = provider.processValue(42)
342+
assert.NoError(t, err)
343+
assert.Equal(t, 42, result)
344+
345+
// Test boolean
346+
result, err = provider.processValue(true)
347+
assert.NoError(t, err)
348+
assert.Equal(t, true, result)
349+
350+
// Test slice
351+
slice := []string{"one", "two"}
352+
result, err = provider.processValue(slice)
353+
assert.NoError(t, err)
354+
assert.Equal(t, slice, result)
355+
})
356+
357+
t.Run("nil http request pointer", func(t *testing.T) {
358+
provider := NewContextProvider(constants.EvalData)
359+
var nilReq *http.Request = nil
360+
361+
result, err := provider.processValue(nilReq)
362+
assert.NoError(t, err, "Should not error for nil HTTP request")
363+
assert.Nil(t, result, "Result should be nil")
364+
})
365+
366+
t.Run("http request", func(t *testing.T) {
367+
provider := NewContextProvider(constants.EvalData)
368+
req := createTestRequestHelper()
369+
370+
// Test *http.Request
371+
result, err := provider.processValue(req)
372+
assert.NoError(t, err)
373+
resultMap, ok := result.(map[string]any)
374+
assert.True(t, ok, "Result should be a map")
375+
assert.Equal(t, "GET", resultMap["Method"])
376+
377+
// Test http.Request (value)
378+
result, err = provider.processValue(*req)
379+
assert.NoError(t, err)
380+
resultMap, ok = result.(map[string]any)
381+
assert.True(t, ok, "Result should be a map")
382+
assert.Equal(t, "GET", resultMap["Method"])
383+
})
384+
385+
t.Run("map with empty key", func(t *testing.T) {
386+
provider := NewContextProvider(constants.EvalData)
387+
mapWithEmptyKey := map[string]any{
388+
"": "value for empty key",
389+
"valid": "value",
390+
}
391+
392+
_, err := provider.processValue(mapWithEmptyKey)
393+
assert.Error(t, err, "Should reject maps with empty keys")
394+
assert.Contains(t, err.Error(), "empty keys are not allowed")
395+
})
396+
397+
t.Run("deeply nested map", func(t *testing.T) {
398+
provider := NewContextProvider(constants.EvalData)
399+
nestedMap := map[string]any{
400+
"level1": map[string]any{
401+
"level2": map[string]any{
402+
"level3": map[string]any{
403+
"value": 42,
404+
},
405+
},
406+
},
407+
}
408+
409+
result, err := provider.processValue(nestedMap)
410+
assert.NoError(t, err)
411+
412+
// Navigate through the levels to verify all maps were processed
413+
resultMap, ok := result.(map[string]any)
414+
assert.True(t, ok)
415+
416+
level1, ok := resultMap["level1"].(map[string]any)
417+
assert.True(t, ok)
418+
419+
level2, ok := level1["level2"].(map[string]any)
420+
assert.True(t, ok)
421+
422+
level3, ok := level2["level3"].(map[string]any)
423+
assert.True(t, ok)
424+
425+
assert.Equal(t, 42, level3["value"])
426+
})
427+
428+
t.Run("nested map with error in deeper level", func(t *testing.T) {
429+
provider := NewContextProvider(constants.EvalData)
430+
problematicMap := map[string]any{
431+
"level1": map[string]any{
432+
"level2": map[string]any{
433+
"": "this key is empty and should cause an error",
434+
},
435+
},
436+
}
437+
438+
_, err := provider.processValue(problematicMap)
439+
assert.Error(t, err)
440+
assert.Contains(t, err.Error(), "empty keys are not allowed")
441+
})
442+
}
443+
319444
// TestContextProvider_DataIntegration tests more complex data scenarios
320445
func TestContextProvider_DataIntegration(t *testing.T) {
321446
t.Parallel()

0 commit comments

Comments
 (0)