From 42c94aeeb0da3a2685fd98970ba8a47c4c58b048 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Sun, 3 Sep 2023 01:19:38 +0300 Subject: [PATCH] test CacheResult --- env_generic_sugar.go | 2 ++ env_generic_sugar_test.go | 54 +++++++++++++++++++++++++++++++++++++++ env_test.go | 53 +++++++++++++++++++++++++++++++++++++- go.sum | 1 + interface.go | 2 ++ 5 files changed, 111 insertions(+), 1 deletion(-) diff --git a/env_generic_sugar.go b/env_generic_sugar.go index 5995e40..0594e9a 100644 --- a/env_generic_sugar.go +++ b/env_generic_sugar.go @@ -7,6 +7,7 @@ import "fmt" // Cache is call f once per cache scope (default per test) and cache result (success or error). // All other calls of the f will return same result +// Deprecated: Use CacheResult func Cache[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, error)) TRes { addSkipLevel(&opt) callbackResult := env.Cache(cacheKey, opt, func() (res interface{}, err error) { @@ -23,6 +24,7 @@ func Cache[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, // CacheWithCleanup is call f once per cache scope (default per test) and cache result (success or error). // All other calls of the f will return same result. // Used when fixture need own cleanup after exit from test scope +// Deprecated: Use CacheResult func CacheWithCleanup[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes { addSkipLevel(&opt) callbackResult := env.CacheWithCleanup(cacheKey, opt, func() (res interface{}, cleanup FixtureCleanupFunc, err error) { diff --git a/env_generic_sugar_test.go b/env_generic_sugar_test.go index a49a939..78a826e 100644 --- a/env_generic_sugar_test.go +++ b/env_generic_sugar_test.go @@ -6,6 +6,8 @@ package fixenv import ( "fmt" "github.com/rekby/fixenv/internal" + "github.com/stretchr/testify/assert" + "math/rand" "testing" "github.com/stretchr/testify/require" @@ -91,6 +93,7 @@ func TestCacheWithCleanupGeneric(t *testing.T) { require.Equal(t, 2, f2()) }) } + func TestCacheResultGeneric(t *testing.T) { t.Run("PassParams", func(t *testing.T) { inOpt := CacheOptions{ @@ -136,6 +139,57 @@ func TestCacheResultGeneric(t *testing.T) { }) } +func TestCacheResultPanic(t *testing.T) { + t.Run("Simple", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env) int { + return CacheResult(e, func() (*GenericResult[int], error) { + return NewGenericResult(rand.Int()), nil + }) + } + first := rndFix(e) + second := rndFix(e) + + at.Equal(first, second) + }) + t.Run("Options", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env, name string) int { + return CacheResult(e, func() (*GenericResult[int], error) { + return NewGenericResult(rand.Int()), nil + }, CacheOptions{CacheKey: name}) + } + first1 := rndFix(e, "first") + first2 := rndFix(e, "first") + second1 := rndFix(e, "second") + second2 := rndFix(e, "second") + + at.Equal(first1, first2) + at.Equal(second1, second2) + at.NotEqual(first1, second1) + }) + t.Run("Panic", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env, name string) int { + return CacheResult(e, func() (*GenericResult[int], error) { + return NewGenericResult(rand.Int()), nil + }, CacheOptions{CacheKey: name}, CacheOptions{CacheKey: name}) + } + at.Panics(func() { + rndFix(e, "first") + }) + }) +} + type envMock struct { onCache func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} onCacheWithCleanup func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} diff --git a/env_test.go b/env_test.go index 6457fc3..fc45a4b 100644 --- a/env_test.go +++ b/env_test.go @@ -3,6 +3,7 @@ package fixenv import ( "errors" "github.com/rekby/fixenv/internal" + "math/rand" "runtime" "sync" "testing" @@ -301,6 +302,57 @@ func Test_Env_CacheWithCleanup(t *testing.T) { }) } +func Test_Env_CacheResult(t *testing.T) { + t.Run("Simple", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env) int { + return e.CacheResult(func() (*Result, error) { + return NewResult(rand.Int()), nil + }).(int) + } + first := rndFix(e) + second := rndFix(e) + + at.Equal(first, second) + }) + t.Run("Options", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env, name string) int { + return e.CacheResult(func() (*Result, error) { + return NewResult(rand.Int()), nil + }, CacheOptions{CacheKey: name}).(int) + } + first1 := rndFix(e, "first") + first2 := rndFix(e, "first") + second1 := rndFix(e, "second") + second2 := rndFix(e, "second") + + at.Equal(first1, first2) + at.Equal(second1, second2) + at.NotEqual(first1, second1) + }) + t.Run("Panic", func(t *testing.T) { + at := assert.New(t) + tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true} + e := New(tMock) + + rndFix := func(e Env, name string) int { + return e.CacheResult(func() (*Result, error) { + return NewResult(rand.Int()), nil + }, CacheOptions{CacheKey: name}, CacheOptions{CacheKey: name}).(int) + } + at.Panics(func() { + rndFix(e, "first") + }) + }) +} + func Test_FixtureWrapper(t *testing.T) { t.Run("ok", func(t *testing.T) { at := assert.New(t) @@ -651,7 +703,6 @@ func TestNewEnv(t *testing.T) { tm.SkipGoexit = true New(tm) - //goland:noinspection GoDeprecation NewEnv(tm) if len(tm.Fatals) == 0 { t.Fatal("bad double login between new and NewEnv") diff --git a/go.sum b/go.sum index acb88a4..26500d5 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/interface.go b/interface.go index 97555d7..a171b3b 100644 --- a/interface.go +++ b/interface.go @@ -14,11 +14,13 @@ type Env interface { // Cache result of f calls // f call exactly once for every combination of scope and params // params must be json serializable (deserialize not need) + // Deprecated: Use CacheResult Cache(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} // CacheWithCleanup cache result of f calls // f call exactly once for every combination of scope and params // params must be json serializable (deserialize not need) + // Deprecated: Use CacheResult CacheWithCleanup(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} }