Skip to content

Commit

Permalink
GH-257: Amended two test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Jul 3, 2024
1 parent 01042d4 commit 342dc4f
Show file tree
Hide file tree
Showing 2 changed files with 291 additions and 0 deletions.
142 changes: 142 additions & 0 deletions test/60rescache_legacy_test.go
Original file line number Diff line number Diff line change
@@ -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
}
149 changes: 149 additions & 0 deletions test/61rescache_resourcepattern_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 342dc4f

Please sign in to comment.