mirror of https://github.com/etcd-io/bbolt.git
Add bolt.Open().
Per the suggestion of @tv42 and @cespare, this commit adds a package level function to create and initialize a database at a given path. This is a common interface for database packages.pull/34/head
parent
41fb285e37
commit
a47c50295a
|
@ -0,0 +1,15 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Open creates and opens a database at the given path.
|
||||||
|
// If the file does not exist then it will be created automatically.
|
||||||
|
func Open(path string, mode os.FileMode) (*DB, error) {
|
||||||
|
db := &DB{}
|
||||||
|
if err := db.Open(path, mode); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db, nil
|
||||||
|
}
|
21
db_test.go
21
db_test.go
|
@ -14,6 +14,27 @@ import (
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Ensure that a database can be opened without error.
|
||||||
|
func TestOpen(t *testing.T) {
|
||||||
|
f, _ := ioutil.TempFile("", "bolt-")
|
||||||
|
path := f.Name()
|
||||||
|
f.Close()
|
||||||
|
os.Remove(path)
|
||||||
|
defer os.RemoveAll(path)
|
||||||
|
|
||||||
|
db, err := Open(path, 0666)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, db)
|
||||||
|
db.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that opening a database with a bad path returns an error.
|
||||||
|
func TestOpenBadPath(t *testing.T) {
|
||||||
|
db, err := Open("/../bad-path", 0666)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, db)
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that a database can be opened without error.
|
// Ensure that a database can be opened without error.
|
||||||
func TestDBOpen(t *testing.T) {
|
func TestDBOpen(t *testing.T) {
|
||||||
withDB(func(db *DB, path string) {
|
withDB(func(db *DB, path string) {
|
||||||
|
|
Loading…
Reference in New Issue