zordfsdb/zordfsdb_test.go

326 lines
6.8 KiB
Go
Raw Normal View History

2025-07-10 21:53:25 +03:00
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2024-07-18 12:40:53 +03:00
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")
}
}
2024-07-18 13:53:28 +03:00
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)
2024-07-18 22:53:43 +03:00
if !found || len(val) < 10 {
2024-07-18 13:53:28 +03:00
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")
}
}
2024-08-08 15:23:03 +03:00
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")
}
}
2025-03-16 02:18:12 +02:00
func TestListLength(t *testing.T) {
copy(t)
db, err := InitDB("./testdata2")
if err != nil {
t.Fatal(err)
}
success := db.CreateNode("object.list3")
if !success {
t.Fatal("object.list3 node was not created")
}
id, _ := db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
node, _ := db.GetNode("object.list3")
node.FixType()
fmt.Println(db.GetNode("object.list3"))
length, err := db.Length("object.list3")
if err != nil {
t.Fatal(err)
}
if length != 4 {
t.Fatal("object.list3 length wrong")
}
length, err = db.Length("object")
if err == nil {
t.Fatal("Error expected")
}
}
func TestListRandomNode(t *testing.T) {
copy(t)
db, err := InitDB("./testdata2")
if err != nil {
t.Fatal(err)
}
success := db.CreateNode("object.list3")
if !success {
t.Fatal("object.list3 node was not created")
}
id, _ := db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
id, _ = db.AddObject("object.list3")
db.Save("object.list3."+id+".key", "value")
node, _ := db.GetNode("object.list3")
node.FixType()
2025-03-16 21:22:31 +02:00
node, _ = db.GetRandomNode("object.list3")
2025-03-16 02:18:12 +02:00
if node.Nodes["key"].Value != "value" {
fmt.Println(node)
t.Fatal("Random node value wrong")
}
}
2025-07-11 21:06:20 +03:00
func TestExecuteCommands(t *testing.T) {
copy(t)
db, err := InitDB("./testdata2")
if err != nil {
t.Fatal(err)
}
var lastPath string
var lastKey string
err = db.ExecuteCommands("SAVE newKey2 newValue", &lastPath, &lastKey)
if err != nil {
t.Fatal(err)
}
db.Refresh()
val, found := db.Get("newKey2")
if !found || val != "newValue" {
t.Fatal("newKey2 value get fail")
}
}
2025-07-11 22:50:02 +03:00
func TestTemplate(t *testing.T) {
copy(t)
db, err := InitDB("./testdata2")
if err != nil {
t.Fatal(err)
}
db.AddObject("list")
val, found := db.Get("list.3.created")
if !found {
t.Fatal("No list.3.created added by template")
}
val, found = db.Get("list.3.auto")
if !found || val != "1" {
t.Fatal("No list.3.auto added by template")
}
}