func TestParents(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) } // 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 init() { s, err := mgo.Dial("127.0.0.1:27017") if err != nil { panic(err) } d := s.DB("test") err = d.DropDatabase() if err != nil { panic(err) } session = s testDb = d db = mongoDb.New(session, testDb, map[string]interface{}{}, emptyHooks.New()) emptyOptDoc := nesteddata.New(map[string]interface{}{}) clientId := db.NewId() e = &Entries{ shared{ db, clientId, false, emptyOptDoc, "timeTables", "intervals", false, }, } proId = db.NewId() tt = &TimeTable{ shared{ db, proId, true, emptyOptDoc, "timeTables", "intervals", false, }, } newFilter := func(coll string) iface.Filter { f, err := db.NewFilter(coll, nil) if err != nil { panic(err) } return f } newE = func() iface.Filter { return newFilter("entries") } newTT = func() iface.Filter { return newFilter("timeTables") } newI = func() iface.Filter { return newFilter("intervals") } }
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 := hooks.New() inp := map[string]interface{}{ "crit": "x", } f, err := filter.New(set, ev, map[string]interface{}{}, 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 TestQueryIn(t *testing.T) { set := &TestSet{} ev := hooks.New() inp := map[string]interface{}{ "key": []interface{}{1, 2, 3}, "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 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 := 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) } }