2013-11-15 18:55:45 -05:00
|
|
|
package graphdb
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
import (
|
2013-10-22 19:23:52 -04:00
|
|
|
"database/sql"
|
2013-11-04 13:28:38 -05:00
|
|
|
"fmt"
|
2013-10-04 22:25:15 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2014-06-12 01:15:53 -04:00
|
|
|
|
2015-09-03 13:11:31 -04:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
func newTestDb(t *testing.T) (*Database, string) {
|
|
|
|
p := path.Join(os.TempDir(), "sqlite.db")
|
|
|
|
conn, err := sql.Open("sqlite3", p)
|
2014-12-02 06:02:25 -05:00
|
|
|
db, err := NewDatabase(conn)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-22 19:23:52 -04:00
|
|
|
return db, p
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
func destroyTestDb(dbPath string) {
|
|
|
|
os.Remove(dbPath)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDatabase(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
2013-10-04 22:25:15 -04:00
|
|
|
if db == nil {
|
2013-10-09 22:45:45 -04:00
|
|
|
t.Fatal("Database should not be nil")
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-10-28 19:58:59 -04:00
|
|
|
db.Close()
|
2013-10-22 19:23:52 -04:00
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 01:19:00 -04:00
|
|
|
func TestCreateRootEntity(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
root := db.RootEntity()
|
|
|
|
if root == nil {
|
|
|
|
t.Fatal("Root entity should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRootEntity(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
e := db.Get("/")
|
|
|
|
if e == nil {
|
|
|
|
t.Fatal("Entity should not be nil")
|
|
|
|
}
|
|
|
|
if e.ID() != "0" {
|
2015-04-27 16:33:30 -04:00
|
|
|
t.Fatalf("Entity id should be 0, got %s", e.ID())
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetEntityWithDifferentName(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/test", "1")
|
|
|
|
if _, err := db.Set("/other", "1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 22:44:31 -04:00
|
|
|
func TestSetDuplicateEntity(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-09 22:44:31 -04:00
|
|
|
|
|
|
|
if _, err := db.Set("/foo", "42"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/foo", "43"); err == nil {
|
2015-04-27 16:33:30 -04:00
|
|
|
t.Fatalf("Creating an entry with a duplicate path did not cause an error")
|
2013-10-09 22:44:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestCreateChild(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
child, err := db.Set("/db", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if child == nil {
|
|
|
|
t.Fatal("Child should not be nil")
|
|
|
|
}
|
|
|
|
if child.ID() != "1" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:19:00 -04:00
|
|
|
func TestParents(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
a := strconv.Itoa(i)
|
|
|
|
if _, err := db.Set("/"+a, a); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 6; i < 11; i++ {
|
|
|
|
a := strconv.Itoa(i)
|
|
|
|
p := strconv.Itoa(i - 5)
|
|
|
|
|
|
|
|
key := fmt.Sprintf("/%s/%s", p, a)
|
|
|
|
|
|
|
|
if _, err := db.Set(key, a); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parents, err := db.Parents(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parents) != 1 {
|
2015-06-28 19:54:05 -04:00
|
|
|
t.Fatalf("Expected 1 entry for %s got %d", key, len(parents))
|
2014-08-26 01:19:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if parents[0] != p {
|
|
|
|
t.Fatalf("ID %s received, %s expected", parents[0], p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildren(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
str := "/"
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
a := strconv.Itoa(i)
|
|
|
|
if _, err := db.Set(str+a, a); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
str = str + a + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
str = "/"
|
|
|
|
for i := 10; i < 30; i++ { // 20 entities
|
|
|
|
a := strconv.Itoa(i)
|
|
|
|
if _, err := db.Set(str+a, a); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
str = str + a + "/"
|
|
|
|
}
|
|
|
|
entries, err := db.Children("/", 5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != 11 {
|
|
|
|
t.Fatalf("Expect 11 entries for / got %d", len(entries))
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err = db.Children("/", 20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != 25 {
|
|
|
|
t.Fatalf("Expect 25 entries for / got %d", len(entries))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestListAllRootChildren(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
a := strconv.Itoa(i)
|
|
|
|
if _, err := db.Set("/"+a, a); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := db.List("/", -1)
|
|
|
|
if len(entries) != 5 {
|
|
|
|
t.Fatalf("Expect 5 entries for / got %d", len(entries))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListAllSubChildren(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
_, err := db.Set("/webapp", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child2, err := db.Set("/db", "2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child4, err := db.Set("/logs", "4")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/db/logs", child4.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child3, err := db.Set("/sentry", "3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/sentry", child3.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/db", child2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := db.List("/webapp", 1)
|
|
|
|
if len(entries) != 3 {
|
|
|
|
t.Fatalf("Expect 3 entries for / got %d", len(entries))
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = db.List("/webapp", 0)
|
|
|
|
if len(entries) != 2 {
|
|
|
|
t.Fatalf("Expect 2 entries for / got %d", len(entries))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddSelfAsChild(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
child, err := db.Set("/test", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/test/other", child.ID()); err == nil {
|
|
|
|
t.Fatal("Error should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 18:24:18 -04:00
|
|
|
func TestAddChildToNonExistentRoot(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
if _, err := db.Set("/myapp", "1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.Set("/myapp/proxy/db", "2"); err == nil {
|
|
|
|
t.Fatal("Error should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkAll(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
_, err := db.Set("/webapp", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child2, err := db.Set("/db", "2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child4, err := db.Set("/db/logs", "4")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/logs", child4.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child3, err := db.Set("/sentry", "3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/sentry", child3.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/db", child2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child5, err := db.Set("/gograph", "5")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/same-ref-diff-name", child5.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Walk("/", func(p string, e *Entity) error {
|
|
|
|
t.Logf("Path: %s Entity: %s", p, e.ID())
|
|
|
|
return nil
|
|
|
|
}, -1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEntityByPath(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
_, err := db.Set("/webapp", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child2, err := db.Set("/db", "2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child4, err := db.Set("/logs", "4")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/db/logs", child4.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child3, err := db.Set("/sentry", "3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/sentry", child3.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/db", child2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child5, err := db.Set("/gograph", "5")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/same-ref-diff-name", child5.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entity := db.Get("/webapp/db/logs")
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatal("Entity should not be nil")
|
|
|
|
}
|
|
|
|
if entity.ID() != "4" {
|
|
|
|
t.Fatalf("Expected to get entity with id 4, got %s", entity.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnitiesPaths(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
_, err := db.Set("/webapp", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child2, err := db.Set("/db", "2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child4, err := db.Set("/logs", "4")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/db/logs", child4.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child3, err := db.Set("/sentry", "3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/sentry", child3.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/db", child2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child5, err := db.Set("/gograph", "5")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/same-ref-diff-name", child5.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := db.List("/", -1)
|
|
|
|
for _, p := range out.Paths() {
|
|
|
|
t.Log(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRootEntity(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
if err := db.Delete("/"); err == nil {
|
|
|
|
t.Fatal("Error should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteEntity(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
_, err := db.Set("/webapp", "1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child2, err := db.Set("/db", "2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
child4, err := db.Set("/logs", "4")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/db/logs", child4.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child3, err := db.Set("/sentry", "3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/sentry", child3.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/db", child2.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
child5, err := db.Set("/gograph", "5")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Set("/webapp/same-ref-diff-name", child5.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Delete("/webapp/sentry"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
entity := db.Get("/webapp/sentry")
|
|
|
|
if entity != nil {
|
|
|
|
t.Fatal("Entity /webapp/sentry should be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountRefs(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/webapp", "1")
|
|
|
|
|
|
|
|
if db.Refs("1") != 1 {
|
|
|
|
t.Fatal("Expect reference count to be 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Set("/db", "2")
|
|
|
|
db.Set("/webapp/db", "2")
|
|
|
|
if db.Refs("2") != 2 {
|
|
|
|
t.Fatal("Expect reference count to be 2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeId(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/webapp", "1")
|
|
|
|
|
2015-04-14 17:10:17 -04:00
|
|
|
if c := db.Refs("1"); c != 1 {
|
|
|
|
t.Fatalf("Expect reference count to be 1, got %d", c)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
db.Set("/db", "2")
|
|
|
|
db.Set("/webapp/db", "2")
|
|
|
|
|
|
|
|
count, err := db.Purge("2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 2 {
|
2015-04-14 17:10:17 -04:00
|
|
|
t.Fatalf("Expected 2 references to be removed, got %d", count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test https://github.com/docker/docker/issues/12334
|
|
|
|
func TestPurgeIdRefPaths(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
db.Set("/webapp", "1")
|
|
|
|
db.Set("/db", "2")
|
|
|
|
|
|
|
|
db.Set("/db/webapp", "1")
|
|
|
|
|
|
|
|
if c := db.Refs("1"); c != 2 {
|
|
|
|
t.Fatalf("Expected 2 reference for webapp, got %d", c)
|
|
|
|
}
|
|
|
|
if c := db.Refs("2"); c != 1 {
|
|
|
|
t.Fatalf("Expected 1 reference for db, got %d", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rp := db.RefPaths("2"); len(rp) != 1 {
|
|
|
|
t.Fatalf("Expected 1 reference path for db, got %d", len(rp))
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := db.Purge("2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count != 2 {
|
|
|
|
t.Fatalf("Expected 2 rows to be removed, got %d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c := db.Refs("2"); c != 0 {
|
|
|
|
t.Fatalf("Expected 0 reference for db, got %d", c)
|
|
|
|
}
|
|
|
|
if c := db.Refs("1"); c != 1 {
|
|
|
|
t.Fatalf("Expected 1 reference for webapp, got %d", c)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRename(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/webapp", "1")
|
|
|
|
|
|
|
|
if db.Refs("1") != 1 {
|
|
|
|
t.Fatal("Expect reference count to be 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Set("/db", "2")
|
|
|
|
db.Set("/webapp/db", "2")
|
|
|
|
|
|
|
|
if db.Get("/webapp/db") == nil {
|
|
|
|
t.Fatal("Cannot find entity at path /webapp/db")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Rename("/webapp/db", "/webapp/newdb"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if db.Get("/webapp/db") != nil {
|
|
|
|
t.Fatal("Entity should not exist at /webapp/db")
|
|
|
|
}
|
|
|
|
if db.Get("/webapp/newdb") == nil {
|
|
|
|
t.Fatal("Cannot find entity at path /webapp/newdb")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateMultipleNames(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/db", "1")
|
|
|
|
if _, err := db.Set("/myapp", "1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Walk("/", func(p string, e *Entity) error {
|
|
|
|
t.Logf("%s\n", p)
|
|
|
|
return nil
|
|
|
|
}, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefPaths(t *testing.T) {
|
2013-10-22 19:23:52 -04:00
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
db.Set("/webapp", "1")
|
|
|
|
|
|
|
|
db.Set("/db", "2")
|
|
|
|
db.Set("/webapp/db", "2")
|
|
|
|
|
|
|
|
refs := db.RefPaths("2")
|
|
|
|
if len(refs) != 2 {
|
|
|
|
t.Fatalf("Expected reference count to be 2, got %d", len(refs))
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 19:58:59 -04:00
|
|
|
|
|
|
|
func TestExistsTrue(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
db.Set("/testing", "1")
|
|
|
|
|
|
|
|
if !db.Exists("/testing") {
|
|
|
|
t.Fatalf("/tesing should exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExistsFalse(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
2013-10-28 22:19:31 -04:00
|
|
|
db.Set("/toerhe", "1")
|
2013-10-28 19:58:59 -04:00
|
|
|
|
|
|
|
if db.Exists("/testing") {
|
|
|
|
t.Fatalf("/tesing should not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-10-28 22:19:31 -04:00
|
|
|
|
|
|
|
func TestGetNameWithTrailingSlash(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
db.Set("/todo", "1")
|
|
|
|
|
|
|
|
e := db.Get("/todo/")
|
|
|
|
if e == nil {
|
|
|
|
t.Fatalf("Entity should not be nil")
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 13:28:38 -05:00
|
|
|
|
|
|
|
func TestConcurrentWrites(t *testing.T) {
|
|
|
|
db, dbpath := newTestDb(t)
|
|
|
|
defer destroyTestDb(dbpath)
|
|
|
|
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
|
|
|
|
save := func(name string, id string) {
|
|
|
|
if _, err := db.Set(fmt.Sprintf("/%s", name), id); err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
errs <- nil
|
|
|
|
}
|
|
|
|
purge := func(id string) {
|
|
|
|
if _, err := db.Purge(id); err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
errs <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
save("/1", "1")
|
|
|
|
|
|
|
|
go purge("1")
|
|
|
|
go save("/2", "2")
|
|
|
|
|
|
|
|
any := false
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if err := <-errs; err != nil {
|
|
|
|
any = true
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if any {
|
2014-06-12 01:15:53 -04:00
|
|
|
t.Fail()
|
2013-11-04 13:28:38 -05:00
|
|
|
}
|
|
|
|
}
|