Merge pull request #368 from mdlayher/test_tx_foreach
tx_test: add tests for two tx.ForEach cases
Ben Johnson
8 years ago
247 | 247 | defer db.Close() |
248 | 248 | db.Update(func(tx *bolt.Tx) error { |
249 | 249 | 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 | })) | |
250 | 282 | return nil |
251 | 283 | }) |
252 | 284 | } |