From 8868024dc2bd0585cd19797abf307df68efbd5c1 Mon Sep 17 00:00:00 2001 From: Henry Jewell Date: Fri, 12 Apr 2024 16:45:34 +0100 Subject: [PATCH 1/2] roundtrip and error tests for jsonresource Introduced the miniredis package for helping us unit test Redis code properly. re AB#9351 --- go.mod | 3 ++ go.sum | 7 ++++ redis/jsonresource_test.go | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 redis/jsonresource_test.go diff --git a/go.mod b/go.mod index d3f5d8f..4020ce3 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,8 @@ require ( require ( github.com/Azure/go-amqp v1.0.0 // indirect + github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect + github.com/alicebob/miniredis/v2 v2.32.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd v0.20.1-beta // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect @@ -64,6 +66,7 @@ require ( github.com/prometheus/procfs v0.6.0 // indirect github.com/shengdoushi/base58 v1.0.0 // indirect github.com/x448/float16 v0.8.4 // indirect + github.com/yuin/gopher-lua v1.1.1 // indirect ) require ( diff --git a/go.sum b/go.sum index 02b78f9..fc5bf36 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,10 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= +github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= +github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= +github.com/alicebob/miniredis/v2 v2.32.1 h1:Bz7CciDnYSaa0mX5xODh6GUITRSx+cVhjNoOR4JssBo= +github.com/alicebob/miniredis/v2 v2.32.1/go.mod h1:AqkLNAfUm0K07J28hnAyyQKf/x0YkCY/g5DCtuL01Mw= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= @@ -542,6 +546,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -664,6 +670,7 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/redis/jsonresource_test.go b/redis/jsonresource_test.go new file mode 100644 index 0000000..c618e84 --- /dev/null +++ b/redis/jsonresource_test.go @@ -0,0 +1,69 @@ +package redis + +import ( + "context" + "testing" + + "github.com/alicebob/miniredis/v2" + "github.com/datatrails/go-datatrails-common/logger" + "github.com/go-redis/redis/v8" + "github.com/stretchr/testify/require" +) + +// NewTestResource sets up a fresh instance of miniredis and returns a configured JsonResource +func NewTestResource(t *testing.T) JsonResource { + mr := miniredis.RunT(t) + c := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + return JsonResource{ + client: c, + ClientContext: ClientContext{ + cfg: &clusterConfig{}, + name: "test-json-resource", + }, + keyPrefix: "resource-prefix", + } +} + +type TestStruct struct { + Foo string + Bar int64 +} + +// TestRoundtrip ensures we can Get a previously Set value, marshalling and unmarshalling as needed. +func TestRoundtrip(t *testing.T) { + logger.New("NOOP") + defer logger.OnExit() + + resource := NewTestResource(t) + + setErr := resource.Set(context.TODO(), "tenant/1", &TestStruct{Foo: "hello world", Bar: 1337}) + require.NoError(t, setErr) + + result := &TestStruct{} + getErr := resource.Get(context.TODO(), "tenant/1", result) + require.NoError(t, getErr) + + require.Equal(t, "hello world", result.Foo) + require.Equal(t, int64(1337), result.Bar) +} + +// TestExpectedUnmarshalError ensures that Get errors if it cannot unmarshal into the provided +// struct. +func TestExpectedUnmarshalError(t *testing.T) { + logger.New("NOOP") + defer logger.OnExit() + + resource := NewTestResource(t) + + setErr := resource.Set(context.TODO(), "tenant/1", &TestStruct{Foo: "hello world", Bar: 1337}) + require.NoError(t, setErr) + + type OtherTestStruct struct { + Foo int64 + Bar string + } + + result := &OtherTestStruct{} + getErr := resource.Get(context.TODO(), "tenant/1", result) + require.Error(t, getErr) +} From 1a5ca5f8f2753bf2cb32fd8ca750936e83c2fd63 Mon Sep 17 00:00:00 2001 From: Henry Jewell Date: Mon, 15 Apr 2024 16:53:02 +0100 Subject: [PATCH 2/2] fixup! roundtrip and error tests for jsonresource --- redis/jsonresource_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/redis/jsonresource_test.go b/redis/jsonresource_test.go index c618e84..d877539 100644 --- a/redis/jsonresource_test.go +++ b/redis/jsonresource_test.go @@ -11,10 +11,10 @@ import ( ) // NewTestResource sets up a fresh instance of miniredis and returns a configured JsonResource -func NewTestResource(t *testing.T) JsonResource { +func NewTestResource(t *testing.T) *JsonResource { mr := miniredis.RunT(t) c := redis.NewClient(&redis.Options{Addr: mr.Addr()}) - return JsonResource{ + return &JsonResource{ client: c, ClientContext: ClientContext{ cfg: &clusterConfig{}, @@ -39,8 +39,8 @@ func TestRoundtrip(t *testing.T) { setErr := resource.Set(context.TODO(), "tenant/1", &TestStruct{Foo: "hello world", Bar: 1337}) require.NoError(t, setErr) - result := &TestStruct{} - getErr := resource.Get(context.TODO(), "tenant/1", result) + result := TestStruct{} + getErr := resource.Get(context.TODO(), "tenant/1", &result) require.NoError(t, getErr) require.Equal(t, "hello world", result.Foo) @@ -63,7 +63,7 @@ func TestExpectedUnmarshalError(t *testing.T) { Bar string } - result := &OtherTestStruct{} - getErr := resource.Get(context.TODO(), "tenant/1", result) + result := OtherTestStruct{} + getErr := resource.Get(context.TODO(), "tenant/1", &result) require.Error(t, getErr) }