refactor(context): refactor context handling and improve test robustness

- Use `assert.InDelta` for float comparison with tolerance in `TestContextGetFloat32`
- Remove unnecessary blank line in `TestContextInitQueryCache`
- Replace anonymous struct with named `contextKey` type in `TestContextWithFallbackValueFromRequestContext`
- Update context key handling in `TestContextWithFallbackValueFromRequestContext` to use `contextKey` type

Signed-off-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
appleboy 2024-10-25 09:13:22 +08:00
parent b080116a7f
commit b6c7ebd5f0
No known key found for this signature in database

View File

@ -323,7 +323,7 @@ func TestContextGetFloat32(t *testing.T) {
key := "float32"
value := float32(3.14)
c.Set(key, value)
assert.Equal(t, value, c.GetFloat32(key))
assert.InDelta(t, value, c.GetFloat32(key), 0.0001)
}
func TestContextGetFloat64(t *testing.T) {
@ -610,7 +610,6 @@ func TestContextInitQueryCache(t *testing.T) {
assert.Equal(t, test.expectedQueryCache, test.testContext.queryCache)
})
}
}
func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
@ -2857,13 +2856,13 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
{
name: "c with struct context key",
getContextAndKey: func() (*Context, any) {
var key struct{}
type contextKey struct{}
c, _ := CreateTestContext(httptest.NewRecorder())
// enable ContextWithFallback feature flag
c.engine.ContextWithFallback = true
c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request = c.Request.WithContext(context.WithValue(context.TODO(), key, "value"))
return c, key
c.Request = c.Request.WithContext(context.WithValue(context.TODO(), contextKey{}, "value"))
return c, contextKey(contextKey{})
},
value: "value",
},