zk/core/note/create_test.go

190 lines
4.0 KiB
Go
Raw Normal View History

2021-01-01 17:24:15 +00:00
package note
import (
"fmt"
"testing"
"time"
2021-01-01 17:24:15 +00:00
"github.com/mickael-menu/zk/core/templ"
2021-01-01 17:24:15 +00:00
"github.com/mickael-menu/zk/core/zk"
2021-01-10 15:39:10 +00:00
"github.com/mickael-menu/zk/util/test/assert"
2021-01-01 17:24:15 +00:00
"github.com/mickael-menu/zk/util/opt"
)
var now = time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
2021-01-01 17:24:15 +00:00
func TestCreate(t *testing.T) {
filenameTemplate := spyTemplateString("filename")
bodyTemplate := spyTemplateString("body")
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{
filenameTemplate: &filenameTemplate,
bodyTemplate: &bodyTemplate,
genId: func() string { return "abc" },
validatePath: func(path string) (bool, error) { return true, nil },
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.
assert.Equal(t, filenameTemplate.Contexts, []renderContext{{
ID: "abc",
Title: "Note title",
Content: "Note content",
Dir: "log",
Extra: map[string]string{
"hello": "world",
},
Now: now,
2021-01-01 17:24:15 +00:00
}})
assert.Equal(t, bodyTemplate.Contexts, []renderContext{{
ID: "abc",
Title: "Note title",
Content: "Note content",
Dir: "log",
Filename: "filename.md",
FilenameStem: "filename",
Extra: map[string]string{
"hello": "world",
},
Now: now,
2021-01-01 17:24:15 +00:00
}})
}
func TestCreateTriesUntilValidPath(t *testing.T) {
filenameTemplate := spyTemplate(func(context renderContext) string {
return context.ID
})
bodyTemplate := spyTemplateString("body")
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{
filenameTemplate: &filenameTemplate,
bodyTemplate: &bodyTemplate,
genId: incrementingID(),
validatePath: func(path string) (bool, error) {
return path == "/test/log/3.md", nil
},
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",
})
assert.Equal(t, filenameTemplate.Contexts, []renderContext{
{
ID: "1",
Title: "Note title",
Dir: "log",
Now: now,
2021-01-01 17:24:15 +00:00
},
{
ID: "2",
Title: "Note title",
Dir: "log",
Now: now,
2021-01-01 17:24:15 +00:00
},
{
ID: "3",
Title: "Note title",
Dir: "log",
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 },
now: now,
2021-01-01 17:24:15 +00:00
},
)
assert.Err(t, err, "/test/log/filename.md: note already exists")
}
func spyTemplate(result func(renderContext) string) TemplateSpy {
return TemplateSpy{
Contexts: make([]renderContext, 0),
Result: result,
}
}
func spyTemplateString(result string) TemplateSpy {
return TemplateSpy{
Contexts: make([]renderContext, 0),
Result: func(_ renderContext) string { return result },
}
}
type TemplateSpy struct {
Result func(renderContext) string
Contexts []renderContext
}
func (m *TemplateSpy) Render(context interface{}) (string, error) {
renderContext := context.(renderContext)
m.Contexts = append(m.Contexts, renderContext)
return m.Result(renderContext), nil
}
func incrementingID() func() string {
i := 0
return func() string {
i++
return fmt.Sprintf("%d", i)
}
}