no submod
This commit is contained in:
98
work/notea/server/tree_test.go
Normal file
98
work/notea/server/tree_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTreeForEach(t *testing.T) {
|
||||
tree := NewTree(t.TempDir())
|
||||
id := ""
|
||||
for i := 0; i < 5; i++ {
|
||||
id = path.Join(id, strconv.Itoa(i))
|
||||
leaf := Leaf{Content: id}
|
||||
leaf.Meta.Title = id
|
||||
if err := tree.Put(NewID(id), leaf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
branch, err := tree.GetRoot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := branch.ForEach(func(id ID, leaf Leaf) error {
|
||||
t.Logf("id=%+v, leaf=%+v", id, leaf)
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeDel(t *testing.T) {
|
||||
tree := NewTree(t.TempDir())
|
||||
if err := tree.Put(NewID("id"), Leaf{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tree.Put(NewID("id/subid"), Leaf{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := tree.Del(NewID("id")); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if got, err := tree.Get(NewID("id")); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !got.Meta.Deleted {
|
||||
t.Fatal(got)
|
||||
}
|
||||
|
||||
if root, err := tree.GetRoot(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(root.Branches) != 0 {
|
||||
t.Fatal(root.Branches)
|
||||
}
|
||||
|
||||
if root, err := tree.getRoot(NewID(""), false, true); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(root.Branches) != 1 {
|
||||
t.Fatal(root.Branches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeCrud(t *testing.T) {
|
||||
tree := NewTree(t.TempDir())
|
||||
|
||||
if m, err := tree.GetRoot(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if m.Branches == nil {
|
||||
t.Fatal(m)
|
||||
}
|
||||
|
||||
if err := tree.Del(NewID("id")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
want := Leaf{}
|
||||
want.Meta.Title = "leaf title"
|
||||
want.Meta.Deleted = false
|
||||
want.Content = "leaf content"
|
||||
if err := tree.Put(NewID("id"), want); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if l, err := tree.Get(NewID("id")); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if l != want {
|
||||
t.Fatal(want, l)
|
||||
}
|
||||
|
||||
if withContent, err := tree.GetRoot(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if withoutContent, err := tree.GetRootMeta(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if fmt.Sprint(withContent) == fmt.Sprint(withoutContent) {
|
||||
with, _ := json.MarshalIndent(withContent, "", " ")
|
||||
without, _ := json.MarshalIndent(withoutContent, "", " ")
|
||||
t.Fatalf("without content == with content: \n\twith=%s\n\twout=%s", with, without)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user