1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/tool/kratos-gen-bts/testdata/none_test.go
jiankuny 0ffb6233bc
add bts-gen & mc-gen (#96)
add genbts & genmc
2019-05-14 13:57:02 +08:00

51 lines
1.4 KiB
Go

package testdata
import (
"context"
"errors"
"testing"
)
func TestNoneCache(t *testing.T) {
d := New()
meta := &Demo{ID: 1}
getFromCache := func(c context.Context) (*Demo, error) { return meta, nil }
notGetFromCache := func(c context.Context) (*Demo, error) { return nil, errors.New("err") }
getFromSource := func(c context.Context) (*Demo, error) { return meta, nil }
notGetFromSource := func(c context.Context) (*Demo, error) { return meta, errors.New("err") }
addToCache := func(c context.Context, values *Demo) error { return nil }
// get from cache
_noneCacheFunc = getFromCache
_noneRawFunc = notGetFromSource
_noneAddCacheFunc = addToCache
res, err := d.None(context.TODO())
if err != nil {
t.Fatalf("err should be nil, get: %v", err)
}
if res.ID != 1 {
t.Fatalf("id should be 1")
}
// get from source
_noneCacheFunc = notGetFromCache
_noneRawFunc = getFromSource
res, err = d.None(context.TODO())
if err != nil {
t.Fatalf("err should be nil, get: %v", err)
}
if res.ID != 1 {
t.Fatalf("id should be 1")
}
// with null cache
nullCache := &Demo{ID: -1}
getNullFromCache := func(c context.Context) (*Demo, error) { return nullCache, nil }
_noneCacheFunc = getNullFromCache
_noneRawFunc = notGetFromSource
res, err = d.None(context.TODO())
if err != nil {
t.Fatalf("err should be nil, get: %v", err)
}
if res != nil {
t.Fatalf("res should be nil")
}
}