mirror of
https://github.com/dnote/dnote
synced 2026-03-16 23:45:52 +01:00
Simplify remove book command (#240)
* Allow to skip confirmation when removing * Allow to pass book name directly as an argument when removing
This commit is contained in:
parent
e84e8ed2f3
commit
12a933d8ec
7 changed files with 255 additions and 166 deletions
|
|
@ -45,7 +45,7 @@ dnote view 12
|
|||
|
||||
_alias: e_
|
||||
|
||||
Edit a note.
|
||||
Edit a note or a book.
|
||||
|
||||
```bash
|
||||
# Launch a text editor to edit a note with the given id.
|
||||
|
|
@ -53,20 +53,26 @@ dnote edit 12
|
|||
|
||||
# Edit a note with the given id in the specified book with a content.
|
||||
dnote edit 12 -c "New Content"
|
||||
|
||||
# Launch a text editor to edit a book name.
|
||||
dnote edit js
|
||||
|
||||
# Edit a book name by using a flag.
|
||||
dnote edit js -n "javascript"
|
||||
```
|
||||
|
||||
## dnote remove
|
||||
|
||||
_alias: d_
|
||||
_alias: rm, d_
|
||||
|
||||
Remove either a note or a book.
|
||||
|
||||
```bash
|
||||
# Remove the note with an id in the specified book.
|
||||
# Remove a note with an id.
|
||||
dnote remove 1
|
||||
|
||||
# Remove the book with the `book name`.
|
||||
dnote remove -b JS
|
||||
# Remove a book with the `book name`.
|
||||
dnote remove js
|
||||
```
|
||||
|
||||
## dnote find
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ var example = `
|
|||
dnote cat javascript 2
|
||||
`
|
||||
|
||||
var deprecationWarning = `and "view" will replace it in v0.5.0.
|
||||
var deprecationWarning = `and "view" will replace it in the future version.
|
||||
|
||||
Run "dnote view --help" for more information.
|
||||
`
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ var example = `
|
|||
// NewCmd returns a new edit command
|
||||
func NewCmd(ctx context.DnoteCtx) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit",
|
||||
Use: "edit <note id|book name>",
|
||||
Short: "Edit a note or a book",
|
||||
Aliases: []string{"e"},
|
||||
Example: example,
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ var example = `
|
|||
dnote ls javascript
|
||||
`
|
||||
|
||||
var deprecationWarning = `and "view" will replace it in v1.0.0.
|
||||
var deprecationWarning = `and "view" will replace it in the future version.
|
||||
|
||||
Run "dnote view --help" for more information.
|
||||
`
|
||||
|
|
|
|||
|
|
@ -33,68 +33,100 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var targetBookName string
|
||||
var bookFlag string
|
||||
var yesFlag bool
|
||||
|
||||
var example = `
|
||||
* Delete a note by its id
|
||||
* Delete a note by id
|
||||
dnote delete 2
|
||||
|
||||
* Delete a book
|
||||
dnote delete -b js`
|
||||
* Delete a book by name
|
||||
dnote delete js
|
||||
`
|
||||
|
||||
// NewCmd returns a new remove command
|
||||
func NewCmd(ctx context.DnoteCtx) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "remove",
|
||||
Use: "remove <note id|book name>",
|
||||
Short: "Remove a note or a book",
|
||||
Aliases: []string{"rm", "d", "delete"},
|
||||
Example: example,
|
||||
PreRunE: preRun,
|
||||
RunE: newRun(ctx),
|
||||
}
|
||||
|
||||
f := cmd.Flags()
|
||||
f.StringVarP(&targetBookName, "book", "b", "", "The book name to delete")
|
||||
f.StringVarP(&bookFlag, "book", "b", "", "The book name to delete")
|
||||
f.BoolVarP(&yesFlag, "yes", "y", false, "Assume yes to the prompts and run in non-interactive mode")
|
||||
|
||||
f.MarkDeprecated("book", "Pass the book name as an argument. e.g. `dnote rm book_name`")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func preRun(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 && len(args) != 2 {
|
||||
return errors.New("Incorrect number of argument")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func maybeConfirm(message string, defaultValue bool) (bool, error) {
|
||||
if yesFlag {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return ui.Confirm(message, defaultValue)
|
||||
}
|
||||
|
||||
func newRun(ctx context.DnoteCtx) infra.RunEFunc {
|
||||
return func(cmd *cobra.Command, args []string) error {
|
||||
if targetBookName != "" {
|
||||
if err := removeBook(ctx, targetBookName); err != nil {
|
||||
// DEPRECATED: Remove in 1.0.0
|
||||
if bookFlag != "" {
|
||||
if err := runBook(ctx, bookFlag); err != nil {
|
||||
return errors.Wrap(err, "removing the book")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var noteRowIDArg string
|
||||
// DEPRECATED: Remove in 1.0.0
|
||||
if len(args) == 2 {
|
||||
log.Plain(log.ColorYellow.Sprintf("DEPRECATED: you no longer need to pass book name to the view command. e.g. `dnote view 123`.\n\n"))
|
||||
log.Plain(log.ColorYellow.Sprintf("DEPRECATED: you no longer need to pass book name to the remove command. e.g. `dnote remove 123`.\n\n"))
|
||||
|
||||
noteRowIDArg = args[1]
|
||||
} else if len(args) == 1 {
|
||||
noteRowIDArg = args[0]
|
||||
target := args[1]
|
||||
if err := runNote(ctx, target); err != nil {
|
||||
return errors.Wrap(err, "removing the note")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
target := args[0]
|
||||
|
||||
if utils.IsNumber(target) {
|
||||
if err := runNote(ctx, target); err != nil {
|
||||
return errors.Wrap(err, "removing the note")
|
||||
}
|
||||
} else {
|
||||
return errors.New("Missing argument")
|
||||
}
|
||||
|
||||
noteRowID, err := strconv.Atoi(noteRowIDArg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid rowid")
|
||||
}
|
||||
|
||||
if err := removeNote(ctx, noteRowID); err != nil {
|
||||
return errors.Wrap(err, "removing the note")
|
||||
if err := runBook(ctx, target); err != nil {
|
||||
return errors.Wrap(err, "removing the book")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func removeNote(ctx context.DnoteCtx, noteRowID int) error {
|
||||
func runNote(ctx context.DnoteCtx, rowIDArg string) error {
|
||||
db := ctx.DB
|
||||
|
||||
noteRowID, err := strconv.Atoi(rowIDArg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid rowid")
|
||||
}
|
||||
|
||||
noteInfo, err := database.GetNoteInfo(db, noteRowID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -102,7 +134,7 @@ func removeNote(ctx context.DnoteCtx, noteRowID int) error {
|
|||
|
||||
output.NoteInfo(noteInfo)
|
||||
|
||||
ok, err := ui.Confirm("remove this note?", false)
|
||||
ok, err := maybeConfirm("remove this note?", false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting confirmation")
|
||||
}
|
||||
|
|
@ -117,16 +149,22 @@ func removeNote(ctx context.DnoteCtx, noteRowID int) error {
|
|||
}
|
||||
|
||||
if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE uuid = ?", true, true, "", noteInfo.UUID); err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "removing the note")
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "comitting transaction")
|
||||
}
|
||||
|
||||
log.Successf("removed from %s\n", noteInfo.BookLabel)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeBook(ctx context.DnoteCtx, bookLabel string) error {
|
||||
func runBook(ctx context.DnoteCtx, bookLabel string) error {
|
||||
db := ctx.DB
|
||||
|
||||
bookUUID, err := database.GetBookUUID(db, bookLabel)
|
||||
|
|
@ -134,7 +172,7 @@ func removeBook(ctx context.DnoteCtx, bookLabel string) error {
|
|||
return errors.Wrap(err, "finding book uuid")
|
||||
}
|
||||
|
||||
ok, err := ui.Confirm(fmt.Sprintf("delete book '%s' and all its notes?", bookLabel), false)
|
||||
ok, err := maybeConfirm(fmt.Sprintf("delete book '%s' and all its notes?", bookLabel), false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting confirmation")
|
||||
}
|
||||
|
|
@ -149,16 +187,22 @@ func removeBook(ctx context.DnoteCtx, bookLabel string) error {
|
|||
}
|
||||
|
||||
if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE book_uuid = ?", true, true, "", bookUUID); err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "removing notes in the book")
|
||||
}
|
||||
|
||||
// override the label with a random string
|
||||
uniqLabel := utils.GenerateUUID()
|
||||
if _, err = tx.Exec("UPDATE books SET deleted = ?, dirty = ?, label = ? WHERE uuid = ?", true, true, uniqLabel, bookUUID); err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "removing the book")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "committing transaction")
|
||||
}
|
||||
|
||||
log.Success("removed book\n")
|
||||
|
||||
|
|
|
|||
|
|
@ -320,137 +320,176 @@ func TestEditBook(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRemoveNote(t *testing.T) {
|
||||
// Setup
|
||||
db := database.InitTestDB(t, fmt.Sprintf("%s/%s", opts.DnoteDir, consts.DnoteDBFileName), nil)
|
||||
testutils.Setup2(t, db)
|
||||
testCases := []struct {
|
||||
yesFlag bool
|
||||
}{
|
||||
{
|
||||
yesFlag: false,
|
||||
},
|
||||
{
|
||||
yesFlag: true,
|
||||
},
|
||||
}
|
||||
|
||||
// Execute
|
||||
testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "js", "1")
|
||||
defer testutils.RemoveDir(t, opts.HomeDir)
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) {
|
||||
// Setup
|
||||
db := database.InitTestDB(t, fmt.Sprintf("%s/%s", opts.DnoteDir, consts.DnoteDBFileName), nil)
|
||||
testutils.Setup2(t, db)
|
||||
|
||||
// Test
|
||||
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
|
||||
database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
|
||||
database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount)
|
||||
database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount)
|
||||
database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount)
|
||||
// Execute
|
||||
if tc.yesFlag {
|
||||
testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "1")
|
||||
} else {
|
||||
testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "1")
|
||||
}
|
||||
defer testutils.RemoveDir(t, opts.HomeDir)
|
||||
|
||||
assert.Equalf(t, bookCount, 2, "book count mismatch")
|
||||
assert.Equalf(t, noteCount, 3, "note count mismatch")
|
||||
assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes")
|
||||
assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note")
|
||||
// Test
|
||||
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
|
||||
database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
|
||||
database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount)
|
||||
database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount)
|
||||
database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount)
|
||||
|
||||
var b1, b2 database.Book
|
||||
var n1, n2, n3 database.Note
|
||||
database.MustScan(t, "getting b1",
|
||||
db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"),
|
||||
&b1.Label, &b1.Deleted, &b1.USN)
|
||||
database.MustScan(t, "getting b2",
|
||||
db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"),
|
||||
&b2.Label, &b2.Deleted, &b2.USN)
|
||||
database.MustScan(t, "getting n1",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"),
|
||||
&n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN)
|
||||
database.MustScan(t, "getting n2",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"),
|
||||
&n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN)
|
||||
database.MustScan(t, "getting n3",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"),
|
||||
&n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN)
|
||||
assert.Equalf(t, bookCount, 2, "book count mismatch")
|
||||
assert.Equalf(t, noteCount, 3, "note count mismatch")
|
||||
assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes")
|
||||
assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note")
|
||||
|
||||
assert.Equal(t, b1.Label, "js", "b1 label mismatch")
|
||||
assert.Equal(t, b1.Deleted, false, "b1 deleted mismatch")
|
||||
assert.Equal(t, b1.Dirty, false, "b1 Dirty mismatch")
|
||||
assert.Equal(t, b1.USN, 111, "b1 usn mismatch")
|
||||
var b1, b2 database.Book
|
||||
var n1, n2, n3 database.Note
|
||||
database.MustScan(t, "getting b1",
|
||||
db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"),
|
||||
&b1.Label, &b1.Deleted, &b1.USN)
|
||||
database.MustScan(t, "getting b2",
|
||||
db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"),
|
||||
&b2.Label, &b2.Deleted, &b2.USN)
|
||||
database.MustScan(t, "getting n1",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"),
|
||||
&n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN)
|
||||
database.MustScan(t, "getting n2",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"),
|
||||
&n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN)
|
||||
database.MustScan(t, "getting n3",
|
||||
db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"),
|
||||
&n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN)
|
||||
|
||||
assert.Equal(t, b2.Label, "linux", "b2 label mismatch")
|
||||
assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch")
|
||||
assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch")
|
||||
assert.Equal(t, b2.USN, 122, "b2 usn mismatch")
|
||||
assert.Equal(t, b1.Label, "js", "b1 label mismatch")
|
||||
assert.Equal(t, b1.Deleted, false, "b1 deleted mismatch")
|
||||
assert.Equal(t, b1.Dirty, false, "b1 Dirty mismatch")
|
||||
assert.Equal(t, b1.USN, 111, "b1 usn mismatch")
|
||||
|
||||
assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID")
|
||||
assert.Equal(t, n1.Body, "", "n1 body mismatch")
|
||||
assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch")
|
||||
assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch")
|
||||
assert.Equal(t, n1.USN, 11, "n1 usn mismatch")
|
||||
assert.Equal(t, b2.Label, "linux", "b2 label mismatch")
|
||||
assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch")
|
||||
assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch")
|
||||
assert.Equal(t, b2.USN, 122, "b2 usn mismatch")
|
||||
|
||||
assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID")
|
||||
assert.Equal(t, n2.Body, "n2 body", "n2 body mismatch")
|
||||
assert.Equal(t, n2.Deleted, false, "n2 deleted mismatch")
|
||||
assert.Equal(t, n2.Dirty, false, "n2 Dirty mismatch")
|
||||
assert.Equal(t, n2.USN, 12, "n2 usn mismatch")
|
||||
assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID")
|
||||
assert.Equal(t, n1.Body, "", "n1 body mismatch")
|
||||
assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch")
|
||||
assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch")
|
||||
assert.Equal(t, n1.USN, 11, "n1 usn mismatch")
|
||||
|
||||
assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID")
|
||||
assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch")
|
||||
assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch")
|
||||
assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch")
|
||||
assert.Equal(t, n3.USN, 13, "n3 usn mismatch")
|
||||
assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID")
|
||||
assert.Equal(t, n2.Body, "n2 body", "n2 body mismatch")
|
||||
assert.Equal(t, n2.Deleted, false, "n2 deleted mismatch")
|
||||
assert.Equal(t, n2.Dirty, false, "n2 Dirty mismatch")
|
||||
assert.Equal(t, n2.USN, 12, "n2 usn mismatch")
|
||||
|
||||
assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID")
|
||||
assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch")
|
||||
assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch")
|
||||
assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch")
|
||||
assert.Equal(t, n3.USN, 13, "n3 usn mismatch")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveBook(t *testing.T) {
|
||||
// Setup
|
||||
db := database.InitTestDB(t, fmt.Sprintf("%s/%s", opts.DnoteDir, consts.DnoteDBFileName), nil)
|
||||
testutils.Setup2(t, db)
|
||||
testCases := []struct {
|
||||
yesFlag bool
|
||||
}{
|
||||
{
|
||||
yesFlag: false,
|
||||
},
|
||||
{
|
||||
yesFlag: true,
|
||||
},
|
||||
}
|
||||
|
||||
// Execute
|
||||
testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "-b", "js")
|
||||
defer testutils.RemoveDir(t, opts.HomeDir)
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) {
|
||||
// Setup
|
||||
db := database.InitTestDB(t, fmt.Sprintf("%s/%s", opts.DnoteDir, consts.DnoteDBFileName), nil)
|
||||
testutils.Setup2(t, db)
|
||||
|
||||
// Test
|
||||
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
|
||||
database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
|
||||
database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount)
|
||||
database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount)
|
||||
database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount)
|
||||
// Execute
|
||||
if tc.yesFlag {
|
||||
testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "js")
|
||||
} else {
|
||||
testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "js")
|
||||
}
|
||||
|
||||
assert.Equalf(t, bookCount, 2, "book count mismatch")
|
||||
assert.Equalf(t, noteCount, 3, "note count mismatch")
|
||||
assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes")
|
||||
assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note")
|
||||
defer testutils.RemoveDir(t, opts.HomeDir)
|
||||
|
||||
var b1, b2 database.Book
|
||||
var n1, n2, n3 database.Note
|
||||
database.MustScan(t, "getting b1",
|
||||
db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"),
|
||||
&b1.Label, &b1.Dirty, &b1.Deleted, &b1.USN)
|
||||
database.MustScan(t, "getting b2",
|
||||
db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"),
|
||||
&b2.Label, &b2.Dirty, &b2.Deleted, &b2.USN)
|
||||
database.MustScan(t, "getting n1",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"),
|
||||
&n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN)
|
||||
database.MustScan(t, "getting n2",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"),
|
||||
&n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN)
|
||||
database.MustScan(t, "getting n3",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"),
|
||||
&n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN)
|
||||
// Test
|
||||
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
|
||||
database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
|
||||
database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount)
|
||||
database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount)
|
||||
database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount)
|
||||
|
||||
assert.NotEqual(t, b1.Label, "js", "b1 label mismatch")
|
||||
assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch")
|
||||
assert.Equal(t, b1.Deleted, true, "b1 deleted mismatch")
|
||||
assert.Equal(t, b1.USN, 111, "b1 usn mismatch")
|
||||
assert.Equalf(t, bookCount, 2, "book count mismatch")
|
||||
assert.Equalf(t, noteCount, 3, "note count mismatch")
|
||||
assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes")
|
||||
assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note")
|
||||
|
||||
assert.Equal(t, b2.Label, "linux", "b2 label mismatch")
|
||||
assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch")
|
||||
assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch")
|
||||
assert.Equal(t, b2.USN, 122, "b2 usn mismatch")
|
||||
var b1, b2 database.Book
|
||||
var n1, n2, n3 database.Note
|
||||
database.MustScan(t, "getting b1",
|
||||
db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"),
|
||||
&b1.Label, &b1.Dirty, &b1.Deleted, &b1.USN)
|
||||
database.MustScan(t, "getting b2",
|
||||
db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"),
|
||||
&b2.Label, &b2.Dirty, &b2.Deleted, &b2.USN)
|
||||
database.MustScan(t, "getting n1",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"),
|
||||
&n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN)
|
||||
database.MustScan(t, "getting n2",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"),
|
||||
&n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN)
|
||||
database.MustScan(t, "getting n3",
|
||||
db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"),
|
||||
&n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN)
|
||||
|
||||
assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID")
|
||||
assert.Equal(t, n1.Body, "", "n1 body mismatch")
|
||||
assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch")
|
||||
assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch")
|
||||
assert.Equal(t, n1.USN, 11, "n1 usn mismatch")
|
||||
assert.NotEqual(t, b1.Label, "js", "b1 label mismatch")
|
||||
assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch")
|
||||
assert.Equal(t, b1.Deleted, true, "b1 deleted mismatch")
|
||||
assert.Equal(t, b1.USN, 111, "b1 usn mismatch")
|
||||
|
||||
assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID")
|
||||
assert.Equal(t, n2.Body, "", "n2 body mismatch")
|
||||
assert.Equal(t, n2.Dirty, true, "n2 Dirty mismatch")
|
||||
assert.Equal(t, n2.Deleted, true, "n2 deleted mismatch")
|
||||
assert.Equal(t, n2.USN, 12, "n2 usn mismatch")
|
||||
assert.Equal(t, b2.Label, "linux", "b2 label mismatch")
|
||||
assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch")
|
||||
assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch")
|
||||
assert.Equal(t, b2.USN, 122, "b2 usn mismatch")
|
||||
|
||||
assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID")
|
||||
assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch")
|
||||
assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch")
|
||||
assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch")
|
||||
assert.Equal(t, n3.USN, 13, "n3 usn mismatch")
|
||||
assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID")
|
||||
assert.Equal(t, n1.Body, "", "n1 body mismatch")
|
||||
assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch")
|
||||
assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch")
|
||||
assert.Equal(t, n1.USN, 11, "n1 usn mismatch")
|
||||
|
||||
assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID")
|
||||
assert.Equal(t, n2.Body, "", "n2 body mismatch")
|
||||
assert.Equal(t, n2.Dirty, true, "n2 Dirty mismatch")
|
||||
assert.Equal(t, n2.Deleted, true, "n2 deleted mismatch")
|
||||
assert.Equal(t, n2.USN, 12, "n2 usn mismatch")
|
||||
|
||||
assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID")
|
||||
assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch")
|
||||
assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch")
|
||||
assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch")
|
||||
assert.Equal(t, n3.USN, 13, "n3 usn mismatch")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,10 +61,10 @@ func Setup3(t *testing.T, db *database.DB) {
|
|||
func Setup4(t *testing.T, db *database.DB) {
|
||||
b1UUID := "js-book-uuid"
|
||||
|
||||
database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js")
|
||||
database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b1UUID, "js", 111)
|
||||
|
||||
database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?, ?)", 1, "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943)
|
||||
database.MustExec(t, "setting up note 2", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?, ?)", 2, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "Date object implements mathematical comparisons", 1515199951)
|
||||
database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?, ?)", 1, "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943, 11)
|
||||
database.MustExec(t, "setting up note 2", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?, ?)", 2, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "Date object implements mathematical comparisons", 1515199951, 12)
|
||||
}
|
||||
|
||||
// Setup5 sets up a dnote env #2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue