From 689ffedfe66d35e55512f3ff3ff2c1ea41c31d03 Mon Sep 17 00:00:00 2001 From: Steve Domino Date: Mon, 23 Nov 2015 11:37:37 -0700 Subject: [PATCH] cleaning up scribble new a bit from previous pull request. Updating/fixing tests --- scribble.go | 54 +++++++++++++++++-------------------- scribble_test.go | 70 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/scribble.go b/scribble.go index 7ea7359..10d05aa 100644 --- a/scribble.go +++ b/scribble.go @@ -37,51 +37,45 @@ type ( // Options uses for specification of working golang-scribble type Options struct { - Logger - // ExistDir not creates new directory before start - ExistDir bool + Logger // the logger scribble will use (configurable) } // New creates a new scribble database at the desired directory location, and // returns a *Driver to then use for interacting with the database -func New(dir string, opt *Options) (driver *Driver, err error) { - var options Options - if opt == nil { - options = Options{} - } else { - options = *opt - } +func New(dir string, options *Options) (*Driver, error) { + // dir = filepath.Clean(dir) - // ensure the database location doesn't already exist (we don't want to overwrite - // any existing files/database) - _, err = os.Stat(dir) - if err == nil && !options.ExistDir { - fmt.Printf("Unable to create database, '%s' already exists. Please specify a different location.\n", dir) - os.Exit(1) + // create default options + opts := Options{} + + // if options are passed in, use those + if options != nil { + opts = *options + } + + // if no logger is provided, create a default + if opts.Logger == nil { + opts.Logger = lumber.NewConsoleLogger(lumber.INFO) } // - if options.Logger == nil { - options.Logger = lumber.NewConsoleLogger(lumber.INFO) - } - - // - driver = &Driver{ + driver := Driver{ dir: dir, mutexes: make(map[string]sync.Mutex), - log: options.Logger, + log: opts.Logger, } - options.Logger.Info("Creating scribble database at '%v'...\n", dir) - - // - if options.ExistDir && err == nil { - return driver, nil + // 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) + 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 diff --git a/scribble_test.go b/scribble_test.go index 071dd26..467c82c 100644 --- a/scribble_test.go +++ b/scribble_test.go @@ -13,7 +13,7 @@ type Fish struct { // var ( db *Driver - database = "./school" + database = "./deep/school" collection = "fish" onefish = Fish{} twofish = Fish{} @@ -24,41 +24,49 @@ var ( // func TestMain(m *testing.M) { - var err error - - // create a new scribble - if db, err = New(database, nil); err != nil { - panic(err) - } + // remove any thing for a potentially failed previous test + os.RemoveAll("./deep") // run code := m.Run() // cleanup - os.RemoveAll(database) + os.RemoveAll("./deep") // exit os.Exit(code) } -// +// Tests creating a new database, and using an existing database func TestNew(t *testing.T) { - if _, err := os.Stat(database); err != nil { - t.Error("Expected file, got none") - } -} -// Checking opening exist dir -func TestNewExist(t *testing.T) { - var err error - if db, err = New(database, &Options{ExistDir: true}); err != nil { - panic(err) + // database should not exist + if _, err := os.Stat(database); err == nil { + t.Error("Expected nothing, got database") + } + + // 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) { + createDB() + // add fish to database if err := db.Write(collection, "redfish", redfish); err != nil { t.Error("Create fish failed: ", err.Error()) @@ -79,6 +87,8 @@ func TestWriteAndRead(t *testing.T) { // func TestReadall(t *testing.T) { + + createDB() createSchool() fish, err := db.ReadAll(collection) @@ -96,6 +106,8 @@ func TestReadall(t *testing.T) { // func TestWriteAndReadEmpty(t *testing.T) { + createDB() + // create a fish with no home if err := db.Write("", "redfish", redfish); err == nil { t.Error("Allowed write of empty resource", err.Error()) @@ -117,6 +129,8 @@ func TestWriteAndReadEmpty(t *testing.T) { // func TestDelete(t *testing.T) { + createDB() + // add fish to database if err := db.Write(collection, "redfish", redfish); err != nil { t.Error("Create fish failed: ", err.Error()) @@ -137,6 +151,8 @@ func TestDelete(t *testing.T) { // func TestDeleteall(t *testing.T) { + + createDB() createSchool() 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 { return db.Write(collection, fish.Type, fish) } -// +// create many fish func createSchool() error { for _, f := range []Fish{Fish{Type: "red"}, Fish{Type: "blue"}} { if err := db.Write(collection, f.Type, f); err != nil { @@ -166,12 +194,12 @@ func createSchool() error { return nil } -// +// destroy a fish func destroyFish(name string) error { return db.Delete(collection, name) } -// +// destroy all fish func destroySchool() error { return db.Delete(collection, "") }