forked from nanobox-io/golang-scribble
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing some issues with how scribble was reading/writing. added tests
- Loading branch information
Showing
2 changed files
with
153 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |