TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete
(t *testing.T)
| 767 | |
| 768 | // TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete |
| 769 | func TestNoteFTS(t *testing.T) { |
| 770 | // set up |
| 771 | db := InitTestMemoryDB(t) |
| 772 | |
| 773 | // execute - insert |
| 774 | n := Note{ |
| 775 | UUID: "n1-uuid", |
| 776 | BookUUID: "b1-uuid", |
| 777 | Body: "foo bar", |
| 778 | AddedOn: 1542058875, |
| 779 | EditedOn: 0, |
| 780 | USN: 0, |
| 781 | Deleted: false, |
| 782 | Dirty: false, |
| 783 | } |
| 784 | |
| 785 | tx, err := db.Begin() |
| 786 | if err != nil { |
| 787 | t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) |
| 788 | } |
| 789 | |
| 790 | if err := n.Insert(tx); err != nil { |
| 791 | tx.Rollback() |
| 792 | t.Fatal(errors.Wrap(err, "inserting").Error()) |
| 793 | } |
| 794 | |
| 795 | tx.Commit() |
| 796 | |
| 797 | // test |
| 798 | var noteCount, noteFtsCount, noteSearchCount int |
| 799 | MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) |
| 800 | MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) |
| 801 | MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), ¬eSearchCount) |
| 802 | |
| 803 | assert.Equal(t, noteCount, 1, "noteCount mismatch") |
| 804 | assert.Equal(t, noteFtsCount, 1, "noteFtsCount mismatch") |
| 805 | assert.Equal(t, noteSearchCount, 1, "noteSearchCount mismatch") |
| 806 | |
| 807 | // execute - update |
| 808 | tx, err = db.Begin() |
| 809 | if err != nil { |
| 810 | t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) |
| 811 | } |
| 812 | |
| 813 | n.Body = "baz quz" |
| 814 | if err := n.Update(tx); err != nil { |
| 815 | tx.Rollback() |
| 816 | t.Fatal(errors.Wrap(err, "updating").Error()) |
| 817 | } |
| 818 | |
| 819 | tx.Commit() |
| 820 | |
| 821 | // test |
| 822 | MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) |
| 823 | MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) |
| 824 | assert.Equal(t, noteCount, 1, "noteCount mismatch") |
| 825 | assert.Equal(t, noteFtsCount, 1, "noteFtsCount mismatch") |
| 826 |
nothing calls this directly
no test coverage detected