Codebase list golang-github-boltdb-bolt / 764b484
Merge pull request #368 from mdlayher/test_tx_foreach tx_test: add tests for two tx.ForEach cases Ben Johnson 8 years ago
1 changed file(s) with 32 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
247247 defer db.Close()
248248 db.Update(func(tx *bolt.Tx) error {
249249 equals(t, bolt.ErrBucketNotFound, tx.DeleteBucket([]byte("widgets")))
250 return nil
251 })
252 }
253
254 // Ensure that no error is returned when a tx.ForEach function does not return
255 // an error.
256 func TestTx_ForEach_NoError(t *testing.T) {
257 db := NewTestDB()
258 defer db.Close()
259 db.Update(func(tx *bolt.Tx) error {
260 tx.CreateBucket([]byte("widgets"))
261 tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
262
263 equals(t, nil, tx.ForEach(func(name []byte, b *bolt.Bucket) error {
264 return nil
265 }))
266 return nil
267 })
268 }
269
270 // Ensure that an error is returned when a tx.ForEach function returns an error.
271 func TestTx_ForEach_WithError(t *testing.T) {
272 db := NewTestDB()
273 defer db.Close()
274 db.Update(func(tx *bolt.Tx) error {
275 tx.CreateBucket([]byte("widgets"))
276 tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
277
278 err := errors.New("foo")
279 equals(t, err, tx.ForEach(func(name []byte, b *bolt.Bucket) error {
280 return err
281 }))
250282 return nil
251283 })
252284 }