MCPcopy
hub / github.com/etcd-io/bbolt / TestCursor_Seek

Function TestCursor_Seek

cursor_test.go:170–237  ·  cursor_test.go::TestCursor_Seek

Ensure that a Tx cursor can seek to the appropriate keys.

(t *testing.T)

Source from the content-addressed store, hash-verified

168
169// Ensure that a Tx cursor can seek to the appropriate keys.
170func TestCursor_Seek(t *testing.T) {
171 db := btesting.MustCreateDB(t)
172 if err := db.Update(func(tx *bolt.Tx) error {
173 b, err := tx.CreateBucket([]byte("widgets"))
174 if err != nil {
175 t.Fatal(err)
176 }
177 if err := b.Put([]byte("foo"), []byte("0001")); err != nil {
178 t.Fatal(err)
179 }
180 if err := b.Put([]byte("bar"), []byte("0002")); err != nil {
181 t.Fatal(err)
182 }
183 if err := b.Put([]byte("baz"), []byte("0003")); err != nil {
184 t.Fatal(err)
185 }
186
187 if _, err := b.CreateBucket([]byte("bkt")); err != nil {
188 t.Fatal(err)
189 }
190 return nil
191 }); err != nil {
192 t.Fatal(err)
193 }
194
195 if err := db.View(func(tx *bolt.Tx) error {
196 c := tx.Bucket([]byte("widgets")).Cursor()
197
198 // Exact match should go to the key.
199 if k, v := c.Seek([]byte("bar")); !bytes.Equal(k, []byte("bar")) {
200 t.Fatalf("unexpected key: %v", k)
201 } else if !bytes.Equal(v, []byte("0002")) {
202 t.Fatalf("unexpected value: %v", v)
203 }
204
205 // Inexact match should go to the next key.
206 if k, v := c.Seek([]byte("bas")); !bytes.Equal(k, []byte("baz")) {
207 t.Fatalf("unexpected key: %v", k)
208 } else if !bytes.Equal(v, []byte("0003")) {
209 t.Fatalf("unexpected value: %v", v)
210 }
211
212 // Low key should go to the first key.
213 if k, v := c.Seek([]byte("")); !bytes.Equal(k, []byte("bar")) {
214 t.Fatalf("unexpected key: %v", k)
215 } else if !bytes.Equal(v, []byte("0002")) {
216 t.Fatalf("unexpected value: %v", v)
217 }
218
219 // High key should return no key.
220 if k, v := c.Seek([]byte("zzz")); k != nil {
221 t.Fatalf("expected nil key: %v", k)
222 } else if v != nil {
223 t.Fatalf("expected nil value: %v", v)
224 }
225
226 // Buckets should return their key but no value.
227 if k, v := c.Seek([]byte("bkt")); !bytes.Equal(k, []byte("bkt")) {

Callers

nothing calls this directly

Calls 10

MustCreateDBFunction · 0.92
UpdateMethod · 0.80
ViewMethod · 0.80
SeekMethod · 0.80
FatalMethod · 0.65
FatalfMethod · 0.65
CreateBucketMethod · 0.45
PutMethod · 0.45
CursorMethod · 0.45
BucketMethod · 0.45

Tested by

no test coverage detected