Skip to content

Commit

Permalink
test CacheResult
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Sep 2, 2023
1 parent c796957 commit 76b3f82
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions env_generic_sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
54 changes: 54 additions & 0 deletions env_generic_sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{}
Expand Down
53 changes: 52 additions & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fixenv
import (
"errors"
"github.com/rekby/fixenv/internal"
"math/rand"
"runtime"
"sync"
"testing"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down Expand Up @@ -114,6 +116,17 @@ type CacheOptions struct {
additionlSkipExternalCalls int
}

func cacheOptionsFromFixtureOptionsAndCacheKey(fixtureOptions *FixtureOptions, cacheKey interface{}) CacheOptions {
if fixtureOptions == nil {
fixtureOptions = &FixtureOptions{}
}
return CacheOptions{
Scope: fixtureOptions.Scope,
CacheKey: cacheKey,
additionlSkipExternalCalls: fixtureOptions.additionlSkipExternalCalls,
}
}

// T is subtype of testing.TB
type T interface {
// Cleanup registers a function to be called when the test (or subtest) and all its subtests complete.
Expand Down

0 comments on commit 76b3f82

Please sign in to comment.