influxdb/kv/cursor_test.go

245 lines
4.1 KiB
Go
Raw Normal View History

feat(platform): add generic kv store Co-authored-by: Leonardo Di Donato <leodidonato@gmail.com> Co-authored-by: Michael Desa <mjdesa@gmail.com> feat(kv): add kv store interface for services feat(bolt): add boltdb implementation of kv.Store spike(platform): add kv backed user service feat(kv): add static cursor Note here that this operation cannot be transactionally done. This poses a bit of issues that will need to be worked out. fix(bolt): use error explicit error message squash: play with interface a bit fix(kv): remove commit and rollback from kv interface feat(inmem): add inmem kv store chore: add note for inmem transactions fix(bolt): remove call to tx in kv store tests feat(kv): add tests for static cursor doc(kv): add comments to store and associated interfaces doc(bolt): add comments to key value store feat(testing): add kv store tests test(testing): add conformance test for kv.Store test(inmem): add kv.Store conformance tests doc(inmem): add comments to key value store feat(inmem): remove CreateBucketIfNotExists from Tx interface feat(bolt): remove CreateBucketIfNotExists from Tx feat(inmem): remove CreateBucketIfNotExists from Tx doc(kv): add note to bucket interface about conditions methods can be called feat(kv): add context methods to kv.Tx feat(bolt): add context methods to bolt.Tx feat(inmem): add context methods to inmem.Tx test(kv): add contract tests for view/update transactions feat(kv): ensure that static cursor is always valid Co-authored-by: Leonardo Di Donato <leodidonato@gmail.com> Co-authored-by: Michael Desa <mjdesa@gmail.com> fix(kv): remove error from cursor methods test(kv): remove want errors from cursor test test(testing): add concurrent update test for kv.Store feat(kv): make kv user service an example service fix(testing): add concurrnent update test to the kv.Store contract tests test(platform): fix example kv service tests dep(platform): make platform tidy
2018-12-18 15:44:25 +00:00
package kv_test
import (
"bytes"
"testing"
"github.com/influxdata/influxdb/v2/kv"
feat(platform): add generic kv store Co-authored-by: Leonardo Di Donato <leodidonato@gmail.com> Co-authored-by: Michael Desa <mjdesa@gmail.com> feat(kv): add kv store interface for services feat(bolt): add boltdb implementation of kv.Store spike(platform): add kv backed user service feat(kv): add static cursor Note here that this operation cannot be transactionally done. This poses a bit of issues that will need to be worked out. fix(bolt): use error explicit error message squash: play with interface a bit fix(kv): remove commit and rollback from kv interface feat(inmem): add inmem kv store chore: add note for inmem transactions fix(bolt): remove call to tx in kv store tests feat(kv): add tests for static cursor doc(kv): add comments to store and associated interfaces doc(bolt): add comments to key value store feat(testing): add kv store tests test(testing): add conformance test for kv.Store test(inmem): add kv.Store conformance tests doc(inmem): add comments to key value store feat(inmem): remove CreateBucketIfNotExists from Tx interface feat(bolt): remove CreateBucketIfNotExists from Tx feat(inmem): remove CreateBucketIfNotExists from Tx doc(kv): add note to bucket interface about conditions methods can be called feat(kv): add context methods to kv.Tx feat(bolt): add context methods to bolt.Tx feat(inmem): add context methods to inmem.Tx test(kv): add contract tests for view/update transactions feat(kv): ensure that static cursor is always valid Co-authored-by: Leonardo Di Donato <leodidonato@gmail.com> Co-authored-by: Michael Desa <mjdesa@gmail.com> fix(kv): remove error from cursor methods test(kv): remove want errors from cursor test test(testing): add concurrent update test for kv.Store feat(kv): make kv user service an example service fix(testing): add concurrnent update test to the kv.Store contract tests test(platform): fix example kv service tests dep(platform): make platform tidy
2018-12-18 15:44:25 +00:00
)
func TestStaticCursor_First(t *testing.T) {
type args struct {
pairs []kv.Pair
}
type wants struct {
key []byte
val []byte
}
tests := []struct {
name string
args args
wants wants
}{
{
name: "nil pairs",
args: args{
pairs: nil,
},
wants: wants{},
},
{
name: "empty pairs",
args: args{
pairs: []kv.Pair{},
},
wants: wants{},
},
{
name: "unsorted pairs",
args: args{
pairs: []kv.Pair{
{
Key: []byte("bcd"),
Value: []byte("yoyo"),
},
{
Key: []byte("abc"),
Value: []byte("oyoy"),
},
},
},
wants: wants{
key: []byte("abc"),
val: []byte("oyoy"),
},
},
{
name: "sorted pairs",
args: args{
pairs: []kv.Pair{
{
Key: []byte("abc"),
Value: []byte("oyoy"),
},
{
Key: []byte("bcd"),
Value: []byte("yoyo"),
},
},
},
wants: wants{
key: []byte("abc"),
val: []byte("oyoy"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cur := kv.NewStaticCursor(tt.args.pairs)
key, val := cur.First()
if want, got := tt.wants.key, key; !bytes.Equal(want, got) {
t.Errorf("exptected to get key %s got %s", string(want), string(got))
}
if want, got := tt.wants.val, val; !bytes.Equal(want, got) {
t.Errorf("exptected to get value %s got %s", string(want), string(got))
}
})
}
}
func TestStaticCursor_Last(t *testing.T) {
type args struct {
pairs []kv.Pair
}
type wants struct {
key []byte
val []byte
}
tests := []struct {
name string
args args
wants wants
}{
{
name: "nil pairs",
args: args{
pairs: nil,
},
wants: wants{},
},
{
name: "empty pairs",
args: args{
pairs: []kv.Pair{},
},
wants: wants{},
},
{
name: "unsorted pairs",
args: args{
pairs: []kv.Pair{
{
Key: []byte("bcd"),
Value: []byte("yoyo"),
},
{
Key: []byte("abc"),
Value: []byte("oyoy"),
},
},
},
wants: wants{
key: []byte("bcd"),
val: []byte("yoyo"),
},
},
{
name: "sorted pairs",
args: args{
pairs: []kv.Pair{
{
Key: []byte("abc"),
Value: []byte("oyoy"),
},
{
Key: []byte("bcd"),
Value: []byte("yoyo"),
},
},
},
wants: wants{
key: []byte("bcd"),
val: []byte("yoyo"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cur := kv.NewStaticCursor(tt.args.pairs)
key, val := cur.Last()
if want, got := tt.wants.key, key; !bytes.Equal(want, got) {
t.Errorf("exptected to get key %s got %s", string(want), string(got))
}
if want, got := tt.wants.val, val; !bytes.Equal(want, got) {
t.Errorf("exptected to get value %s got %s", string(want), string(got))
}
})
}
}
func TestStaticCursor_Seek(t *testing.T) {
type args struct {
prefix []byte
pairs []kv.Pair
}
type wants struct {
key []byte
val []byte
}
tests := []struct {
name string
args args
wants wants
}{
{
name: "sorted pairs",
args: args{
prefix: []byte("bc"),
pairs: []kv.Pair{
{
Key: []byte("abc"),
Value: []byte("oyoy"),
},
{
Key: []byte("abcd"),
Value: []byte("oyoy"),
},
{
Key: []byte("bcd"),
Value: []byte("yoyo"),
},
{
Key: []byte("bcde"),
Value: []byte("yoyo"),
},
{
Key: []byte("cde"),
Value: []byte("yyoo"),
},
},
},
wants: wants{
key: []byte("bcd"),
val: []byte("yoyo"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cur := kv.NewStaticCursor(tt.args.pairs)
key, val := cur.Seek(tt.args.prefix)
if want, got := tt.wants.key, key; !bytes.Equal(want, got) {
t.Errorf("exptected to get key %s got %s", string(want), string(got))
}
if want, got := tt.wants.val, val; !bytes.Equal(want, got) {
t.Errorf("exptected to get value %s got %s", string(want), string(got))
}
})
}
}