zk/core/note/create_test.go

163 lines
3.5 KiB
Go
Raw Normal View History

2021-01-01 17:24:15 +00:00
package note
import (
"fmt"
"testing"
"github.com/mickael-menu/zk/core/templ"
2021-01-01 17:24:15 +00:00
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/opt"
2021-01-19 22:06:17 +00:00
"github.com/mickael-menu/zk/util/test/assert"
2021-01-01 17:24:15 +00:00
)
func TestCreate(t *testing.T) {
2021-01-19 22:06:17 +00:00
filenameTemplate := NewRendererSpyString("filename")
bodyTemplate := NewRendererSpyString("body")
2021-01-01 17:24:15 +00:00
res, err := create(
CreateOpts{
Dir: zk.Dir{
Name: "log",
Path: "/test/log",
Config: zk.DirConfig{
Extension: "md",
2021-01-01 17:24:15 +00:00
Extra: map[string]string{
"hello": "world",
},
},
},
Title: opt.NewString("Note title"),
Content: opt.NewString("Note content"),
},
createDeps{
2021-01-19 22:06:17 +00:00
filenameTemplate: filenameTemplate,
bodyTemplate: bodyTemplate,
2021-01-01 17:24:15 +00:00
genId: func() string { return "abc" },
validatePath: func(path string) (bool, error) { return true, nil },
2021-01-19 22:06:17 +00:00
now: Now,
2021-01-01 17:24:15 +00:00
},
)
// Check the created note.
assert.Nil(t, err)
assert.Equal(t, res, &createdNote{
path: "/test/log/filename.md",
content: "body",
})
// Check that the templates received the proper render contexts.
2021-01-19 22:06:17 +00:00
assert.Equal(t, filenameTemplate.Contexts, []interface{}{renderContext{
2021-01-01 17:24:15 +00:00
ID: "abc",
Title: "Note title",
Content: "Note content",
Dir: "log",
Extra: map[string]string{
"hello": "world",
},
2021-01-19 22:06:17 +00:00
Now: Now,
2021-01-01 17:24:15 +00:00
}})
2021-01-19 22:06:17 +00:00
assert.Equal(t, bodyTemplate.Contexts, []interface{}{renderContext{
2021-01-01 17:24:15 +00:00
ID: "abc",
Title: "Note title",
Content: "Note content",
Dir: "log",
Filename: "filename.md",
FilenameStem: "filename",
Extra: map[string]string{
"hello": "world",
},
2021-01-19 22:06:17 +00:00
Now: Now,
2021-01-01 17:24:15 +00:00
}})
}
func TestCreateTriesUntilValidPath(t *testing.T) {
2021-01-19 22:06:17 +00:00
filenameTemplate := NewRendererSpy(func(context interface{}) string {
return context.(renderContext).ID
2021-01-01 17:24:15 +00:00
})
2021-01-19 22:06:17 +00:00
bodyTemplate := NewRendererSpyString("body")
2021-01-01 17:24:15 +00:00
res, err := create(
CreateOpts{
Dir: zk.Dir{
Name: "log",
Path: "/test/log",
Config: zk.DirConfig{
Extension: "md",
},
2021-01-01 17:24:15 +00:00
},
Title: opt.NewString("Note title"),
},
createDeps{
2021-01-19 22:06:17 +00:00
filenameTemplate: filenameTemplate,
bodyTemplate: bodyTemplate,
2021-01-01 17:24:15 +00:00
genId: incrementingID(),
validatePath: func(path string) (bool, error) {
return path == "/test/log/3.md", nil
},
2021-01-19 22:06:17 +00:00
now: Now,
2021-01-01 17:24:15 +00:00
},
)
// Check the created note.
assert.Nil(t, err)
assert.Equal(t, res, &createdNote{
path: "/test/log/3.md",
content: "body",
})
2021-01-19 22:06:17 +00:00
assert.Equal(t, filenameTemplate.Contexts, []interface{}{
renderContext{
2021-01-01 17:24:15 +00:00
ID: "1",
Title: "Note title",
Dir: "log",
2021-01-19 22:06:17 +00:00
Now: Now,
2021-01-01 17:24:15 +00:00
},
2021-01-19 22:06:17 +00:00
renderContext{
2021-01-01 17:24:15 +00:00
ID: "2",
Title: "Note title",
Dir: "log",
2021-01-19 22:06:17 +00:00
Now: Now,
2021-01-01 17:24:15 +00:00
},
2021-01-19 22:06:17 +00:00
renderContext{
2021-01-01 17:24:15 +00:00
ID: "3",
Title: "Note title",
Dir: "log",
2021-01-19 22:06:17 +00:00
Now: Now,
2021-01-01 17:24:15 +00:00
},
})
}
func TestCreateErrorWhenNoValidPaths(t *testing.T) {
_, err := create(
CreateOpts{
Dir: zk.Dir{
Name: "log",
Path: "/test/log",
Config: zk.DirConfig{
Extension: "md",
},
2021-01-01 17:24:15 +00:00
},
},
createDeps{
filenameTemplate: templ.RendererFunc(func(context interface{}) (string, error) {
2021-01-01 17:24:15 +00:00
return "filename", nil
}),
bodyTemplate: templ.NullRenderer,
2021-01-01 17:24:15 +00:00
genId: func() string { return "abc" },
validatePath: func(path string) (bool, error) { return false, nil },
2021-01-19 22:06:17 +00:00
now: Now,
2021-01-01 17:24:15 +00:00
},
)
assert.Err(t, err, "/test/log/filename.md: note already exists")
}
2021-01-19 22:06:17 +00:00
// incrementingID returns a generator of incrementing string ID.
2021-01-01 17:24:15 +00:00
func incrementingID() func() string {
i := 0
return func() string {
i++
return fmt.Sprintf("%d", i)
}
}