(t *testing.T)
| 237 | } |
| 238 | |
| 239 | func TestCursor_Delete(t *testing.T) { |
| 240 | db := btesting.MustCreateDB(t) |
| 241 | |
| 242 | const count = 1000 |
| 243 | |
| 244 | // Insert every other key between 0 and $count. |
| 245 | if err := db.Update(func(tx *bolt.Tx) error { |
| 246 | b, err := tx.CreateBucket([]byte("widgets")) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | for i := 0; i < count; i += 1 { |
| 251 | k := make([]byte, 8) |
| 252 | binary.BigEndian.PutUint64(k, uint64(i)) |
| 253 | if err := b.Put(k, make([]byte, 100)); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | } |
| 257 | if _, err := b.CreateBucket([]byte("sub")); err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | return nil |
| 261 | }); err != nil { |
| 262 | t.Fatal(err) |
| 263 | } |
| 264 | |
| 265 | if err := db.Update(func(tx *bolt.Tx) error { |
| 266 | c := tx.Bucket([]byte("widgets")).Cursor() |
| 267 | bound := make([]byte, 8) |
| 268 | binary.BigEndian.PutUint64(bound, uint64(count/2)) |
| 269 | for key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() { |
| 270 | if err := c.Delete(); err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | c.Seek([]byte("sub")) |
| 276 | if err := c.Delete(); err != errors.ErrIncompatibleValue { |
| 277 | t.Fatalf("unexpected error: %s", err) |
| 278 | } |
| 279 | |
| 280 | return nil |
| 281 | }); err != nil { |
| 282 | t.Fatal(err) |
| 283 | } |
| 284 | |
| 285 | if err := db.View(func(tx *bolt.Tx) error { |
| 286 | stats := tx.Bucket([]byte("widgets")).Stats() |
| 287 | if stats.KeyN != count/2+1 { |
| 288 | t.Fatalf("unexpected KeyN: %d", stats.KeyN) |
| 289 | } |
| 290 | return nil |
| 291 | }); err != nil { |
| 292 | t.Fatal(err) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Ensure that a Tx cursor can seek to the appropriate keys when there are a |
nothing calls this directly
no test coverage detected