145 lines
3.0 KiB
Go
145 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"path"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
func TestTree(t *testing.T) {
|
|
path := path.Join(t.TempDir(), "index.yaml")
|
|
tree := NewTree(path)
|
|
|
|
if m, err := tree.Get(); err != nil {
|
|
t.Fatal(err)
|
|
} else if m == nil {
|
|
t.Fatal(m)
|
|
}
|
|
|
|
if err := tree.Del("id"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := tree.Put("id", Branch{PID: "fake"}); err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := tree.Put("id", Branch{}); err != nil {
|
|
t.Fatal(err)
|
|
} else if branches, err := tree.Get(); err != nil {
|
|
t.Fatal(err)
|
|
} else if branch, ok := branches["id"]; !ok {
|
|
t.Fatal(err)
|
|
} else if branch.Title == "" {
|
|
t.Fatal(branch)
|
|
} else if time.Since(branch.Updated) > time.Hour {
|
|
t.Fatal(branch)
|
|
} else if branch.Deleted {
|
|
t.Fatal(branch)
|
|
}
|
|
|
|
if err := tree.Put("id2", Branch{PID: "id"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := tree.Del("id"); err != nil {
|
|
t.Fatal(err)
|
|
} else if branches, err := tree.Get(); 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(path.Join(t.TempDir(), "tree_pretty.yaml"))
|
|
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)
|
|
}
|
|
}
|