func TestAddQuerySafety(t *testing.T) { set := &TestSet{} ev := &MockEvent{} inp := map[string]interface{}{ "crit": "x", } f := filter.New(set, ev, inp) 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 := &MockEvent{} inp := map[string]interface{}{ "crit": "x", } f := filter.New(set, ev, inp) 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 := &MockEvent{} inp := map[string]interface{}{ "key": []interface{}{1, 2, 3}, "limit": 10, "skip": 3, "sort": []string{"x", "y"}, } f := filter.New(set, ev, inp) 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 := &MockEvent{} inp := map[string]interface{}{ "limit": 10, "skip": 3, "sort": []string{"x", "y"}, } f := filter.New(set, ev, inp) 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 TestParents(t *testing.T) { set := &TestSet{} ev := &MockEvent{} inp := map[string]interface{}{ "crit": "x", } f := filter.New(set, ev, inp) // Field referencing other collection fieldname := "fname" f.AddParents(fieldname, []bson.ObjectId{bson.NewObjectId(), bson.NewObjectId(), bson.NewObjectId()}) f.Find() if len(set.lastQuery) != 2 || len(set.lastQuery[fieldname].(map[string]interface{})["$in"].([]bson.ObjectId)) != 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].([]bson.ObjectId)) != 3 { t.Fatal(set.lastData) } }
func filterCreator(db *mgo.Database, ev iface.Event, nouns, input map[string]interface{}, c string) iface.Filter { return filter.New(set.New(db, c), ev, input) }