2021-01-05 19:50:39 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2021-01-25 20:44:44 +00:00
|
|
|
"database/sql"
|
2021-01-05 19:50:39 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
"github.com/mickael-menu/zk/internal/core"
|
|
|
|
"github.com/mickael-menu/zk/internal/util"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/opt"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/paths"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/test/assert"
|
2021-01-05 19:50:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoteDAOIndexed(t *testing.T) {
|
2021-04-04 13:31:54 +00:00
|
|
|
testNoteDAOWithFixtures(t, "", func(tx Transaction, dao *NoteDAO) {
|
2021-04-14 18:14:01 +00:00
|
|
|
for _, note := range []core.Note{
|
2021-01-05 19:50:39 +00:00
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "a.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2020, 1, 20, 8, 52, 42, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "dir1/a.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "b.md",
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "dir1/b.md",
|
|
|
|
Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "dir1/dir1/a.md",
|
|
|
|
Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "dir2/a.md",
|
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "dir1 a space/a.md",
|
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "Dir3/a.md",
|
|
|
|
Modified: time.Date(2019, 11, 12, 20, 34, 6, 0, time.UTC),
|
|
|
|
},
|
|
|
|
} {
|
2021-01-25 20:44:44 +00:00
|
|
|
_, err := dao.Add(note)
|
|
|
|
assert.Nil(t, err)
|
2021-01-17 16:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We check that the metadata are sorted by the path but not
|
|
|
|
// lexicographically. Instead it needs to be sorted on each path
|
|
|
|
// component, like filepath.Walk would.
|
|
|
|
expected := []paths.Metadata{
|
|
|
|
{
|
|
|
|
Path: "Dir3/a.md",
|
|
|
|
Modified: time.Date(2019, 11, 12, 20, 34, 6, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "a.md",
|
|
|
|
Modified: time.Date(2020, 1, 20, 8, 52, 42, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "b.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "dir1/a.md",
|
|
|
|
Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "dir1/b.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "dir1/dir1/a.md",
|
2021-01-12 19:54:08 +00:00
|
|
|
Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "dir1 a space/a.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-17 16:37:16 +00:00
|
|
|
Path: "dir2/a.md",
|
2021-01-12 19:33:55 +00:00
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := dao.Indexed()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-11 20:51:50 +00:00
|
|
|
actual := make([]paths.Metadata, 0)
|
|
|
|
for a := range c {
|
|
|
|
actual = append(actual, a)
|
2021-01-05 19:50:39 +00:00
|
|
|
}
|
2021-01-11 20:51:50 +00:00
|
|
|
assert.Equal(t, actual, expected)
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOAdd(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-04-14 18:14:01 +00:00
|
|
|
_, err := dao.Add(core.Note{
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "log/added.md",
|
|
|
|
Title: "Added note",
|
|
|
|
Lead: "Note",
|
|
|
|
Body: "Note body",
|
|
|
|
RawContent: "# Added note\nNote body",
|
|
|
|
WordCount: 2,
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{"key": "value"},
|
2021-01-16 19:18:03 +00:00
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC),
|
|
|
|
Checksum: "check",
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-09 11:17:15 +00:00
|
|
|
row, err := queryNoteRow(tx, `path = "log/added.md"`)
|
2021-01-05 19:50:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, row, noteRow{
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "log/added.md",
|
|
|
|
Title: "Added note",
|
|
|
|
Lead: "Note",
|
|
|
|
Body: "Note body",
|
|
|
|
RawContent: "# Added note\nNote body",
|
|
|
|
WordCount: 2,
|
|
|
|
Checksum: "check",
|
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC),
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: `{"key":"value"}`,
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we can't add a duplicate note with an existing path.
|
|
|
|
func TestNoteDAOAddExistingNote(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-04-14 18:14:01 +00:00
|
|
|
_, err := dao.Add(core.Note{Path: "ref/test/a.md"})
|
2021-03-06 18:38:52 +00:00
|
|
|
assert.Err(t, err, "UNIQUE constraint failed: notes.path")
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOUpdate(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-04-14 18:14:01 +00:00
|
|
|
id, err := dao.Update(core.Note{
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "ref/test/a.md",
|
|
|
|
Title: "Updated note",
|
|
|
|
Lead: "Updated lead",
|
|
|
|
Body: "Updated body",
|
|
|
|
RawContent: "Updated raw content",
|
|
|
|
Checksum: "updated checksum",
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{"updated-key": "updated-value"},
|
2021-01-16 19:18:03 +00:00
|
|
|
WordCount: 42,
|
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC),
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
2021-04-14 18:14:01 +00:00
|
|
|
assert.Equal(t, id, core.NoteID(6))
|
2021-01-05 19:50:39 +00:00
|
|
|
|
2021-01-09 11:17:15 +00:00
|
|
|
row, err := queryNoteRow(tx, `path = "ref/test/a.md"`)
|
2021-01-05 19:50:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, row, noteRow{
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "ref/test/a.md",
|
|
|
|
Title: "Updated note",
|
|
|
|
Lead: "Updated lead",
|
|
|
|
Body: "Updated body",
|
|
|
|
RawContent: "Updated raw content",
|
|
|
|
Checksum: "updated checksum",
|
|
|
|
WordCount: 42,
|
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC),
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: `{"updated-key":"updated-value"}`,
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOUpdateUnknown(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-04-14 18:14:01 +00:00
|
|
|
_, err := dao.Update(core.Note{
|
2021-01-09 11:17:15 +00:00
|
|
|
Path: "unknown/unknown.md",
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
2021-03-06 18:38:52 +00:00
|
|
|
assert.Err(t, err, "note not found in the index")
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAORemove(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-01-25 20:44:44 +00:00
|
|
|
_, err := queryNoteRow(tx, `path = "ref/test/a.md"`)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = dao.Remove("ref/test/a.md")
|
2021-01-05 19:50:39 +00:00
|
|
|
assert.Nil(t, err)
|
2021-01-25 20:44:44 +00:00
|
|
|
|
|
|
|
_, err = queryNoteRow(tx, `path = "ref/test/a.md"`)
|
|
|
|
assert.Equal(t, err, sql.ErrNoRows)
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAORemoveUnknown(t *testing.T) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-01-09 11:17:15 +00:00
|
|
|
err := dao.Remove("unknown/unknown.md")
|
2021-03-06 18:38:52 +00:00
|
|
|
assert.Err(t, err, "note not found in the index")
|
2021-01-05 19:50:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:44:44 +00:00
|
|
|
// Also remove the outbound links, and set the target_id of inbound links to NULL.
|
|
|
|
func TestNoteDAORemoveCascadeLinks(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
links := queryLinkRows(t, tx, `source_id = 1`)
|
|
|
|
assert.Equal(t, len(links) > 0, true)
|
|
|
|
|
|
|
|
links = queryLinkRows(t, tx, `id = 4`)
|
2021-04-14 18:14:01 +00:00
|
|
|
assert.Equal(t, *links[0].TargetId, core.NoteID(1))
|
2021-01-25 20:44:44 +00:00
|
|
|
|
|
|
|
err := dao.Remove("log/2021-01-03.md")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
links = queryLinkRows(t, tx, `source_id = 1`)
|
|
|
|
assert.Equal(t, len(links), 0)
|
|
|
|
|
|
|
|
links = queryLinkRows(t, tx, `id = 4`)
|
|
|
|
assert.Nil(t, links[0].TargetId)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:50:21 +00:00
|
|
|
func TestNoteDAOFindIdsByHref(t *testing.T) {
|
|
|
|
test := func(href string, allowPartialHref bool, expected []core.NoteID) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
actual, err := dao.FindIdsByHref(href, allowPartialHref)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, actual, expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
test("test", false, []core.NoteID{})
|
|
|
|
test("test", true, []core.NoteID{6, 5, 8})
|
|
|
|
|
|
|
|
// Filename takes precedence over the rest of the path.
|
|
|
|
// See https://github.com/mickael-menu/zk/issues/111
|
|
|
|
test("ref", true, []core.NoteID{8})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindIncludingHrefs(t *testing.T) {
|
|
|
|
test := func(href string, allowPartialHref bool, expected []string) {
|
|
|
|
testNoteDAOFindPaths(t,
|
|
|
|
core.NoteFindOpts{
|
|
|
|
IncludeHrefs: []string{href},
|
|
|
|
AllowPartialHrefs: allowPartialHref,
|
|
|
|
},
|
|
|
|
expected,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("test", false, []string{})
|
|
|
|
test("test", true, []string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md"})
|
|
|
|
|
|
|
|
// Filename takes precedence over the rest of the path.
|
|
|
|
// See https://github.com/mickael-menu/zk/issues/111
|
|
|
|
test("ref", true, []string{"ref/test/ref.md"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindExcludingHrefs(t *testing.T) {
|
|
|
|
test := func(href string, allowPartialHref bool, expected []string) {
|
|
|
|
testNoteDAOFindPaths(t,
|
|
|
|
core.NoteFindOpts{
|
|
|
|
ExcludeHrefs: []string{href},
|
|
|
|
AllowPartialHrefs: allowPartialHref,
|
|
|
|
},
|
|
|
|
expected,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("test", false, []string{"ref/test/ref.md", "ref/test/b.md",
|
|
|
|
"f39c8.md", "ref/test/a.md", "log/2021-01-03.md", "log/2021-02-04.md",
|
|
|
|
"index.md", "log/2021-01-04.md"})
|
|
|
|
test("test", true, []string{"f39c8.md", "log/2021-01-03.md",
|
|
|
|
"log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
|
|
|
|
|
|
|
|
// Filename takes precedence over the rest of the path.
|
|
|
|
// See https://github.com/mickael-menu/zk/issues/111
|
|
|
|
test("ref", true, []string{"ref/test/b.md", "f39c8.md", "ref/test/a.md",
|
|
|
|
"log/2021-01-03.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func TestNoteDAOFindMinimalAll(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
notes, err := dao.FindMinimal(core.NoteFindOpts{})
|
|
|
|
assert.Nil(t, err)
|
2021-04-04 13:31:54 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
assert.Equal(t, notes, []core.MinimalNote{
|
2021-11-28 20:50:21 +00:00
|
|
|
{ID: 8, Path: "ref/test/ref.md", Title: "", Metadata: map[string]interface{}{}},
|
2021-09-25 17:28:29 +00:00
|
|
|
{ID: 5, Path: "ref/test/b.md", Title: "A nested note", Metadata: map[string]interface{}{}},
|
|
|
|
{ID: 4, Path: "f39c8.md", Title: "An interesting note", Metadata: map[string]interface{}{}},
|
|
|
|
{ID: 6, Path: "ref/test/a.md", Title: "Another nested note", Metadata: map[string]interface{}{
|
|
|
|
"alias": "a.md",
|
|
|
|
}},
|
|
|
|
{ID: 1, Path: "log/2021-01-03.md", Title: "Daily note", Metadata: map[string]interface{}{
|
|
|
|
"author": "Dom",
|
|
|
|
}},
|
|
|
|
{ID: 7, Path: "log/2021-02-04.md", Title: "February 4, 2021", Metadata: map[string]interface{}{}},
|
|
|
|
{ID: 3, Path: "index.md", Title: "Index", Metadata: map[string]interface{}{
|
|
|
|
"aliases": []interface{}{"First page"},
|
|
|
|
}},
|
|
|
|
{ID: 2, Path: "log/2021-01-04.md", Title: "January 4, 2021", Metadata: map[string]interface{}{}},
|
2021-04-14 18:14:01 +00:00
|
|
|
})
|
2021-04-04 13:31:54 +00:00
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
2021-04-04 13:31:54 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func TestNoteDAOFindMinimalWithFilter(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
notes, err := dao.FindMinimal(core.NoteFindOpts{
|
2022-11-27 14:56:18 +00:00
|
|
|
Match: []string{"daily | index"},
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Sorters: []core.NoteSorter{{Field: core.NoteSortWordCount, Ascending: true}},
|
|
|
|
Limit: 3,
|
2021-04-14 18:14:01 +00:00
|
|
|
})
|
2021-04-04 13:31:54 +00:00
|
|
|
assert.Nil(t, err)
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
assert.Equal(t, notes, []core.MinimalNote{
|
2021-09-25 17:28:29 +00:00
|
|
|
{ID: 1, Path: "log/2021-01-03.md", Title: "Daily note", Metadata: map[string]interface{}{
|
|
|
|
"author": "Dom",
|
|
|
|
}},
|
|
|
|
{ID: 3, Path: "index.md", Title: "Index", Metadata: map[string]interface{}{
|
|
|
|
"aliases": []interface{}{"First page"},
|
|
|
|
}},
|
|
|
|
{ID: 7, Path: "log/2021-02-04.md", Title: "February 4, 2021", Metadata: map[string]interface{}{}},
|
2021-04-14 18:14:01 +00:00
|
|
|
})
|
2021-04-04 13:31:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:29:57 +00:00
|
|
|
func TestNoteDAOFindAll(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindPaths(t, core.NoteFindOpts{}, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "log/2021-01-03.md",
|
2021-03-13 14:31:05 +00:00
|
|
|
"log/2021-02-04.md", "index.md", "log/2021-01-04.md",
|
2021-01-10 11:29:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-11 21:22:23 +00:00
|
|
|
func TestNoteDAOFindLimit(t *testing.T) {
|
2021-11-28 20:50:21 +00:00
|
|
|
testNoteDAOFindPaths(t, core.NoteFindOpts{Limit: 3}, []string{
|
|
|
|
"ref/test/ref.md",
|
2021-01-12 19:54:08 +00:00
|
|
|
"ref/test/b.md",
|
|
|
|
"f39c8.md",
|
2021-01-11 21:22:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:00:09 +00:00
|
|
|
func TestNoteDAOFindTag(t *testing.T) {
|
|
|
|
test := func(tags []string, expectedPaths []string) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindPaths(t, core.NoteFindOpts{Tags: tags}, expectedPaths)
|
2021-03-07 16:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test([]string{"fiction"}, []string{"log/2021-01-03.md"})
|
|
|
|
test([]string{" adventure "}, []string{"ref/test/b.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction", "adventure"}, []string{"log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction|fantasy"}, []string{"f39c8.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction | fantasy"}, []string{"f39c8.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction OR fantasy"}, []string{"f39c8.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction | adventure | fantasy"}, []string{"ref/test/b.md", "f39c8.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction | history", "adventure"}, []string{"ref/test/b.md", "log/2021-01-03.md"})
|
|
|
|
test([]string{"fiction", "unknown"}, []string{})
|
2021-11-28 20:50:21 +00:00
|
|
|
test([]string{"-fiction"}, []string{"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
|
|
|
|
test([]string{"NOT fiction"}, []string{"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
|
|
|
|
test([]string{"NOTfiction"}, []string{"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
|
2021-03-07 16:00:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:22:23 +00:00
|
|
|
func TestNoteDAOFindMatch(t *testing.T) {
|
|
|
|
testNoteDAOFind(t,
|
2022-05-28 18:03:06 +00:00
|
|
|
core.NoteFindOpts{
|
2022-11-27 14:56:18 +00:00
|
|
|
Match: []string{"daily | index"},
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
},
|
2021-04-14 18:14:01 +00:00
|
|
|
[]core.ContextualNote{
|
2021-01-11 21:22:23 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 3,
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "index.md",
|
|
|
|
Title: "Index",
|
|
|
|
Lead: "Index of the Zettelkasten",
|
|
|
|
Body: "Index of the Zettelkasten",
|
|
|
|
RawContent: "# Index\nIndex of the Zettelkasten",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-07 16:00:09 +00:00
|
|
|
Tags: []string{},
|
2021-03-13 14:31:05 +00:00
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"aliases": []interface{}{"First page"},
|
|
|
|
},
|
|
|
|
Created: time.Date(2019, 12, 4, 11, 59, 11, 0, time.UTC),
|
|
|
|
Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC),
|
|
|
|
Checksum: "iaefhv",
|
2021-01-11 21:22:23 +00:00
|
|
|
},
|
2021-01-30 17:08:02 +00:00
|
|
|
Snippets: []string{"<zk:match>Index</zk:match> of the Zettelkasten"},
|
2021-01-11 21:22:23 +00:00
|
|
|
},
|
2021-03-13 14:31:05 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 1,
|
2021-03-13 14:31:05 +00:00
|
|
|
Path: "log/2021-01-03.md",
|
|
|
|
Title: "Daily note",
|
|
|
|
Lead: "A daily note",
|
|
|
|
Body: "A daily note\n\nWith lot of content",
|
2021-04-17 11:06:23 +00:00
|
|
|
RawContent: "# Daily note\nA note\n\nWith lot of content",
|
2021-03-13 14:31:05 +00:00
|
|
|
WordCount: 3,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-13 14:31:05 +00:00
|
|
|
Tags: []string{"fiction", "adventure"},
|
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"author": "Dom",
|
|
|
|
},
|
|
|
|
Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Checksum: "qwfpgj",
|
|
|
|
},
|
|
|
|
Snippets: []string{"A <zk:match>daily</zk:match> note\n\nWith lot of content"},
|
|
|
|
},
|
2021-01-11 21:22:23 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 7,
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "log/2021-02-04.md",
|
|
|
|
Title: "February 4, 2021",
|
|
|
|
Lead: "A third daily note",
|
|
|
|
Body: "A third daily note",
|
|
|
|
RawContent: "# A third daily note",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-07 16:00:09 +00:00
|
|
|
Tags: []string{},
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{},
|
2021-01-16 19:18:03 +00:00
|
|
|
Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC),
|
|
|
|
Checksum: "earkte",
|
2021-01-11 21:22:23 +00:00
|
|
|
},
|
2021-01-30 17:08:02 +00:00
|
|
|
Snippets: []string{"A third <zk:match>daily</zk:match> note"},
|
2021-01-11 21:22:23 +00:00
|
|
|
},
|
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 2,
|
2021-01-16 19:18:03 +00:00
|
|
|
Path: "log/2021-01-04.md",
|
|
|
|
Title: "January 4, 2021",
|
|
|
|
Lead: "A second daily note",
|
|
|
|
Body: "A second daily note",
|
|
|
|
RawContent: "# A second daily note",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-07 16:00:09 +00:00
|
|
|
Tags: []string{},
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{},
|
2021-01-16 19:18:03 +00:00
|
|
|
Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Checksum: "arstde",
|
2021-01-11 21:22:23 +00:00
|
|
|
},
|
2021-01-30 17:08:02 +00:00
|
|
|
Snippets: []string{"A second <zk:match>daily</zk:match> note"},
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-11 21:22:23 +00:00
|
|
|
)
|
2021-01-11 20:51:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 14:56:18 +00:00
|
|
|
func TestNoteDAOFindMatchWithMultiMatch(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
|
|
|
core.NoteFindOpts{
|
|
|
|
Match: []string{"daily | index", "second"},
|
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Sorters: []core.NoteSorter{
|
|
|
|
{Field: core.NoteSortPath, Ascending: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"log/2021-01-04.md",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-27 10:23:16 +00:00
|
|
|
func TestNoteDAOFindMatchWithSort(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2022-11-27 14:56:18 +00:00
|
|
|
Match: []string{"daily | index"},
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
2021-04-14 18:14:01 +00:00
|
|
|
Sorters: []core.NoteSorter{
|
|
|
|
{Field: core.NoteSortPath, Ascending: false},
|
2021-02-27 10:23:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"log/2021-02-04.md",
|
|
|
|
"log/2021-01-04.md",
|
|
|
|
"log/2021-01-03.md",
|
|
|
|
"index.md",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:06:23 +00:00
|
|
|
func TestNoteDAOFindExactMatch(t *testing.T) {
|
|
|
|
test := func(match string, expected []string) {
|
|
|
|
testNoteDAOFindPaths(t,
|
|
|
|
core.NoteFindOpts{
|
2022-11-27 14:56:18 +00:00
|
|
|
Match: []string{match},
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyExact,
|
2021-04-17 11:06:23 +00:00
|
|
|
},
|
|
|
|
expected,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case insensitive
|
|
|
|
test("dailY NOTe", []string{"log/2021-01-03.md", "log/2021-02-04.md", "log/2021-01-04.md"})
|
|
|
|
// Special characters
|
|
|
|
test(`[exact% ch\ar_acters]`, []string{"ref/test/a.md"})
|
|
|
|
}
|
|
|
|
|
2022-05-28 18:03:06 +00:00
|
|
|
func TestNoteDAOFindMentionRequiresFtsMatchStrategy(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
_, err := dao.Find(core.NoteFindOpts{
|
|
|
|
MatchStrategy: core.MatchStrategyExact,
|
|
|
|
Mention: []string{"mention"},
|
|
|
|
})
|
|
|
|
assert.Err(t, err, "--mention can only be used with --match-strategy=fts")
|
|
|
|
})
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
_, err := dao.Find(core.NoteFindOpts{
|
|
|
|
MatchStrategy: core.MatchStrategyRe,
|
|
|
|
Mention: []string{"mention"},
|
|
|
|
})
|
|
|
|
assert.Err(t, err, "--mention can only be used with --match-strategy=fts")
|
|
|
|
})
|
2021-04-17 11:06:23 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
_, err := dao.Find(core.NoteFindOpts{
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Mention: []string{"mention"},
|
2021-04-17 11:06:23 +00:00
|
|
|
})
|
2022-05-28 18:03:06 +00:00
|
|
|
assert.Err(t, err, "could not find notes at: mention")
|
2021-04-17 11:06:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:23:03 +00:00
|
|
|
func TestNoteDAOFindInPathAbsoluteFile(t *testing.T) {
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
IncludeHrefs: []string{"log/2021-01-03.md"},
|
2021-02-27 18:23:03 +00:00
|
|
|
},
|
|
|
|
[]string{"log/2021-01-03.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can look for files with only their prefix.
|
|
|
|
func TestNoteDAOFindInPathWithFilePrefix(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
IncludeHrefs: []string{"log/2021-01"},
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
2021-01-12 19:54:08 +00:00
|
|
|
[]string{"log/2021-01-03.md", "log/2021-01-04.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:23:03 +00:00
|
|
|
// For directory, only complete names work, no prefixes.
|
|
|
|
func TestNoteDAOFindInPathRequiresCompleteDirName(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
IncludeHrefs: []string{"lo"},
|
|
|
|
AllowPartialHrefs: false,
|
2021-02-27 18:23:03 +00:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
IncludeHrefs: []string{"log"},
|
|
|
|
AllowPartialHrefs: false,
|
2021-02-27 18:23:03 +00:00
|
|
|
},
|
2021-03-13 14:31:05 +00:00
|
|
|
[]string{"log/2021-01-03.md", "log/2021-02-04.md", "log/2021-01-04.md"},
|
2021-02-27 18:23:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can look for multiple paths, in which case notes can be in any of them.
|
2021-01-13 18:53:15 +00:00
|
|
|
func TestNoteDAOFindInMultiplePaths(t *testing.T) {
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
IncludeHrefs: []string{"ref", "index.md"},
|
2021-01-12 19:54:08 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "index.md"},
|
2021-01-12 19:54:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:53:15 +00:00
|
|
|
func TestNoteDAOFindExcludingPath(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
ExcludeHrefs: []string{"log"},
|
2021-01-13 18:53:15 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "index.md"},
|
2021-01-13 18:53:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindExcludingMultiplePaths(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
ExcludeHrefs: []string{"ref", "log/2021-01"},
|
2021-01-13 18:53:15 +00:00
|
|
|
},
|
|
|
|
[]string{"f39c8.md", "log/2021-02-04.md", "index.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-13 14:31:05 +00:00
|
|
|
func TestNoteDAOFindMentions(t *testing.T) {
|
|
|
|
testNoteDAOFind(t,
|
2022-05-28 18:03:06 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Mention: []string{"log/2021-01-03.md", "index.md"},
|
|
|
|
},
|
2021-04-14 18:14:01 +00:00
|
|
|
[]core.ContextualNote{
|
2021-03-13 14:31:05 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 5,
|
2021-03-13 14:31:05 +00:00
|
|
|
Path: "ref/test/b.md",
|
|
|
|
Title: "A nested note",
|
|
|
|
Lead: "This one is in a sub sub directory",
|
|
|
|
Body: "This one is in a sub sub directory, not the first page",
|
|
|
|
RawContent: "# A nested note\nThis one is in a sub sub directory",
|
|
|
|
WordCount: 8,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-10-03 16:36:59 +00:00
|
|
|
Tags: []string{"adventure", "history", "science"},
|
2021-03-13 14:31:05 +00:00
|
|
|
Metadata: map[string]interface{}{},
|
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
|
|
|
Checksum: "yvwbae",
|
|
|
|
},
|
|
|
|
Snippets: []string{"This one is in a sub sub directory, not the <zk:match>first page</zk:match>"},
|
|
|
|
},
|
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 7,
|
2021-03-13 14:31:05 +00:00
|
|
|
Path: "log/2021-02-04.md",
|
|
|
|
Title: "February 4, 2021",
|
|
|
|
Lead: "A third daily note",
|
|
|
|
Body: "A third daily note",
|
|
|
|
RawContent: "# A third daily note",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-13 14:31:05 +00:00
|
|
|
Tags: []string{},
|
|
|
|
Metadata: map[string]interface{}{},
|
|
|
|
Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC),
|
|
|
|
Checksum: "earkte",
|
|
|
|
},
|
|
|
|
Snippets: []string{"A third <zk:match>daily note</zk:match>"},
|
|
|
|
},
|
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 2,
|
2021-03-13 14:31:05 +00:00
|
|
|
Path: "log/2021-01-04.md",
|
|
|
|
Title: "January 4, 2021",
|
|
|
|
Lead: "A second daily note",
|
|
|
|
Body: "A second daily note",
|
|
|
|
RawContent: "# A second daily note",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-13 14:31:05 +00:00
|
|
|
Tags: []string{},
|
|
|
|
Metadata: map[string]interface{}{},
|
|
|
|
Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC),
|
|
|
|
Checksum: "arstde",
|
|
|
|
},
|
|
|
|
Snippets: []string{"A second <zk:match>daily note</zk:match>"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common use case: `--mention x --no-link-to x`
|
|
|
|
func TestNoteDAOFindUnlinkedMentions(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Mention: []string{"log/2021-01-03.md", "index.md"},
|
2021-04-14 18:14:01 +00:00
|
|
|
LinkTo: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-03.md", "index.md"},
|
2021-03-13 14:31:05 +00:00
|
|
|
Negate: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"ref/test/b.md", "log/2021-02-04.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-11 17:53:07 +00:00
|
|
|
func TestNoteDAOFindMentionUnknown(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
opts := core.NoteFindOpts{
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
Mention: []string{"will-not-be-found"},
|
2021-12-11 17:53:07 +00:00
|
|
|
}
|
|
|
|
_, err := dao.Find(opts)
|
|
|
|
assert.Err(t, err, "could not find notes at: will-not-be-found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-20 18:17:46 +00:00
|
|
|
func TestNoteDAOFindMentionedBy(t *testing.T) {
|
|
|
|
testNoteDAOFind(t,
|
2022-05-28 18:03:06 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
MentionedBy: []string{"ref/test/b.md", "log/2021-01-04.md"},
|
|
|
|
},
|
2021-04-14 18:14:01 +00:00
|
|
|
[]core.ContextualNote{
|
2021-03-20 18:17:46 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 1,
|
2021-03-20 18:17:46 +00:00
|
|
|
Path: "log/2021-01-03.md",
|
|
|
|
Title: "Daily note",
|
|
|
|
Lead: "A daily note",
|
|
|
|
Body: "A daily note\n\nWith lot of content",
|
2021-04-17 11:06:23 +00:00
|
|
|
RawContent: "# Daily note\nA note\n\nWith lot of content",
|
2021-03-20 18:17:46 +00:00
|
|
|
WordCount: 3,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-20 18:17:46 +00:00
|
|
|
Tags: []string{"fiction", "adventure"},
|
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"author": "Dom",
|
|
|
|
},
|
|
|
|
Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Checksum: "qwfpgj",
|
|
|
|
},
|
|
|
|
Snippets: []string{"A second <zk:match>daily note</zk:match>"},
|
|
|
|
},
|
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 3,
|
2021-03-20 18:17:46 +00:00
|
|
|
Path: "index.md",
|
|
|
|
Title: "Index",
|
|
|
|
Lead: "Index of the Zettelkasten",
|
|
|
|
Body: "Index of the Zettelkasten",
|
|
|
|
RawContent: "# Index\nIndex of the Zettelkasten",
|
|
|
|
WordCount: 4,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-20 18:17:46 +00:00
|
|
|
Tags: []string{},
|
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"aliases": []interface{}{
|
|
|
|
"First page",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Created: time.Date(2019, 12, 4, 11, 59, 11, 0, time.UTC),
|
|
|
|
Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC),
|
|
|
|
Checksum: "iaefhv",
|
|
|
|
},
|
|
|
|
Snippets: []string{"This one is in a sub sub directory, not the <zk:match>first page</zk:match>"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common use case: `--mentioned-by x --no-linked-by x`
|
|
|
|
func TestNoteDAOFindUnlinkedMentionedBy(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
MentionedBy: []string{"ref/test/b.md", "log/2021-01-04.md"},
|
2021-04-14 18:14:01 +00:00
|
|
|
LinkedBy: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"ref/test/b.md", "log/2021-01-04.md"},
|
2021-03-20 18:17:46 +00:00
|
|
|
Negate: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"log/2021-01-03.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-11 17:53:07 +00:00
|
|
|
func TestNoteDAOFindMentionedByUnknown(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
opts := core.NoteFindOpts{
|
2022-05-28 18:03:06 +00:00
|
|
|
MatchStrategy: core.MatchStrategyFts,
|
|
|
|
MentionedBy: []string{"will-not-be-found"},
|
2021-12-11 17:53:07 +00:00
|
|
|
}
|
|
|
|
_, err := dao.Find(opts)
|
|
|
|
assert.Err(t, err, "could not find notes at: will-not-be-found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:57:12 +00:00
|
|
|
func TestNoteDAOFindLinkedBy(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkedBy: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"f39c8.md", "log/2021-01-03"},
|
2021-01-31 11:24:33 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: false,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-25 21:57:12 +00:00
|
|
|
},
|
|
|
|
[]string{"ref/test/a.md", "log/2021-01-03.md", "log/2021-01-04.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:24:33 +00:00
|
|
|
func TestNoteDAOFindLinkedByRecursive(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkedBy: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-04.md"},
|
2021-01-31 11:24:33 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: true,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-31 11:24:33 +00:00
|
|
|
},
|
|
|
|
[]string{"index.md", "f39c8.md", "ref/test/a.md", "log/2021-01-03.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:07:28 +00:00
|
|
|
func TestNoteDAOFindLinkedByRecursiveWithMaxDistance(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkedBy: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-04.md"},
|
2021-01-31 16:07:28 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: true,
|
|
|
|
MaxDistance: 2,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-31 16:07:28 +00:00
|
|
|
},
|
|
|
|
[]string{"index.md", "f39c8.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:08:02 +00:00
|
|
|
func TestNoteDAOFindLinkedByWithSnippets(t *testing.T) {
|
|
|
|
testNoteDAOFind(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
LinkedBy: &core.LinkFilter{Hrefs: []string{"f39c8.md"}},
|
2021-01-30 17:08:02 +00:00
|
|
|
},
|
2021-04-14 18:14:01 +00:00
|
|
|
[]core.ContextualNote{
|
2021-01-30 17:08:02 +00:00
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 6,
|
2021-01-30 17:08:02 +00:00
|
|
|
Path: "ref/test/a.md",
|
|
|
|
Title: "Another nested note",
|
|
|
|
Lead: "It shall appear before b.md",
|
|
|
|
Body: "It shall appear before b.md",
|
2021-04-17 11:06:23 +00:00
|
|
|
RawContent: "#Another nested note\nIt shall appear before b.md\nMatch [exact% ch\\ar_acters]",
|
2021-01-30 17:08:02 +00:00
|
|
|
WordCount: 5,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-07 16:00:09 +00:00
|
|
|
Tags: []string{},
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"alias": "a.md",
|
|
|
|
},
|
|
|
|
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC),
|
|
|
|
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.UTC),
|
|
|
|
Checksum: "iecywst",
|
2021-01-30 17:08:02 +00:00
|
|
|
},
|
|
|
|
Snippets: []string{
|
|
|
|
"[[<zk:match>Link from 4 to 6</zk:match>]]",
|
|
|
|
"[[<zk:match>Duplicated link</zk:match>]]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-14 18:14:01 +00:00
|
|
|
Note: core.Note{
|
|
|
|
ID: 1,
|
2021-01-30 17:08:02 +00:00
|
|
|
Path: "log/2021-01-03.md",
|
2021-03-13 14:31:05 +00:00
|
|
|
Title: "Daily note",
|
2021-01-30 17:08:02 +00:00
|
|
|
Lead: "A daily note",
|
|
|
|
Body: "A daily note\n\nWith lot of content",
|
2021-04-17 11:06:23 +00:00
|
|
|
RawContent: "# Daily note\nA note\n\nWith lot of content",
|
2021-01-30 17:08:02 +00:00
|
|
|
WordCount: 3,
|
2021-04-14 18:14:01 +00:00
|
|
|
Links: []core.Link{},
|
2021-03-07 16:00:09 +00:00
|
|
|
Tags: []string{"fiction", "adventure"},
|
2021-03-08 20:38:32 +00:00
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
"author": "Dom",
|
|
|
|
},
|
|
|
|
Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC),
|
|
|
|
Checksum: "qwfpgj",
|
2021-01-30 17:08:02 +00:00
|
|
|
},
|
|
|
|
Snippets: []string{
|
|
|
|
"[[<zk:match>Another link</zk:match>]]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-11 17:53:07 +00:00
|
|
|
func TestNoteDAOFindLinkedByUnknown(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
opts := core.NoteFindOpts{
|
|
|
|
LinkedBy: &core.LinkFilter{
|
|
|
|
Hrefs: []string{"will-not-be-found"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := dao.Find(opts)
|
|
|
|
assert.Err(t, err, "could not find notes at: will-not-be-found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-27 20:25:33 +00:00
|
|
|
func TestNoteDAOFindNotLinkedBy(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkedBy: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"f39c8.md", "log/2021-01-03"},
|
2021-01-31 11:24:33 +00:00
|
|
|
Negate: true,
|
|
|
|
Recursive: false,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-27 20:25:33 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "log/2021-02-04.md", "index.md"},
|
2021-01-27 20:25:33 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:14:07 +00:00
|
|
|
func TestNoteDAOFindLinkTo(t *testing.T) {
|
2021-01-27 19:20:26 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkTo: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-04", "ref/test/a.md"},
|
2021-01-31 11:24:33 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: false,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-27 19:20:26 +00:00
|
|
|
},
|
|
|
|
[]string{"f39c8.md", "log/2021-01-03.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:14:07 +00:00
|
|
|
func TestNoteDAOFindLinkToRecursive(t *testing.T) {
|
2021-01-31 11:24:33 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkTo: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-04.md"},
|
2021-01-31 11:24:33 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: true,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-31 11:24:33 +00:00
|
|
|
},
|
|
|
|
[]string{"log/2021-01-03.md", "f39c8.md", "index.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:14:07 +00:00
|
|
|
func TestNoteDAOFindLinkToRecursiveWithMaxDistance(t *testing.T) {
|
2021-01-31 16:07:28 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
LinkTo: &core.LinkFilter{
|
2021-11-28 20:50:21 +00:00
|
|
|
Hrefs: []string{"log/2021-01-04.md"},
|
2021-01-31 16:07:28 +00:00
|
|
|
Negate: false,
|
|
|
|
Recursive: true,
|
|
|
|
MaxDistance: 2,
|
2021-03-13 14:31:05 +00:00
|
|
|
},
|
2021-01-31 16:07:28 +00:00
|
|
|
},
|
|
|
|
[]string{"log/2021-01-03.md", "f39c8.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:14:07 +00:00
|
|
|
func TestNoteDAOFindNotLinkTo(t *testing.T) {
|
2021-01-27 20:25:33 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-11-28 20:50:21 +00:00
|
|
|
LinkTo: &core.LinkFilter{Hrefs: []string{"log/2021-01-04", "ref/test/a.md"}, Negate: true},
|
2021-01-27 20:25:33 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"},
|
2021-01-27 20:25:33 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-11 17:53:07 +00:00
|
|
|
func TestNoteDAOFindLinkToUnknown(t *testing.T) {
|
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
|
|
|
opts := core.NoteFindOpts{
|
|
|
|
LinkTo: &core.LinkFilter{
|
|
|
|
Hrefs: []string{"will-not-be-found"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := dao.Find(opts)
|
|
|
|
assert.Err(t, err, "could not find notes at: will-not-be-found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:04:44 +00:00
|
|
|
func TestNoteDAOFindRelated(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
Related: []string{"log/2021-02-04"},
|
2021-02-01 21:04:44 +00:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
Related: []string{"log/2021-01-03.md"},
|
2021-02-01 21:04:44 +00:00
|
|
|
},
|
|
|
|
[]string{"index.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:40:06 +00:00
|
|
|
func TestNoteDAOFindOrphan(t *testing.T) {
|
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{Orphan: true},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "log/2021-02-04.md"},
|
2021-01-29 19:40:06 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-12 19:54:08 +00:00
|
|
|
func TestNoteDAOFindCreatedOn(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
start := time.Date(2020, 11, 22, 0, 0, 0, 0, time.UTC)
|
|
|
|
end := time.Date(2020, 11, 23, 0, 0, 0, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
CreatedStart: &start,
|
|
|
|
CreatedEnd: &end,
|
2021-01-12 19:54:08 +00:00
|
|
|
},
|
|
|
|
[]string{"log/2021-01-03.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindCreatedBefore(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
end := time.Date(2019, 12, 04, 11, 59, 11, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
CreatedEnd: &end,
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md"},
|
2021-01-11 21:22:23 +00:00
|
|
|
)
|
2021-01-11 20:51:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 19:54:08 +00:00
|
|
|
func TestNoteDAOFindCreatedAfter(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
start := time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
CreatedStart: &start,
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
2021-03-13 14:31:05 +00:00
|
|
|
[]string{"log/2021-01-03.md", "log/2021-02-04.md", "log/2021-01-04.md"},
|
2021-01-12 19:54:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindModifiedOn(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
start := time.Date(2020, 01, 20, 0, 0, 0, 0, time.UTC)
|
|
|
|
end := time.Date(2020, 01, 21, 0, 0, 0, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
ModifiedStart: &start,
|
|
|
|
ModifiedEnd: &end,
|
2021-01-12 19:54:08 +00:00
|
|
|
},
|
|
|
|
[]string{"f39c8.md"},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindModifiedBefore(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
end := time.Date(2020, 01, 20, 8, 52, 42, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
ModifiedEnd: &end,
|
2021-01-12 19:54:08 +00:00
|
|
|
},
|
2021-11-28 20:50:21 +00:00
|
|
|
[]string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "index.md"},
|
2021-01-12 19:54:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindModifiedAfter(t *testing.T) {
|
2021-03-13 14:31:05 +00:00
|
|
|
start := time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC)
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
2021-03-13 14:31:05 +00:00
|
|
|
ModifiedStart: &start,
|
2021-01-11 20:51:50 +00:00
|
|
|
},
|
2021-01-12 19:54:08 +00:00
|
|
|
[]string{"log/2021-01-03.md", "log/2021-01-04.md"},
|
2021-01-11 21:22:23 +00:00
|
|
|
)
|
2021-01-10 21:41:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 21:06:05 +00:00
|
|
|
func TestNoteDAOFindSortCreated(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortCreated, true, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "index.md", "f39c8.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-01-03.md", "log/2021-02-04.md", "log/2021-01-04.md",
|
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortCreated, false, []string{
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-02-04.md", "log/2021-01-04.md", "log/2021-01-03.md",
|
2021-11-28 20:50:21 +00:00
|
|
|
"f39c8.md", "index.md", "ref/test/ref.md", "ref/test/b.md", "ref/test/a.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindSortModified(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortModified, true, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "index.md", "f39c8.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-02-04.md", "log/2021-01-03.md", "log/2021-01-04.md",
|
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortModified, false, []string{
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-01-04.md", "log/2021-01-03.md", "log/2021-02-04.md",
|
2021-11-28 20:50:21 +00:00
|
|
|
"f39c8.md", "index.md", "ref/test/ref.md", "ref/test/b.md", "ref/test/a.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindSortPath(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortPath, true, []string{
|
2021-01-13 21:06:05 +00:00
|
|
|
"f39c8.md", "index.md", "log/2021-01-03.md", "log/2021-01-04.md",
|
2021-11-28 20:50:21 +00:00
|
|
|
"log/2021-02-04.md", "ref/test/a.md", "ref/test/b.md", "ref/test/ref.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortPath, false, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md", "log/2021-02-04.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-01-04.md", "log/2021-01-03.md", "index.md", "f39c8.md",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindSortTitle(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortTitle, true, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/ref.md", "ref/test/b.md", "f39c8.md", "ref/test/a.md", "log/2021-01-03.md",
|
2021-03-13 14:31:05 +00:00
|
|
|
"log/2021-02-04.md", "index.md", "log/2021-01-04.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortTitle, false, []string{
|
2021-03-13 14:31:05 +00:00
|
|
|
"log/2021-01-04.md", "index.md", "log/2021-02-04.md",
|
2021-11-28 20:50:21 +00:00
|
|
|
"log/2021-01-03.md", "ref/test/a.md", "f39c8.md", "ref/test/b.md", "ref/test/ref.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoteDAOFindSortWordCount(t *testing.T) {
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortWordCount, true, []string{
|
2021-01-13 21:06:05 +00:00
|
|
|
"log/2021-01-03.md", "log/2021-02-04.md", "index.md",
|
2021-11-28 20:50:21 +00:00
|
|
|
"log/2021-01-04.md", "ref/test/ref.md", "f39c8.md", "ref/test/a.md", "ref/test/b.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
})
|
2021-04-14 18:14:01 +00:00
|
|
|
testNoteDAOFindSort(t, core.NoteSortWordCount, false, []string{
|
2021-11-28 20:50:21 +00:00
|
|
|
"ref/test/b.md", "ref/test/ref.md", "f39c8.md", "ref/test/a.md", "log/2021-02-04.md",
|
2021-01-13 21:06:05 +00:00
|
|
|
"index.md", "log/2021-01-04.md", "log/2021-01-03.md",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func testNoteDAOFindSort(t *testing.T, field core.NoteSortField, ascending bool, expected []string) {
|
2021-01-13 21:06:05 +00:00
|
|
|
testNoteDAOFindPaths(t,
|
2021-04-14 18:14:01 +00:00
|
|
|
core.NoteFindOpts{
|
|
|
|
Sorters: []core.NoteSorter{{Field: field, Ascending: ascending}},
|
2021-01-13 21:06:05 +00:00
|
|
|
},
|
|
|
|
expected,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func testNoteDAOFindPaths(t *testing.T, opts core.NoteFindOpts, expected []string) {
|
2021-01-12 19:54:08 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-01-23 12:29:14 +00:00
|
|
|
matches, err := dao.Find(opts)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-12 19:54:08 +00:00
|
|
|
actual := make([]string, 0)
|
2021-01-23 12:29:14 +00:00
|
|
|
for _, m := range matches {
|
2021-01-12 19:54:08 +00:00
|
|
|
actual = append(actual, m.Path)
|
2021-01-23 12:29:14 +00:00
|
|
|
}
|
2021-01-12 19:54:08 +00:00
|
|
|
assert.Equal(t, actual, expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func testNoteDAOFind(t *testing.T, opts core.NoteFindOpts, expected []core.ContextualNote) {
|
2021-01-10 11:29:57 +00:00
|
|
|
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
|
2021-01-23 12:29:14 +00:00
|
|
|
actual, err := dao.Find(opts)
|
2021-01-10 11:29:57 +00:00
|
|
|
assert.Nil(t, err)
|
2021-01-10 15:39:10 +00:00
|
|
|
assert.Equal(t, actual, expected)
|
2021-01-10 11:29:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNoteDAO(t *testing.T, callback func(tx Transaction, dao *NoteDAO)) {
|
|
|
|
testTransaction(t, func(tx Transaction) {
|
|
|
|
callback(tx, NewNoteDAO(tx, &util.NullLogger))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-04 13:31:54 +00:00
|
|
|
func testNoteDAOWithFixtures(t *testing.T, fixtures string, callback func(tx Transaction, dao *NoteDAO)) {
|
|
|
|
testTransactionWithFixtures(t, opt.NewNotEmptyString(fixtures), func(tx Transaction) {
|
2021-01-17 16:37:16 +00:00
|
|
|
callback(tx, NewNoteDAO(tx, &util.NullLogger))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:50:39 +00:00
|
|
|
type noteRow struct {
|
2021-03-08 20:38:32 +00:00
|
|
|
Path, Title, Lead, Body, RawContent, Checksum, Metadata string
|
|
|
|
WordCount int
|
|
|
|
Created, Modified time.Time
|
2021-01-05 19:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryNoteRow(tx Transaction, where string) (noteRow, error) {
|
|
|
|
var row noteRow
|
|
|
|
err := tx.QueryRow(fmt.Sprintf(`
|
2021-03-08 20:38:32 +00:00
|
|
|
SELECT path, title, lead, body, raw_content, word_count, checksum, created, modified, metadata
|
2021-01-05 19:50:39 +00:00
|
|
|
FROM notes
|
|
|
|
WHERE %v
|
2021-03-08 20:38:32 +00:00
|
|
|
`, where)).Scan(&row.Path, &row.Title, &row.Lead, &row.Body, &row.RawContent, &row.WordCount, &row.Checksum, &row.Created, &row.Modified, &row.Metadata)
|
2021-01-05 19:50:39 +00:00
|
|
|
return row, err
|
|
|
|
}
|
2021-01-25 20:44:44 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
func idPointer(i int64) *core.NoteID {
|
|
|
|
id := core.NoteID(i)
|
2021-03-06 18:38:52 +00:00
|
|
|
return &id
|
2021-01-25 20:44:44 +00:00
|
|
|
}
|