aboutsummaryrefslogtreecommitdiff
path: root/db_test.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2015-05-18 13:45:02 -0600
committerBen Johnson <benbjohnson@yahoo.com>2015-05-18 13:45:02 -0600
commitdf52bd0803e5062c92eb947ef63fdf0dd74c5753 (patch)
tree3ad764d7a6580f264cd7ceea9175ef76a021775f /db_test.go
parentmake ignoring Truncate() explicit (diff)
downloaddedo-df52bd0803e5062c92eb947ef63fdf0dd74c5753.tar.gz
dedo-df52bd0803e5062c92eb947ef63fdf0dd74c5753.tar.xz
Add test case inline documentation.
Diffstat (limited to 'db_test.go')
-rw-r--r--db_test.go20
1 files changed, 16 insertions, 4 deletions
diff --git a/db_test.go b/db_test.go
index 64eb923..6af6423 100644
--- a/db_test.go
+++ b/db_test.go
@@ -228,11 +228,12 @@ func TestDB_Open_FileTooSmall(t *testing.T) {
// and that a database can not be opened in read-write mode and in read-only
// mode at the same time.
func TestOpen_ReadOnly(t *testing.T) {
- var bucket = []byte(`bucket`)
- var key = []byte(`key`)
- var value = []byte(`value`)
+ bucket, key, value := []byte(`bucket`), []byte(`key`), []byte(`value`)
+
path := tempfile()
defer os.Remove(path)
+
+ // Open in read-write mode.
db, err := bolt.Open(path, 0666, nil)
ok(t, db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket(bucket)
@@ -245,33 +246,44 @@ func TestOpen_ReadOnly(t *testing.T) {
assert(t, !db.IsReadOnly(), "")
ok(t, err)
ok(t, db.Close())
+
// Open in read-only mode.
db0, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
ok(t, err)
defer db0.Close()
- // Try opening in regular mode.
+
+ // Opening in read-write mode should return an error.
_, err = bolt.Open(path, 0666, &bolt.Options{Timeout: time.Millisecond * 100})
assert(t, err != nil, "")
+
// And again (in read-only mode).
db1, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
ok(t, err)
defer db1.Close()
+
+ // Verify both read-only databases are accessible.
for _, db := range []*bolt.DB{db0, db1} {
// Verify is is in read only mode indeed.
assert(t, db.IsReadOnly(), "")
+
+ // Read-only databases should not allow updates.
assert(t,
bolt.ErrDatabaseReadOnly == db.Update(func(*bolt.Tx) error {
panic(`should never get here`)
}),
"")
+
+ // Read-only databases should not allow beginning writable txns.
_, err = db.Begin(true)
assert(t, bolt.ErrDatabaseReadOnly == err, "")
+
// Verify the data.
ok(t, db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return fmt.Errorf("expected bucket `%s`", string(bucket))
}
+
got := string(b.Get(key))
expected := string(value)
if got != expected {