move print note line to separate function and add tests

This commit is contained in:
nicknickel 2023-03-01 21:04:44 +00:00
commit 8e2437ee7d
2 changed files with 50 additions and 12 deletions

View file

@ -168,6 +168,22 @@ func printBooks(ctx context.DnoteCtx, nameOnly bool) error {
return nil
}
func printNoteLine(info noteInfo, showTimestamp bool) {
body, isExcerpt := formatBody(info.Body)
rowid := log.ColorYellow.Sprintf("(%d)", info.RowID)
preface := ""
if showTimestamp {
local_time := time.Unix(0, info.AddedOn).Format(time.DateTime)
preface = log.ColorYellow.Sprintf(" [%s]", local_time)
}
if isExcerpt {
body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]"))
}
log.Plainf("%s%s %s\n", rowid, preface, body)
}
func printNotes(ctx context.DnoteCtx, bookName string, timestamps bool) error {
db := ctx.DB
@ -199,18 +215,7 @@ func printNotes(ctx context.DnoteCtx, bookName string, timestamps bool) error {
log.Infof("on book %s\n", bookName)
for _, info := range infos {
body, isExcerpt := formatBody(info.Body)
preface := log.ColorYellow.Sprintf("(%d)", info.RowID)
if timestamps {
local_time := time.Unix(0, info.AddedOn).Format(time.DateTime)
preface = log.ColorYellow.Sprintf("(%s)", local_time)
}
if isExcerpt {
body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]"))
}
log.Plainf("%s %s\n", preface, body)
printNoteLine(info, timestamps)
}
return nil

View file

@ -105,6 +105,39 @@ func TestInit(t *testing.T) {
assert.NotEqual(t, lastSyncAt, "", "last sync at should not be empty")
}
func TestViewNote(t *testing.T) {
// tests successful if command does not return an error
t.Run("using default", func(t *testing.T) {
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
testutils.Setup3(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "view", "js")
defer testutils.RemoveDir(t, testDir)
})
t.Run("content only", func(t *testing.T) {
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
testutils.Setup3(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "view", "js", "--content-only")
defer testutils.RemoveDir(t, testDir)
})
t.Run("with timestamps", func(t *testing.T) {
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
testutils.Setup3(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "view", "js", "-t")
defer testutils.RemoveDir(t, testDir)
})
}
func TestAddNote(t *testing.T) {
t.Run("new book", func(t *testing.T) {
// Set up and execute