2015-10-12 19:12:29 -04:00
|
|
|
package scribble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
type Fish struct {
|
|
|
|
Type string `json:"type"`
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
var (
|
2015-11-16 01:44:00 -05:00
|
|
|
db *Driver
|
2015-11-23 13:37:37 -05:00
|
|
|
database = "./deep/school"
|
2015-11-16 01:44:00 -05:00
|
|
|
collection = "fish"
|
|
|
|
onefish = Fish{}
|
|
|
|
twofish = Fish{}
|
|
|
|
redfish = Fish{Type: "red"}
|
|
|
|
bluefish = Fish{Type: "blue"}
|
2015-10-12 19:12:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestMain(m *testing.M) {
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// remove any thing for a potentially failed previous test
|
|
|
|
os.RemoveAll("./deep")
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// run
|
|
|
|
code := m.Run()
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// cleanup
|
2015-11-23 13:37:37 -05:00
|
|
|
os.RemoveAll("./deep")
|
2015-11-16 01:44:00 -05:00
|
|
|
|
|
|
|
// exit
|
|
|
|
os.Exit(code)
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// Tests creating a new database, and using an existing database
|
2015-10-12 19:12:29 -04:00
|
|
|
func TestNew(t *testing.T) {
|
2015-11-23 13:37:37 -05:00
|
|
|
|
|
|
|
// 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
|
2015-11-16 01:44:00 -05:00
|
|
|
if _, err := os.Stat(database); err != nil {
|
2015-11-23 13:37:37 -05:00
|
|
|
t.Error("Expected database, got nothing")
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// should use existing database
|
|
|
|
createDB()
|
|
|
|
|
|
|
|
// database should exist
|
|
|
|
if _, err := os.Stat(database); err != nil {
|
|
|
|
t.Error("Expected database, got nothing")
|
2015-11-21 12:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 19:12:29 -04:00
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestWriteAndRead(t *testing.T) {
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
createDB()
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// add fish to database
|
|
|
|
if err := db.Write(collection, "redfish", redfish); err != nil {
|
|
|
|
t.Error("Create fish failed: ", err.Error())
|
|
|
|
}
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// read fish from database
|
|
|
|
if err := db.Read(collection, "redfish", &onefish); err != nil {
|
|
|
|
t.Error("Failed to read: ", err.Error())
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
//
|
|
|
|
if onefish.Type != "red" {
|
|
|
|
t.Error("Expected red fish, got: ", onefish.Type)
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
2015-10-19 17:01:40 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
destroySchool()
|
2015-10-19 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestReadall(t *testing.T) {
|
2015-11-23 13:37:37 -05:00
|
|
|
|
|
|
|
createDB()
|
2015-11-16 01:44:00 -05:00
|
|
|
createSchool()
|
|
|
|
|
|
|
|
fish, err := db.ReadAll(collection)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to read: ", err.Error())
|
|
|
|
}
|
2015-10-19 17:01:40 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
if len(fish) <= 0 {
|
|
|
|
t.Error("Expected some fish, have none")
|
2015-10-19 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
destroySchool()
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestWriteAndReadEmpty(t *testing.T) {
|
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
createDB()
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// create a fish with no home
|
|
|
|
if err := db.Write("", "redfish", redfish); err == nil {
|
|
|
|
t.Error("Allowed write of empty resource", err.Error())
|
|
|
|
}
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// create a home with no fish
|
|
|
|
if err := db.Write(collection, "", redfish); err == nil {
|
|
|
|
t.Error("Allowed write of empty resource", err.Error())
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// no place to read
|
|
|
|
if err := db.Read("", "redfish", onefish); err == nil {
|
|
|
|
t.Error("Allowed read of empty resource", err.Error())
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
destroySchool()
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 17:01:40 -04:00
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
createDB()
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// add fish to database
|
|
|
|
if err := db.Write(collection, "redfish", redfish); err != nil {
|
|
|
|
t.Error("Create fish failed: ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the fish
|
|
|
|
if err := db.Delete(collection, "redfish"); err != nil {
|
|
|
|
t.Error("Failed to delete: ", err.Error())
|
|
|
|
}
|
2015-10-19 17:01:40 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
// read fish from database
|
|
|
|
if err := db.Read(collection, "redfish", &onefish); err == nil {
|
|
|
|
t.Error("Expected nothing, got fish")
|
2015-10-19 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
destroySchool()
|
2015-10-19 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
2015-10-12 19:12:29 -04:00
|
|
|
//
|
2015-11-16 01:44:00 -05:00
|
|
|
func TestDeleteall(t *testing.T) {
|
2015-11-23 13:37:37 -05:00
|
|
|
|
|
|
|
createDB()
|
2015-11-16 01:44:00 -05:00
|
|
|
createSchool()
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
if err := db.Delete(collection, ""); err != nil {
|
|
|
|
t.Error("Failed to delete: ", err.Error())
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
if _, err := os.Stat(collection); err == nil {
|
|
|
|
t.Error("Expected nothing, have fish")
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
destroySchool()
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-11-23 13:37:37 -05:00
|
|
|
|
|
|
|
// 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
|
2015-11-16 01:44:00 -05:00
|
|
|
func createFish(fish Fish) error {
|
|
|
|
return db.Write(collection, fish.Type, fish)
|
|
|
|
}
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// create many fish
|
2015-11-16 01:44:00 -05:00
|
|
|
func createSchool() error {
|
2017-02-09 22:50:15 -05:00
|
|
|
for _, f := range []Fish{{Type: "red"}, {Type: "blue"}} {
|
2015-11-16 01:44:00 -05:00
|
|
|
if err := db.Write(collection, f.Type, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:44:00 -05:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-12 19:12:29 -04:00
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// destroy a fish
|
2015-11-16 01:44:00 -05:00
|
|
|
func destroyFish(name string) error {
|
|
|
|
return db.Delete(collection, name)
|
|
|
|
}
|
|
|
|
|
2015-11-23 13:37:37 -05:00
|
|
|
// destroy all fish
|
2015-11-16 01:44:00 -05:00
|
|
|
func destroySchool() error {
|
|
|
|
return db.Delete(collection, "")
|
2015-10-12 19:12:29 -04:00
|
|
|
}
|