diff --git a/test/60rescache_legacy_test.go b/test/60rescache_legacy_test.go new file mode 100644 index 0000000..948a38e --- /dev/null +++ b/test/60rescache_legacy_test.go @@ -0,0 +1,142 @@ +package test + +import ( + "encoding/json" + "fmt" + "reflect" + "testing" + + "github.com/resgateio/resgate/server/codec" + "github.com/resgateio/resgate/server/rescache" +) + +func TestLegacy120Model_MarshalJSON_ReturnsSoftReferenceAsString(t *testing.T) { + var v map[string]codec.Value + dta := []byte(`{"name":"softparent","child":{"rid":"test.model","soft":true}}`) + expected := []byte(`{"name":"softparent","child":"test.model"}`) + err := json.Unmarshal(dta, &v) + if err != nil { + t.Fatal(err) + } + m := &rescache.Model{Values: v} + lm := (*rescache.Legacy120Model)(m) + + out, err := lm.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + AssertEqualJSON(t, "Legacy120Model.MarshalJSON", json.RawMessage(out), json.RawMessage(expected)) +} + +func TestLegacy120Model_MarshalJSON_ReturnsDataValuePlaceholder(t *testing.T) { + var v map[string]codec.Value + dta := []byte(`{"name":"data","primitive":{"data":12},"object":{"data":{"foo":["bar"]}},"array":{"data":[{"foo":"bar"}]}}`) + expected := []byte(`{"name":"data","primitive":12,"object":"[Data]","array":"[Data]"}`) + err := json.Unmarshal(dta, &v) + if err != nil { + t.Fatal(err) + } + m := &rescache.Model{Values: v} + lm := (*rescache.Legacy120Model)(m) + + out, err := lm.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + AssertEqualJSON(t, "Legacy120Model.MarshalJSON", json.RawMessage(out), json.RawMessage(expected)) +} + +func TestLegacy120Collection_MarshalJSON_ReturnsSoftReferenceAsString(t *testing.T) { + var v []codec.Value + dta := []byte(`["softparent",{"rid":"test.model","soft":true}]`) + expected := []byte(`["softparent","test.model"]`) + err := json.Unmarshal(dta, &v) + if err != nil { + t.Fatal(err) + } + m := &rescache.Collection{Values: v} + lm := (*rescache.Legacy120Collection)(m) + + out, err := lm.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + AssertEqualJSON(t, "Legacy120Collection.MarshalJSON", json.RawMessage(out), json.RawMessage(expected)) +} + +func TestLegacy120Collection_MarshalJSON_ReturnsDataValuePlaceholder(t *testing.T) { + var v []codec.Value + dta := []byte(`["data",{"data":12},{"data":{"foo":["bar"]}},{"data":[{"foo":"bar"}]}]`) + expected := []byte(`["data",12,"[Data]","[Data]"]`) + err := json.Unmarshal(dta, &v) + if err != nil { + t.Fatal(err) + } + m := &rescache.Collection{Values: v} + lm := (*rescache.Legacy120Collection)(m) + + out, err := lm.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + AssertEqualJSON(t, "Legacy120Collection.MarshalJSON", json.RawMessage(out), json.RawMessage(expected)) +} + +func TestLegacy120Value_MarshalJSON_ReturnsSoftReferenceAsString(t *testing.T) { + var v codec.Value + dta := []byte(`{"rid":"test.model","soft":true}`) + expected := []byte(`"test.model"`) + err := json.Unmarshal(dta, &v) + if err != nil { + t.Fatal(err) + } + lv := rescache.Legacy120Value(v) + + out, err := lv.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + AssertEqualJSON(t, "Legacy120Value.MarshalJSON", json.RawMessage(out), json.RawMessage(expected)) +} + +// AssertEqualJSON expects that a and b json marshals into equal values, and +// returns true if they do, otherwise logs a fatal error and returns false. +func AssertEqualJSON(t *testing.T, name string, result, expected interface{}, ctx ...interface{}) bool { + aa, aj := jsonMap(t, result) + bb, bj := jsonMap(t, expected) + + if !reflect.DeepEqual(aa, bb) { + t.Fatalf("expected %s to be:\n\t%s\nbut got:\n\t%s%s", name, bj, aj, ctxString(ctx)) + return false + } + + return true +} + +func ctxString(ctx []interface{}) string { + if len(ctx) == 0 { + return "" + } + return "\nin " + fmt.Sprint(ctx...) +} + +func jsonMap(t *testing.T, v interface{}) (interface{}, []byte) { + var err error + j, err := json.Marshal(v) + if err != nil { + t.Fatalf("test: error marshaling value:\n\t%+v\nerror:\n\t%s", v, err) + } + + var m interface{} + err = json.Unmarshal(j, &m) + if err != nil { + t.Fatalf("test: error unmarshaling value:\n\t%s\nerror:\n\t%s", j, err) + } + + return m, j +} diff --git a/test/61rescache_resourcepattern_test.go b/test/61rescache_resourcepattern_test.go new file mode 100644 index 0000000..81bf957 --- /dev/null +++ b/test/61rescache_resourcepattern_test.go @@ -0,0 +1,149 @@ +package test + +import ( + "testing" + + "github.com/resgateio/resgate/server/rescache" +) + +func TestParseResourcePattern_WithValidPatterns_IsValidReturnsTrue(t *testing.T) { + tbl := []struct { + Pattern string + }{ + {"test"}, + {"test.model"}, + {"test.model.foo"}, + {"test$.model"}, + {"$test.model"}, + + {">"}, + {"test.>"}, + {"test.model.>"}, + + {"*"}, + {"test.*"}, + {"*.model"}, + {"test.*.foo"}, + {"test.model.*"}, + {"*.model.foo"}, + {"test.*.*"}, + + {"test.*.>"}, + } + + for _, r := range tbl { + if !rescache.ParseResourcePattern(r.Pattern).IsValid() { + t.Errorf("ResourcePattern(%#v).IsValid() did not return true", r.Pattern) + } + } +} + +func TestParseResourcePattern_WithInvalidPatterns_IsValidReturnsFalse(t *testing.T) { + tbl := []struct { + Pattern string + }{ + {""}, + {"."}, + {".test"}, + {"test."}, + {"test..foo"}, + + {"*test"}, + {"test*"}, + {"test.*foo"}, + {"test.foo*"}, + {"test.**.foo"}, + + {">test"}, + {"test>"}, + {"test.>foo"}, + {"test.foo>"}, + {"test.>.foo"}, + + {"test.$foo>"}, + {"test.$foo*"}, + + {"test.foo?"}, + {"test. .foo"}, + {"test.\x127.foo"}, + {"test.räv"}, + } + + for _, r := range tbl { + if rescache.ParseResourcePattern(r.Pattern).IsValid() { + t.Errorf("ResourcePattern(%#v).IsValid() did not return false", r.Pattern) + } + } +} + +func TestResourcePatternMatche_MatchingPattern_ReturnsTrue(t *testing.T) { + tbl := []struct { + Pattern string + ResourceName string + }{ + {"test", "test"}, + {"test.model", "test.model"}, + {"test.model.foo", "test.model.foo"}, + {"test$.model", "test$.model"}, + {"$foo.model", "$foo.model"}, + + {">", "test.model.foo"}, + {"test.>", "test.model.foo"}, + {"test.model.>", "test.model.foo"}, + + {"*", "test"}, + {"test.*", "test.model"}, + {"*.model", "test.model"}, + {"test.*.foo", "test.model.foo"}, + {"test.model.*", "test.model.foo"}, + {"*.model.foo", "test.model.foo"}, + {"test.*.*", "test.model.foo"}, + + {"test.*.>", "test.model.foo"}, + } + + for _, r := range tbl { + if !rescache.ParseResourcePattern(r.Pattern).Match(r.ResourceName) { + t.Errorf("ParseResourcePattern(%#v).Matches(%#v) did not return true", r.Pattern, r.ResourceName) + } + } +} + +func TestResourcePatternMatche_NonMatchingPattern_ReturnsFalse(t *testing.T) { + tbl := []struct { + Pattern string + ResourceName string + }{ + {"", ""}, + {"", "test"}, + {"test", "test.model"}, + {"test.model", "test.mode"}, + {"test.model.foo", "test.model"}, + {"test.model.foo", "test.mode.foo"}, + {"test", "testing"}, + {"testing", "test"}, + {"foo", "bar"}, + + {">", ""}, + {"test.>", "test"}, + {"test.model.>", "test.model"}, + + {"*", "test.model"}, + {"test.*", "test.model.foo"}, + {"*.model", "test"}, + {"test.*.foo", "test.model"}, + {"test.model.*", "test.model"}, + {"*.model.foo", "test.model"}, + {"test.*.*", "test.model"}, + {"foo.*", "bar.model"}, + {"*.bar", "foo.baz"}, + + {"test.*.>", "test.model"}, + } + + for _, r := range tbl { + if rescache.ParseResourcePattern(r.Pattern).Match(r.ResourceName) { + t.Errorf("ParseResourcePattern(%#v).Match(%#v) did not return false", r.Pattern, r.ResourceName) + } + } +}