package zordfsdb import ( "fmt" "os" "testing" cp "github.com/otiai10/copy" ) func TestInit(t *testing.T) { _, err := InitDB("./testdata") if err != nil { t.Fatal(err) } } func TestKeys(t *testing.T) { db, err := InitDB("./testdata") if err != nil { t.Fatal(err) } keys := db.Keys("") fmt.Println(keys) if len(keys) != 3 { t.Fatal("Wrong keys count on root") } keys = db.Keys("list") fmt.Println(keys) if len(keys) != 2 { t.Fatal("Wrong keys count on list") } keys = db.Keys("list.1") fmt.Println(keys) if len(keys) != 1 { t.Fatal("Wrong keys count on list.1") } keys = db.Keys("object") fmt.Println(keys) if len(keys) != 1 { t.Fatal("Wrong keys count on object") } } func TestGet(t *testing.T) { db, err := InitDB("./testdata") if err != nil { t.Fatal(err) } val, found := db.Get("counter") if !found || val != "12" { t.Fatal("counter value get fail") } _, found = db.Get("not_exist") if found { t.Fatal("found not existing") } val, found = db.Get("list.2.key2") if !found || val != "value3" { t.Fatal("list.2.key2 value get fail") } } func copy(t *testing.T) { err := os.RemoveAll("./testdata2") if err != nil { } err = cp.Copy("./testdata", "./testdata2") if err != nil { t.Fatal("Couldn't prepare data for testing") } } func TestInc(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } db.Inc("counter") val, found := db.Get("counter") if !found || val != "13" { t.Fatal("counter value get fail") } } func TestDec(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } db.Dec("counter") val, found := db.Get("counter") if !found || val != "11" { t.Fatal("counter value get fail") } } func TestNow(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } db.Now("today") val, found := db.Get("today") fmt.Println(val) if !found || len(val) < 10 { t.Fatal("today value length wrong:", len(val)) } } func TestSave(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } db.Save("newKey", "newValue") val, found := db.Get("newKey") fmt.Println(val) if !found || val != "newValue" { t.Fatal("newKey value wrong") } db.Save("object.newKey", "newValue2") val, found = db.Get("object.newKey") fmt.Println(val) if !found || val != "newValue2" { t.Fatal("object.newKey value wrong") } success := db.Save("object2.newKey", "newValue2") if success { t.Fatal("object2.newKey value created - should not") } } func TestDel(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } db.Del("counter") _, found := db.Get("counter") if found { t.Fatal("delete failed") } db.Del("list.2") _, found = db.Get("list.2.key2") if found { t.Fatal("delete failed #2") } } func TestCreateList(t *testing.T) { copy(t) db, err := InitDB("./testdata2") if err != nil { t.Fatal(err) } success := db.CreateNode("object.list2") if !success { t.Fatal("object.list2 node was not created") } id, err := db.AddObject("object.list2") fmt.Println("Added object to object.list2 id:", id) if err != nil { t.Fatal(err) } db.Save("object.list2."+id+".newKey4", "newValue4") val, found := db.Get("object.list2." + id + ".newKey4") fmt.Println(val) if !found || val != "newValue4" { t.Fatal("object.list2.0.newKey4 value wrong") } }