fixing some issues with how scribble was reading/writing. added tests

This commit is contained in:
Steve Domino 2015-10-12 17:12:29 -06:00
parent c2b94817bc
commit 77a7709b42
2 changed files with 154 additions and 25 deletions

View File

@ -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
}
@ -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
}
@ -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
}
@ -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)
//
return os.RemoveAll(p)
}
//
func modePath(path string) (m, p string) {
// 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"
}
switch {
// remove the collection from database
case fi.Mode().IsDir():
return os.Remove(d.dir + path)
// remove the record from database
default:
return os.Remove(d.dir + path + ".json")
}
return "dir", path
}
// getOrCreateMutex creates a new collection specific mutex any time a collection

127
scribble_test.go Normal file
View File

@ -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()
}