Skip to content

Commit

Permalink
fixing some issues with how scribble was reading/writing. added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdomino committed Oct 12, 2015
1 parent c2b9481 commit 77a7709
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 24 deletions.
50 changes: 26 additions & 24 deletions scribble.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,16 @@ func (d *Driver) Write(collection, resource string, v interface{}) error {
// Read a record from the database
func (d *Driver) Read(path string, v interface{}) error {

dir := d.dir + path

//
fi, err := os.Stat(path)
if err != nil {
return err
}
m, p := modePath(d.dir + path)

switch {
switch m {

// if the path is a directory, attempt to read all entries into v
case fi.Mode().IsDir():
case "dir":

// read all the files in the transaction.Collection
files, err := ioutil.ReadDir(dir)
files, err := ioutil.ReadDir(p)
if err != nil {
// an error here just means the collection is either empty or doesn't exist
}
Expand All @@ -120,7 +115,7 @@ func (d *Driver) Read(path string, v interface{}) error {
// iterate over each of the files, attempting to read the file. If successful
// append the files to the collection of read files
for _, file := range files {
b, err := ioutil.ReadFile(dir + "/" + file.Name())
b, err := ioutil.ReadFile(p + "/" + file.Name())
if err != nil {
return err
}
Expand All @@ -133,10 +128,10 @@ func (d *Driver) Read(path string, v interface{}) error {
return json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), v)

// if the path is a file, attempt to read the single file
case !fi.Mode().IsDir():
case "file":

// read record from database
b, err := ioutil.ReadFile(dir + ".json")
b, err := ioutil.ReadFile(p)
if err != nil {
return err
}
Expand All @@ -156,21 +151,28 @@ func (d *Driver) Delete(path string) error {
mutex.Lock()
defer mutex.Unlock()

// stat the file to determine if it is a file or dir
fi, err := os.Stat(path)
if err != nil {
return err
}
//
_, p := modePath(d.dir + path)

switch {
// remove the collection from database
case fi.Mode().IsDir():
return os.Remove(d.dir + path)
//
return os.RemoveAll(p)
}

//
func modePath(path string) (m, p string) {

// remove the record from database
default:
return os.Remove(d.dir + path + ".json")
// check for dir
if _, err := os.Stat(path); os.IsNotExist(err) {

// check for file
if _, err := os.Stat(path + ".json"); os.IsNotExist(err) {
fmt.Printf("No file or directory found at '%v'\n", path+".json")
}

return "file", path + ".json"
}

return "dir", path
}

// getOrCreateMutex creates a new collection specific mutex any time a collection
Expand Down
127 changes: 127 additions & 0 deletions scribble_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package scribble

import (
"os"
"testing"
)

//
type Friend struct {
Name string `json:"name"`
}

//
var (
db *Driver
testRoot = "./test_db"
friendsPath = "/friends"
friend0 = Friend{}
friend1 = Friend{Name: "wocket"}
friend2 = Friend{Name: "wasket"}
)

//
func init() {
startup()
}

//
func startup() {
db, _ = New(testRoot, nil)
}

//
func teardown() {
os.RemoveAll(testRoot)
}

//
func createFriend(t *testing.T) {
if err := db.Write(friendsPath, "friend1", friend1); err != nil {
t.Error("Failed to write", err)
}
}

//
func createFriends(t *testing.T) {
if err := db.Write(friendsPath, "friend1", friend1); err != nil {
t.Error("Failed to write", err)
}

if err := db.Write(friendsPath, "friend2", friend2); err != nil {
t.Error("Failed to write", err)
}
}

//
func TestNew(t *testing.T) {
if _, err := os.Stat(testRoot); os.IsNotExist(err) {
t.Error("Expected file, got none", err)
}

teardown()
}

//
func TestWrite(t *testing.T) {
createFriend(t)
}

//
func TestRead(t *testing.T) {
createFriend(t)

if err := db.Read(friendsPath+"/friend1", &friend0); err != nil {
t.Error("Failed to read", err)
}

if friend0.Name == "" {
t.Error("Expected friend, have none")
}
}

//
func TestReadall(t *testing.T) {
createFriends(t)

friends := []Friend{}
if err := db.Read(friendsPath, &friends); err != nil {
t.Error("Failed to read", err)
}

if len(friends) <= 0 {
t.Error("Expected friends, have none")
}

teardown()
}

//
func TestDelete(t *testing.T) {
createFriend(t)

if err := db.Delete(friendsPath + "/friend1"); err != nil {
t.Error("Failed to delete", err)
}

if fi, err := os.Stat(friendsPath + "/friend1"); fi != nil {
t.Error("Expected nothing, have friends", err)
}

teardown()
}

//
func TestDeleteall(t *testing.T) {
createFriends(t)

if err := db.Delete(friendsPath); err != nil {
t.Error("Failed to delete ", err)
}

if fi, err := os.Stat(friendsPath); fi != nil {
t.Error("Expected nothing, have friends", err)
}

teardown()
}

0 comments on commit 77a7709

Please sign in to comment.