Пример #1
0
// Search a Client by its condition(s)
func (s *ClientStore) Search(
	q store.Query) store.Result {

	return upperio.NewResult(func() (res db.Result, err error) {
		// get collection
		coll, err := s.Coll()
		if err != nil {
			return
		}

		// retrieve entities by given query conditions
		conds := upperio.Conds(q.GetConds())
		if conds == nil {
			res = coll.Find()
		} else {
			res = coll.Find(conds)
		}

		// add sorting information, if any
		res = res.Sort(upperio.Sort(q)...)

		// handle paging
		if q.GetOffset() != 0 {
			res = res.Skip(uint(q.GetOffset()))
		}
		if q.GetLimit() != 0 {
			res = res.Limit(uint(q.GetLimit()))
		}

		return
	})

}
Пример #2
0
func TestConds_branching(t *testing.T) {

	var err error

	fn := "./test3.tmp"

	// two branch query
	cond1 := store.NewConds().
		Add("HelloWorld =", "foo bar").
		Add("FooBar !=", "hello world")
	cond2 := store.NewConds().
		Add("HelloWorld =", "foo bar 2").
		Add("FooBar !=", "hello world")

	q := store.NewQuery().
		AddCond("", cond1).
		AddCond("", cond2)

	q.GetConds().SetRel(store.Or)

	// test source
	source := upperio.NewSource(testUpperDb(fn))

	// add dummy data to the database
	if err := testUpperDbData(source); err != nil {
		t.Fatal(err.Error())
	}

	// connect to database again
	conn, err := source.Open()
	if err != nil {
		t.Error(err.Error())
	}
	defer conn.Close()

	// query connection
	sess := conn.Raw().(db.Database)
	coll, err := sess.Collection("dummy_data")
	res := coll.Find(upperio.Conds(q.GetConds()))
	var tds []testData
	res.All(&tds)

	expLen := 2
	if l := len(tds); l != expLen {
		t.Errorf("result set size expected: %d, got: %d\ntest data set:\t%#v",
			expLen, l, tds)
	}

	// clean up the temp database
	err = os.Remove(fn)
	if err != nil {
		t.Error(err.Error())
	}

}
Пример #3
0
func TestConds(t *testing.T) {

	var err error

	fn := "./test2.tmp"

	q := store.NewQuery().
		AddCond("HelloWorld =", "foo bar").
		AddCond("FooBar !=", "hello world")

	// dummy source
	source := upperio.NewSource(testUpperDb(fn))

	// add dummy data to the database
	if err := testUpperDbData(source); err != nil {
		t.Fatal(err.Error())
	}

	// connect to database again
	conn, err := source.Open()
	if err != nil {
		t.Error(err.Error())
	}
	defer conn.Close()

	// query connection
	sess := conn.Raw().(db.Database)
	coll, err := sess.Collection("dummy_data")
	if err != nil {
		t.Error(err.Error())
	}

	conds := upperio.Conds(q.GetConds())
	res := coll.Find(conds)
	var tds []testData
	res.All(&tds)

	if len(tds) != 1 {
		t.Errorf("Incorrect test data set: %#v", tds)
	}

	// clean up the temp database
	err = os.Remove(fn)
	if err != nil {
		t.Error(err.Error())
	}

}
Пример #4
0
func TestConds_empty(t *testing.T) {
	q := store.NewQuery()
	if cs := upperio.Conds(q.GetConds()); cs != nil {
		t.Errorf("Conds with empty new query should return nil. Instead got %#v", cs)
	}
}