cleaning up scribble new a bit from previous pull request. Updating/fixing tests

This commit is contained in:
Steve Domino 2015-11-23 11:37:37 -07:00
parent 77da6bf6a4
commit 689ffedfe6
2 changed files with 73 additions and 51 deletions

View File

@ -37,51 +37,45 @@ type (
// Options uses for specification of working golang-scribble // Options uses for specification of working golang-scribble
type Options struct { type Options struct {
Logger Logger // the logger scribble will use (configurable)
// ExistDir not creates new directory before start
ExistDir bool
} }
// New creates a new scribble database at the desired directory location, and // New creates a new scribble database at the desired directory location, and
// returns a *Driver to then use for interacting with the database // returns a *Driver to then use for interacting with the database
func New(dir string, opt *Options) (driver *Driver, err error) { func New(dir string, options *Options) (*Driver, error) {
var options Options
if opt == nil {
options = Options{}
} else {
options = *opt
}
// //
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
// ensure the database location doesn't already exist (we don't want to overwrite // create default options
// any existing files/database) opts := Options{}
_, err = os.Stat(dir)
if err == nil && !options.ExistDir { // if options are passed in, use those
fmt.Printf("Unable to create database, '%s' already exists. Please specify a different location.\n", dir) if options != nil {
os.Exit(1) opts = *options
}
// if no logger is provided, create a default
if opts.Logger == nil {
opts.Logger = lumber.NewConsoleLogger(lumber.INFO)
} }
// //
if options.Logger == nil { driver := Driver{
options.Logger = lumber.NewConsoleLogger(lumber.INFO)
}
//
driver = &Driver{
dir: dir, dir: dir,
mutexes: make(map[string]sync.Mutex), mutexes: make(map[string]sync.Mutex),
log: options.Logger, log: opts.Logger,
} }
options.Logger.Info("Creating scribble database at '%v'...\n", dir) // if the database already exists, just use it
if _, err := os.Stat(dir); err == nil {
// opts.Logger.Info("Using '%s' (database already exists)\n", dir)
if options.ExistDir && err == nil { return &driver, nil
return driver, nil
} }
// create database
return driver, os.MkdirAll(dir, 0755) // if the database doesn't exist create it
opts.Logger.Info("Creating scribble database at '%s'...\n", dir)
return &driver, os.MkdirAll(dir, 0755)
} }
// Write locks the database and attempts to write the record to the database under // Write locks the database and attempts to write the record to the database under

View File

@ -13,7 +13,7 @@ type Fish struct {
// //
var ( var (
db *Driver db *Driver
database = "./school" database = "./deep/school"
collection = "fish" collection = "fish"
onefish = Fish{} onefish = Fish{}
twofish = Fish{} twofish = Fish{}
@ -24,41 +24,49 @@ var (
// //
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
var err error // remove any thing for a potentially failed previous test
os.RemoveAll("./deep")
// create a new scribble
if db, err = New(database, nil); err != nil {
panic(err)
}
// run // run
code := m.Run() code := m.Run()
// cleanup // cleanup
os.RemoveAll(database) os.RemoveAll("./deep")
// exit // exit
os.Exit(code) os.Exit(code)
} }
// // Tests creating a new database, and using an existing database
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
if _, err := os.Stat(database); err != nil {
t.Error("Expected file, got none")
}
}
// Checking opening exist dir // database should not exist
func TestNewExist(t *testing.T) { if _, err := os.Stat(database); err == nil {
var err error t.Error("Expected nothing, got database")
if db, err = New(database, &Options{ExistDir: true}); err != nil { }
panic(err)
// create a new database
createDB()
// database should exist
if _, err := os.Stat(database); err != nil {
t.Error("Expected database, got nothing")
}
// should use existing database
createDB()
// database should exist
if _, err := os.Stat(database); err != nil {
t.Error("Expected database, got nothing")
} }
} }
// //
func TestWriteAndRead(t *testing.T) { func TestWriteAndRead(t *testing.T) {
createDB()
// add fish to database // add fish to database
if err := db.Write(collection, "redfish", redfish); err != nil { if err := db.Write(collection, "redfish", redfish); err != nil {
t.Error("Create fish failed: ", err.Error()) t.Error("Create fish failed: ", err.Error())
@ -79,6 +87,8 @@ func TestWriteAndRead(t *testing.T) {
// //
func TestReadall(t *testing.T) { func TestReadall(t *testing.T) {
createDB()
createSchool() createSchool()
fish, err := db.ReadAll(collection) fish, err := db.ReadAll(collection)
@ -96,6 +106,8 @@ func TestReadall(t *testing.T) {
// //
func TestWriteAndReadEmpty(t *testing.T) { func TestWriteAndReadEmpty(t *testing.T) {
createDB()
// create a fish with no home // create a fish with no home
if err := db.Write("", "redfish", redfish); err == nil { if err := db.Write("", "redfish", redfish); err == nil {
t.Error("Allowed write of empty resource", err.Error()) t.Error("Allowed write of empty resource", err.Error())
@ -117,6 +129,8 @@ func TestWriteAndReadEmpty(t *testing.T) {
// //
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
createDB()
// add fish to database // add fish to database
if err := db.Write(collection, "redfish", redfish); err != nil { if err := db.Write(collection, "redfish", redfish); err != nil {
t.Error("Create fish failed: ", err.Error()) t.Error("Create fish failed: ", err.Error())
@ -137,6 +151,8 @@ func TestDelete(t *testing.T) {
// //
func TestDeleteall(t *testing.T) { func TestDeleteall(t *testing.T) {
createDB()
createSchool() createSchool()
if err := db.Delete(collection, ""); err != nil { if err := db.Delete(collection, ""); err != nil {
@ -151,11 +167,23 @@ func TestDeleteall(t *testing.T) {
} }
// //
// create a new scribble database
func createDB() error {
var err error
if db, err = New(database, nil); err != nil {
return err
}
return nil
}
// create a fish
func createFish(fish Fish) error { func createFish(fish Fish) error {
return db.Write(collection, fish.Type, fish) return db.Write(collection, fish.Type, fish)
} }
// // create many fish
func createSchool() error { func createSchool() error {
for _, f := range []Fish{Fish{Type: "red"}, Fish{Type: "blue"}} { for _, f := range []Fish{Fish{Type: "red"}, Fish{Type: "blue"}} {
if err := db.Write(collection, f.Type, f); err != nil { if err := db.Write(collection, f.Type, f); err != nil {
@ -166,12 +194,12 @@ func createSchool() error {
return nil return nil
} }
// // destroy a fish
func destroyFish(name string) error { func destroyFish(name string) error {
return db.Delete(collection, name) return db.Delete(collection, name)
} }
// // destroy all fish
func destroySchool() error { func destroySchool() error {
return db.Delete(collection, "") return db.Delete(collection, "")
} }