MCPcopy Create free account
hub / github.com/conforma/cli / TestWithPolicyCache

Function TestWithPolicyCache

internal/policy/cache/cache_test.go:269–328  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

267}
268
269func TestWithPolicyCache(t *testing.T) {
270 tests := []struct {
271 name string
272 setupContext func() context.Context
273 setupCache func() *PolicyCache
274 expectOriginal bool
275 expectNew bool
276 }{
277 {
278 name: "add cache to empty context",
279 setupContext: func() context.Context {
280 return context.Background()
281 },
282 setupCache: func() *PolicyCache {
283 cache := NewPolicyCache(context.Background())
284 return cache
285 },
286 expectOriginal: false,
287 expectNew: true,
288 },
289 {
290 name: "add cache to context with existing cache",
291 setupContext: func() context.Context {
292 ctx := context.Background()
293 existingCache := NewPolicyCache(ctx)
294 return WithPolicyCache(ctx, existingCache)
295 },
296 setupCache: func() *PolicyCache {
297 newCache := NewPolicyCache(context.Background())
298 return newCache
299 },
300 expectOriginal: true,
301 expectNew: true,
302 },
303 }
304
305 for _, tt := range tests {
306 t.Run(tt.name, func(t *testing.T) {
307 originalCtx := tt.setupContext()
308 testCache := tt.setupCache()
309
310 // Execute test
311 newCtx := WithPolicyCache(originalCtx, testCache)
312
313 // Verify cache is in the new context
314 retrievedCache, found := PolicyCacheFromContext(newCtx)
315 assert.Equal(t, tt.expectNew, found)
316 if tt.expectNew {
317 assert.Equal(t, testCache, retrievedCache)
318 }
319
320 // Verify original context is unchanged
321 originalCache, found := PolicyCacheFromContext(originalCtx)
322 assert.Equal(t, tt.expectOriginal, found)
323 if !tt.expectOriginal {
324 assert.Nil(t, originalCache)
325 }
326 })

Callers

nothing calls this directly

Calls 4

NewPolicyCacheFunction · 0.85
WithPolicyCacheFunction · 0.85
PolicyCacheFromContextFunction · 0.85
RunMethod · 0.65

Tested by

no test coverage detected