func TestParents(t *testing.T) { set := &TestSet{} ev := &MockEvents{} inp := map[string]interface{}{ "crit": "x", } f, err := filter.New(set, ev, inp) if err != nil { t.Fatal(err) } // Field referencing other collection fieldname := "fname" f.AddParents(fieldname, []iface.Id{&MockId{}, &MockId{}, &MockId{}}) f.Find() if len(set.lastQuery) != 2 || len(set.lastQuery[fieldname].(map[string]interface{})["$in"].([]iface.Id)) != 3 { t.Fatal(set.lastQuery) } if len(set.lastData) != 0 { t.Fatal(set.lastData) } f.Insert(map[string]interface{}{ "x": "y", }) if len(set.lastData) != 2 || len(set.lastData[fieldname].([]iface.Id)) != 3 { t.Fatal(set.lastData) } }
func TestMultipleSort(t *testing.T) { set := &TestSet{} ev := hooks.New() inp := map[string]interface{}{ "limit": 10, "skip": 3, "sort": []string{"date", "another"}, } _, err := filter.New(set, ev, map[string]interface{}{}, inp) if err != nil { t.Fatal(err) } if len(set.sort) != 1 { t.Fatal(set.sort) } }
func TestAddQuerySafety(t *testing.T) { set := &TestSet{} ev := &MockEvents{} inp := map[string]interface{}{ "crit": "x", } f, err := filter.New(set, ev, inp) if err != nil { t.Fatal(err) } add := map[string]interface{}{ "crit": "y", } f.AddQuery(add) f.Find() if len(set.lastQuery) != 1 || set.lastQuery["crit"] != "x" { t.Fatal(set.lastQuery) } }
func TestCloneQuery(t *testing.T) { set := &TestSet{} ev := &MockEvents{} inp := map[string]interface{}{ "crit": "x", } f, err := filter.New(set, ev, inp) if err != nil { t.Fatal(err) } f1 := f.Clone() f1.AddQuery(map[string]interface{}{ "another_crit": "y", }) f.Find() if len(set.lastQuery) != 1 { t.Fatal(set.lastQuery) } }
func TestQueryIn(t *testing.T) { set := &TestSet{} ev := &MockEvents{} inp := map[string]interface{}{ "key": []interface{}{1, 2, 3}, "limit": 10, "skip": 3, "sort": []string{"x", "y"}, } f, err := filter.New(set, ev, inp) if err != nil { t.Fatal(err) } f.FindOne() if len(set.lastQuery) != 1 { t.Fatal(set.lastQuery) } keys := set.lastQuery["key"].(map[string]interface{})["$in"].([]interface{}) if keys[0] != 1 || keys[1] != 2 || keys[2] != 3 { t.Fatal(keys) } }
func TestMods(t *testing.T) { set := &TestSet{} ev := &MockEvents{} inp := map[string]interface{}{ "limit": 10, "skip": 3, "sort": []string{"x", "y"}, } f, err := filter.New(set, ev, inp) if err != nil { t.Fatal(err) } f.Find() if set.limit != 10 { t.Fatal(set.limit) } if set.skip != 3 { t.Fatal(set.limit) } if len(set.sort) != 2 || set.sort[0] != "x" || set.sort[1] != "y" { t.Fatal(set.sort) } }
func TestMods(t *testing.T) { set := &TestSet{} ev := hooks.New() inp := map[string]interface{}{ "limit": 10, "skip": 3, "sort": "date", } f, err := filter.New(set, ev, map[string]interface{}{}, inp) if err != nil { t.Fatal(err) } f.Find() if set.limit != 10 { t.Fatal(set.limit) } if set.skip != 3 { t.Fatal(set.limit) } if len(set.sort) != 1 || set.sort[0] != "date" { t.Fatal(set.sort) } }
func TestCloneQuery(t *testing.T) { set := &TestSet{} ev := hooks.New() inp := map[string]interface{}{ "crit": "x", } f, err := filter.New(set, ev, map[string]interface{}{}, inp) if err != nil { t.Fatal(err) } f1 := f.Clone() f1.AddQuery(map[string]interface{}{ "anotherCrit": "y", }) f.Find() if len(set.lastQuery) != 1 || set.lastQuery["crit"] != "x" { t.Fatal(set.lastQuery) } f1.Find() if len(set.lastQuery) != 2 || set.lastQuery["crit"] != "x" || set.lastQuery["anotherCrit"] != "y" { t.Fatal(set.lastQuery) } }
// Sorting could have an effect on FindOne though... For now, we specify it as irrelevant. func TestModsSingle(t *testing.T) { set := &TestSet{} ev := hooks.New() inp := map[string]interface{}{ "limit": 10, "skip": 3, "sort": []string{"x", "y"}, } f, err := filter.New(set, ev, map[string]interface{}{}, inp) if err != nil { t.Fatal(err) } f.FindOne() if set.limit != 0 { t.Fatal(set.limit) } if set.skip != 0 { t.Fatal(set.limit) } if len(set.sort) != 0 { t.Fatal(set.sort) } }
func (d *Db) NewFilter(c string, m map[string]interface{}) (iface.Filter, error) { s := set.New(d.db, c) return filter.New(s, d.hooks, m) }
func (d *Db) NewFilter(c string, m map[string]interface{}) (iface.Filter, error) { s := set.New(d.db, c) filterScheme, _ := jsonp.GetM(d.opt, "nouns."+c+".filterScheme") return filter.New(s, d.hooks, filterScheme, m) }