new tree crud test
This commit is contained in:
@@ -1,148 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
import "testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func TestTree(t *testing.T) {
|
||||
func TestTreeCrud(t *testing.T) {
|
||||
tree := NewTree(t.TempDir())
|
||||
|
||||
t.Logf("tree.Get() from zero")
|
||||
if m, err := tree.Get(); err != nil {
|
||||
t.Fatal("failed to get empty tree:", err)
|
||||
} else if m == nil {
|
||||
t.Fatal("got a nil tree:", m)
|
||||
if m, err := tree.GetRoot(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if m.Branches == nil {
|
||||
t.Fatal(m)
|
||||
}
|
||||
|
||||
t.Logf("tree.Del() from zero")
|
||||
if err := tree.Del("id"); err != nil {
|
||||
t.Fatal("failed to del a nil id:", err)
|
||||
}
|
||||
|
||||
t.Logf("tree.Put(bad pid) from zero")
|
||||
if err := tree.Put("id", Branch{PID: "fake"}); err == nil {
|
||||
t.Fatal("failed to put with a fake pid:", err)
|
||||
}
|
||||
|
||||
t.Logf("tree.Put() from zero")
|
||||
if err := tree.Put("id", Branch{}); err != nil {
|
||||
t.Fatal("failed to put with no pid:", err)
|
||||
} else if branches, err := tree.Get(); err != nil {
|
||||
t.Fatal("failed to get after put:", err)
|
||||
} else if branch, ok := branches["id"]; !ok {
|
||||
t.Fatal("got tree without put id:", branches)
|
||||
} else if branch.Title == "" {
|
||||
t.Fatal("got no default title", branch)
|
||||
} else if time.Since(branch.Updated) > time.Hour {
|
||||
t.Fatal("got not updated", branch)
|
||||
} else if branch.Deleted {
|
||||
t.Fatal("got deleted after put", branch)
|
||||
}
|
||||
|
||||
t.Logf("tree.Put(good pid)")
|
||||
if err := tree.Put("id2", Branch{PID: "id"}); err != nil {
|
||||
if err := tree.Del([]string{"id"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("tree.Del(good pid)")
|
||||
if err := tree.Del("id"); err != nil {
|
||||
want := Leaf{
|
||||
Title: "leaf title",
|
||||
Deleted: true,
|
||||
Content: "leaf content",
|
||||
}
|
||||
if err := tree.Put([]string{"id"}, want); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if branches, err := tree.Get(); err != nil {
|
||||
} else if l, err := tree.Get([]string{"id"}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if branch, ok := branches["id"]; !ok {
|
||||
t.Fatal(ok)
|
||||
} else if !branch.Deleted {
|
||||
t.Fatal(branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreePretty(t *testing.T) {
|
||||
tree := NewTree(t.TempDir())
|
||||
tree.Put("A", Branch{Title: "A", PID: ""})
|
||||
tree.Put("AA", Branch{Title: "AA", PID: "A", Deleted: true})
|
||||
tree.Put("B", Branch{Title: "B", PID: ""})
|
||||
tree.Put("BA", Branch{Title: "BA", PID: "B"})
|
||||
tree.Put("BAA", Branch{Title: "BAA", PID: "BA", Deleted: true})
|
||||
tree.Put("BB", Branch{Title: "BB", PID: "B"})
|
||||
tree.Put("BBA", Branch{Title: "BBA", PID: "BB"})
|
||||
tree.Put("BBB", Branch{Title: "BBB", PID: "BB"})
|
||||
tree.Put("BBBC", Branch{Title: "BBBC", PID: "BBB"})
|
||||
tree.Put("C", Branch{Title: "C", PID: "", Deleted: true})
|
||||
tree.Put("D", Branch{Title: "D", PID: ""})
|
||||
tree.Put("DA", Branch{Title: "DA", PID: "D"})
|
||||
tree.Put("DAA", Branch{Title: "DAA", PID: "DA", Deleted: true})
|
||||
tree.Put("DAAA", Branch{Title: "DAAA", PID: "DAA"})
|
||||
got, err := tree.GetPretty()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gotb, _ := json.MarshalIndent(got, "", " ")
|
||||
t.Logf("%s", gotb)
|
||||
/*
|
||||
A
|
||||
-AA
|
||||
B
|
||||
BA
|
||||
-BAA
|
||||
BB
|
||||
BBA
|
||||
BBB
|
||||
BBBC
|
||||
-C
|
||||
D
|
||||
DA
|
||||
-DAA
|
||||
DAAA
|
||||
*/
|
||||
want := bson.M{
|
||||
"A": bson.M{
|
||||
"Title": "A",
|
||||
"Children": bson.M{},
|
||||
},
|
||||
"B": bson.M{
|
||||
"Title": "B",
|
||||
"Children": bson.M{
|
||||
"BA": bson.M{
|
||||
"Title": "BA",
|
||||
"Children": bson.M{},
|
||||
},
|
||||
"BB": bson.M{
|
||||
"Title": "BB",
|
||||
"Children": bson.M{
|
||||
"BBA": bson.M{
|
||||
"Title": "BBA",
|
||||
"Children": bson.M{},
|
||||
},
|
||||
"BBB": bson.M{
|
||||
"Title": "BBB",
|
||||
"Children": bson.M{
|
||||
"BBBC": bson.M{
|
||||
"Title": "BBBC",
|
||||
"Children": bson.M{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"D": bson.M{
|
||||
"Title": "D",
|
||||
"Children": bson.M{
|
||||
"DA": bson.M{
|
||||
"Title": "DA",
|
||||
"Children": bson.M{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
wantb, _ := json.MarshalIndent(want, "", " ")
|
||||
if !bytes.Equal(gotb, wantb) {
|
||||
t.Fatalf("want:\n\t%s, got\n\t%s", wantb, gotb)
|
||||
} else if l != want {
|
||||
t.Fatal(want, l)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user