func TestSelectFirst(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() var score Score err := db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(2).First(&score) assert.NoError(t, err) assert.Equal(t, 2, score.Value) err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(1).Reverse().First(&score) assert.NoError(t, err) assert.Equal(t, 18, score.Value) }
func TestSelectFindLimitSkip(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() var scores []Score err := db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Limit(2).Skip(2).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 2) assert.Equal(t, 2, scores[0].Value) assert.Equal(t, 5, scores[1].Value) err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Limit(2).Skip(5).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 1) assert.Equal(t, 19, scores[0].Value) }
func TestSelectFind(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() var scores []Score var scoresPtr []*Score err := db.Select(q.Eq("Value", 5)).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 1) assert.Equal(t, 5, scores[0].Value) err = db.Select(q.Eq("Value", 5)).Find(&scoresPtr) assert.NoError(t, err) assert.Len(t, scoresPtr, 1) assert.Equal(t, 5, scoresPtr[0].Value) err = db.Select( q.Or( q.Eq("Value", 5), q.Eq("Value", 6), ), ).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 2) assert.Equal(t, 5, scores[0].Value) assert.Equal(t, 6, scores[1].Value) err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 6) assert.Equal(t, 0, scores[0].Value) assert.Equal(t, 1, scores[1].Value) assert.Equal(t, 2, scores[2].Value) assert.Equal(t, 5, scores[3].Value) assert.Equal(t, 18, scores[4].Value) assert.Equal(t, 19, scores[5].Value) err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Reverse().Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 6) assert.Equal(t, 19, scores[0].Value) assert.Equal(t, 18, scores[1].Value) assert.Equal(t, 5, scores[2].Value) assert.Equal(t, 2, scores[3].Value) assert.Equal(t, 1, scores[4].Value) assert.Equal(t, 0, scores[5].Value) }
// Range returns one or more records by the specified index within the specified range func (n *node) Range(fieldName string, min, max, to interface{}, options ...func(*index.Options)) error { sink, err := newListSink(n, to) if err != nil { return err } bucketName := sink.bucketName() if bucketName == "" { return ErrNoName } ref := reflect.Indirect(reflect.New(sink.elemType)) cfg, err := extractSingleField(&ref, fieldName) if err != nil { return err } opts := index.NewOptions() for _, fn := range options { fn(opts) } field, ok := cfg.Fields[fieldName] if !ok || (!field.IsID && field.Index == "") { sink.limit = opts.Limit sink.skip = opts.Skip query := newQuery(n, q.And(q.Gte(fieldName, min), q.Lte(fieldName, max))) if opts.Reverse { query.Reverse() } err = n.readTx(func(tx *bolt.Tx) error { return query.query(tx, sink) }) if err != nil { return err } return sink.flush() } mn, err := toBytes(min, n.s.codec) if err != nil { return err } mx, err := toBytes(max, n.s.codec) if err != nil { return err } return n.readTx(func(tx *bolt.Tx) error { return n.rnge(tx, bucketName, fieldName, cfg, sink, mn, mx, opts) }) }
func TestSelectCount(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() total, err := db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Count(&Score{}) assert.NoError(t, err) assert.Equal(t, 6, total) total, err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(2).Count(&Score{}) assert.NoError(t, err) assert.Equal(t, 4, total) total, err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(2).Limit(2).Count(&Score{}) assert.NoError(t, err) assert.Equal(t, 2, total) total, err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(5).Limit(2).Count(&Score{}) assert.NoError(t, err) assert.Equal(t, 1, total) }
func TestSelectDelete(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() err := db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(2).Delete(&Score{}) assert.NoError(t, err) var scores []Score err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 2) assert.Equal(t, 0, scores[0].Value) assert.Equal(t, 1, scores[1].Value) for i := 0; i < 10; i++ { w := User{ID: i + 1, Name: fmt.Sprintf("John%d", i+1)} err = db.Save(&w) assert.NoError(t, err) } err = db.Select(q.Gte("ID", 5)).Delete(&User{}) assert.NoError(t, err) var user User err = db.One("Name", "John6", &user) assert.Error(t, err) assert.Equal(t, ErrNotFound, err) err = db.One("Name", "John4", &user) assert.NoError(t, err) }
func TestSelectFindSkip(t *testing.T) { db, cleanup := prepareScoreDB(t) defer cleanup() var scores []Score err := db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(4).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 2) assert.Equal(t, 18, scores[0].Value) assert.Equal(t, 19, scores[1].Value) err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(-10).Find(&scores) assert.NoError(t, err) assert.Len(t, scores, 6) assert.Equal(t, 0, scores[0].Value) scores = nil err = db.Select(q.Or( q.Eq("Value", 5), q.Or( q.Lte("Value", 2), q.Gte("Value", 18), ), )).Skip(1000).Find(&scores) assert.Error(t, err) assert.True(t, ErrNotFound == err) assert.Len(t, scores, 0) }