MCPcopy Index your code
hub / github.com/dnote/dnote / TestNoteFTS

Function TestNoteFTS

pkg/cli/database/models_test.go:769–851  ·  view source on GitHub ↗

TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete

(t *testing.T)

Source from the content-addressed store, hash-verified

767
768// TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete
769func 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"), &noteCount)
800 MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), &noteFtsCount)
801 MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), &noteSearchCount)
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"), &noteCount)
823 MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), &noteFtsCount)
824 assert.Equal(t, noteCount, 1, "noteCount mismatch")
825 assert.Equal(t, noteFtsCount, 1, "noteFtsCount mismatch")
826

Callers

nothing calls this directly

Calls 11

InsertMethod · 0.95
UpdateMethod · 0.95
ExpungeMethod · 0.95
EqualFunction · 0.92
InitTestMemoryDBFunction · 0.85
MustScanFunction · 0.85
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected