Test NoteDAO find all

pull/6/head
Mickaël Menu 3 years ago
parent 81cc8ae9a4
commit 9f1ec95194
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -4,8 +4,8 @@
body: "A daily note"
word_count: 3
checksum: "qwfpgj"
created: "2020-11-22T16:27:45.734392408+01:00"
modified: "2020-11-22T16:27:45.734454655+01:00"
created: "2020-11-22T16:27:45+01:00"
modified: "2020-11-22T16:27:45+01:00"
- id: 2
path: "log/2021-01-04.md"
@ -13,8 +13,8 @@
body: "A second daily note"
word_count: 4
checksum: "arstde"
created: "2020-11-29T08:20:18.138816226+01:00"
modified: "2020-11-29T08:20:18.138907236+01:00"
created: "2020-11-29T08:20:18+01:00"
modified: "2020-11-29T08:20:18+01:00"
- id: 3
path: "index.md"
@ -22,8 +22,8 @@
body: "Index of the Zettelkasten"
word_count: 4
checksum: "iaefhv"
created: "2019-12-04T11:59:11.927409053+01:00"
modified: "2019-12-04T12:17:21.720747+01:00"
created: "2019-12-04T11:59:11+01:00"
modified: "2019-12-04T12:17:21+01:00"
- id: 4
path: "f39c8.md"
@ -31,8 +31,8 @@
body: "Its content will surprise you"
word_count: 5
checksum: "irkwyc"
created: "2020-01-19T10:58:41.567911029+01:00"
modified: "2020-01-20T08:52:42.321024+01:00"
created: "2020-01-19T10:58:41+01:00"
modified: "2020-01-20T08:52:42+01:00"
- id: 5
path: "ref/test/b.md"
@ -40,8 +40,8 @@
body: "This one is in a sub sub directory"
word_count: 8
checksum: "yvwbae"
created: "2019-11-20T20:32:56.107028961+01:00"
modified: "2019-11-20T20:34:06.120375+01:00"
created: "2019-11-20T20:32:56+01:00"
modified: "2019-11-20T20:34:06+01:00"
- id: 6
path: "ref/test/a.md"
@ -49,5 +49,5 @@
body: "It shall appear before b.md"
word_count: 5
checksum: "iecywst"
created: "2019-11-20T20:32:56.107028961+01:00"
modified: "2019-11-20T20:34:06.120375+01:00"
created: "2019-11-20T20:32:56+01:00"
modified: "2019-11-20T20:34:06+01:00"

@ -12,33 +12,31 @@ import (
)
func TestNoteDAOIndexed(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
expected := []paths.Metadata{
{
Path: "f39c8.md",
Modified: date("2020-01-20T08:52:42.321024+01:00"),
Modified: date("2020-01-20T08:52:42+01:00"),
},
{
Path: "index.md",
Modified: date("2019-12-04T12:17:21.720747+01:00"),
Modified: date("2019-12-04T12:17:21+01:00"),
},
{
Path: "log/2021-01-03.md",
Modified: date("2020-11-22T16:27:45.734454655+01:00"),
Modified: date("2020-11-22T16:27:45+01:00"),
},
{
Path: "log/2021-01-04.md",
Modified: date("2020-11-29T08:20:18.138907236+01:00"),
Modified: date("2020-11-29T08:20:18+01:00"),
},
{
Path: "ref/test/a.md",
Modified: date("2019-11-20T20:34:06.120375+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
},
{
Path: "ref/test/b.md",
Modified: date("2019-11-20T20:34:06.120375+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
},
}
@ -54,16 +52,14 @@ func TestNoteDAOIndexed(t *testing.T) {
}
func TestNoteDAOAdd(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Add(note.Metadata{
Path: "log/added.md",
Title: "Added note",
Body: "Note body",
WordCount: 2,
Created: date("2019-11-19T15:33:31.467036963+01:00"),
Modified: date("2020-01-16T16:04:59.396405+01:00"),
Created: date("2019-11-19T15:33:31+01:00"),
Modified: date("2020-01-16T16:04:59+01:00"),
Checksum: "check",
})
assert.Nil(t, err)
@ -76,33 +72,29 @@ func TestNoteDAOAdd(t *testing.T) {
Body: "Note body",
WordCount: 2,
Checksum: "check",
Created: date("2019-11-19T15:33:31.467036963+01:00"),
Modified: date("2020-01-16T16:04:59.396405+01:00"),
Created: date("2019-11-19T15:33:31+01:00"),
Modified: date("2020-01-16T16:04:59+01:00"),
})
})
}
// Check that we can't add a duplicate note with an existing path.
func TestNoteDAOAddExistingNote(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Add(note.Metadata{Path: "ref/test/a.md"})
assert.Err(t, err, "ref/test/a.md: can't add note to the index: UNIQUE constraint failed: notes.path")
})
}
func TestNoteDAOUpdate(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Update(note.Metadata{
Path: "ref/test/a.md",
Title: "Updated note",
Body: "Updated body",
WordCount: 42,
Created: date("2020-11-22T16:49:47.309530098+01:00"),
Modified: date("2020-11-22T16:49:47.309769915+01:00"),
Created: date("2020-11-22T16:49:47+01:00"),
Modified: date("2020-11-22T16:49:47+01:00"),
Checksum: "updated checksum",
})
assert.Nil(t, err)
@ -115,16 +107,14 @@ func TestNoteDAOUpdate(t *testing.T) {
Body: "Updated body",
WordCount: 42,
Checksum: "updated checksum",
Created: date("2019-11-20T20:32:56.107028961+01:00"),
Modified: date("2020-11-22T16:49:47.309769915+01:00"),
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2020-11-22T16:49:47+01:00"),
})
})
}
func TestNoteDAOUpdateUnknown(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Update(note.Metadata{
Path: "unknown/unknown.md",
})
@ -133,23 +123,135 @@ func TestNoteDAOUpdateUnknown(t *testing.T) {
}
func TestNoteDAORemove(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Remove("ref/test/a.md")
assert.Nil(t, err)
})
}
func TestNoteDAORemoveUnknown(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
err := dao.Remove("unknown/unknown.md")
assert.Err(t, err, "unknown/unknown.md: failed to remove note index: note not found in the index")
})
}
func TestNoteDAOFindAll(t *testing.T) {
testNoteDAOFind(t, []note.Match{
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "ref/test/b.md",
Title: "A nested note",
Body: "This one is in a sub sub directory",
WordCount: 8,
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
Checksum: "yvwbae",
},
},
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "f39c8.md",
Title: "An interesting note",
Body: "Its content will surprise you",
WordCount: 5,
Created: date("2020-01-19T10:58:41+01:00"),
Modified: date("2020-01-20T08:52:42+01:00"),
Checksum: "irkwyc",
},
},
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "ref/test/a.md",
Title: "Another nested note",
Body: "It shall appear before b.md",
WordCount: 5,
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
Checksum: "iecywst",
},
},
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "index.md",
Title: "Index",
Body: "Index of the Zettelkasten",
WordCount: 4,
Created: date("2019-12-04T11:59:11+01:00"),
Modified: date("2019-12-04T12:17:21+01:00"),
Checksum: "iaefhv",
},
},
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "log/2021-01-03.md",
Title: "January 3, 2021",
Body: "A daily note",
WordCount: 3,
Created: date("2020-11-22T16:27:45+01:00"),
Modified: date("2020-11-22T16:27:45+01:00"),
Checksum: "qwfpgj",
},
},
note.Match{
Snippet: "",
Metadata: note.Metadata{
Path: "log/2021-01-04.md",
Title: "January 4, 2021",
Body: "A second daily note",
WordCount: 4,
Created: date("2020-11-29T08:20:18+01:00"),
Modified: date("2020-11-29T08:20:18+01:00"),
Checksum: "arstde",
},
},
})
}
func testNoteDAOFind(t *testing.T, expected []note.Match) {
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
actual := make([]note.Match, 0)
err := dao.Find(func(m note.Match) error {
actual = append(actual, m)
return nil
})
assert.Nil(t, err)
popExpected := func() (note.Match, bool) {
if len(expected) == 0 {
return note.Match{}, false
}
item := expected[0]
expected = expected[1:]
return item, true
}
for _, act := range actual {
exp, ok := popExpected()
if !ok {
t.Errorf("More matches than expected: %v", actual)
return
}
assert.Equal(t, act, exp)
}
if len(expected) > 0 {
t.Errorf("Missing expected matches: %v", expected)
}
})
}
func testNoteDAO(t *testing.T, callback func(tx Transaction, dao *NoteDAO)) {
testTransaction(t, func(tx Transaction) {
callback(tx, NewNoteDAO(tx, &util.NullLogger))
})
}
type noteRow struct {
Path, Title, Body, Checksum string
WordCount int

@ -28,7 +28,7 @@ func (s *Styler) Style(text string, rules ...style.Rule) (string, error) {
// FIXME: User config
var themeAliases = map[style.Rule][]style.Rule{
"title": {"bold", "yellow"},
"path": {"cyan"},
"path": {"underline", "cyan"},
"match": {"red"},
}

@ -45,7 +45,7 @@ func TestStyleAllRules(t *testing.T) {
}
test("title", "1;33m")
test("path", "36m")
test("path", "4;36m")
test("match", "31m")
test("reset", "0m")

@ -26,6 +26,18 @@ type Metadata struct {
Checksum string
}
func (m Metadata) String() string {
return fmt.Sprintf(`note.Metadata{
Path: "%v",
Title: "%v",
Body: "%v",
WordCount: %v,
Created: "%v",
Modified: "%v",
Checksum: "%v",
}`, m.Path, m.Title, m.Body, m.WordCount, m.Created.Format(time.RFC3339), m.Modified.Format(time.RFC3339), m.Checksum)
}
// Indexer persists the notes index.
type Indexer interface {
// Indexed returns the list of indexed note file metadata.

@ -1,6 +1,7 @@
package note
import (
"fmt"
"os"
"path/filepath"
"regexp"
@ -21,6 +22,13 @@ type Match struct {
Metadata
}
func (m Match) String() string {
return fmt.Sprintf(`note.Match{
Snippet: "%v",
Metadata: %v,
}`, m.Snippet, m.Metadata)
}
// Finder retrieves notes matching the given Filter.
type Finder interface {
Find(callback func(Match) error, filters ...Filter) error
@ -62,27 +70,27 @@ func List(opts ListOpts, deps ListDeps, callback func(formattedNote string) erro
var templates = map[string]string{
"path": `{{path}}`,
"oneline": `{{style "path" path}} {{style "title" title}} ({{date created "elapsed"}})`,
"oneline": `{{style "title" title}} {{style "path" path}} ({{date created "elapsed"}})`,
"short": `{{style "path" path}} {{style "title" title}} ({{date created "elapsed"}})
"short": `{{style "title" title}} {{style "path" path}} ({{date created "elapsed"}})
{{prepend " " snippet}}
`,
"medium": `{{style "path" path}} {{style "title" title}}
"medium": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
{{prepend " " snippet}}
`,
"long": `{{style "path" path}} {{style "title" title}}
"long": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
Modified: {{date created "short"}}
{{prepend " " snippet}}
`,
"full": `{{style "path" path}} {{style "title" title}}
"full": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
Modified: {{date created "short"}}

Loading…
Cancel
Save