linkwallet/db/index_test.go
Justin Hawkins d2aa6fdd2f Merge the feature branch to introduce the bleve indexing.
commit e8c2bc7e4ade56f893c8fb23eac4cdb754555389
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sat May 3 12:36:18 2025 +0930

    Clean up menu/version

commit 1993533a4613a5db7bbb2ecd6b7a2694f48acd52
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sat May 3 12:31:50 2025 +0930

    Update README

commit 044cc830dcb3d05b43ab770e8ea42959873823dd
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sat May 3 12:22:33 2025 +0930

    No longer needed

commit a7c37ad7c5c12fcac1d5589271e0e66af4f086e8
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sat May 3 12:14:59 2025 +0930

    Fixup version handling

commit ade0b748e978ce1d9533a928bd6369f1714ca8a8
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sat May 3 11:58:15 2025 +0930

    Use the correct analyser for searches

commit e5a65cf5cfdd82ba7aa3d2470c12c6e82c851a3f
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 19:51:54 2025 +0930

    Fix version in template

commit 0171be0ee4d11796cfc0419a00b412f8037f4d97
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 19:51:41 2025 +0930

    Rescrape all links if needed on startup

commit ae654998f751c3883f8b3bd76005afae38209edf
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 19:51:25 2025 +0930

    Spelling

commit bfe9bbee028e5515be5b6d406e9090c61cdbbebd
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 19:34:06 2025 +0930

    Make goreleaser set the version

commit 4436313413f52b2b29a08e510ac4496016aae8a0
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 11:43:58 2025 +0930

    Make release matrix sane

commit 7b467ecee7a3d74ba194b723034dd16bf1c53b53
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri May 2 11:40:07 2025 +0930

    I hate YAML, so much.

commit b578e0f044e754ca70cd4ace95fdc6dc3cdcfe07
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Thu May 1 23:47:07 2025 +0930

    Update goreleaser

commit fba84f0827d35359a450fb847b03f5fa78960a1f
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Thu May 1 23:45:46 2025 +0930

    Update version

commit e4edb08bd19c560b6f2ea5e572d5950896ac6062
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Thu May 1 23:42:59 2025 +0930

    Deps

commit 58b6692d1b397de91f3531c35c6518fb58ad285a
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Thu May 1 23:39:51 2025 +0930

    Mostly done, first cut

commit badbe5e92ffa415b444535eca385d87fa00afa68
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sun Apr 27 20:28:37 2025 +0930

    Remove unused code

commit 903240dd18cdcd7722705f04ff40b30661969d4d
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sun Apr 27 20:26:19 2025 +0930

    Update deps

commit de90b9951a82c420be0588e6c2c2b63fe0b10eff
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Sun Apr 27 20:21:33 2025 +0930

    Keep on bleving

commit 9b15528510a06661237249eaf7bf64c3ce71fc3c
Author: Justin Hawkins <justin@hawkins.id.au>
Date:   Fri Apr 25 23:57:04 2025 +0930

    Start of blevification
2025-05-03 12:37:44 +09:30

258 lines
5.5 KiB
Go

package db
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/search/query"
"github.com/tardisx/linkwallet/entity"
)
var serverResponse string
func newTestServer() *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(serverResponse))
})
return httptest.NewServer(mux)
}
func TestAddRemove(t *testing.T) {
ts := newTestServer()
defer ts.Close()
serverResponse = "<p>the quick brown fox</p>"
db := DB{}
f, _ := os.CreateTemp("", "test_boltdb_*")
f.Close()
defer os.Remove(f.Name())
db.Open(f.Name())
bmm := NewBookmarkManager(&db)
bm := entity.Bookmark{URL: ts.URL}
err := bmm.AddBookmark(&bm)
if err != nil {
t.Fatalf("error adding: %s", err)
}
if bm.ID == 0 {
t.Error("bookmark did not get an id")
}
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err := bmm.Search(SearchOptions{Query: "fox"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get one id")
}
// change content, rescrape
serverResponse = "<p>the quick brown rabbit</p>"
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err = bmm.Search(SearchOptions{Query: "fox"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 0 {
t.Error("got result when should not")
}
searchRes, err = bmm.Search(SearchOptions{Query: "rabbit"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get result when should")
}
err = bmm.DeleteBookmark(&bm)
if err != nil {
t.Errorf("got error when deleting: %s", err)
}
searchRes, err = bmm.Search(SearchOptions{Query: "rabbit"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 0 {
t.Error("rabbit should be gone from index")
}
}
func TestTagIndexing(t *testing.T) {
ts := newTestServer()
defer ts.Close()
serverResponse = "<p>the quick brown fox</p>"
db := DB{}
f, _ := os.CreateTemp("", "test_boltdb_*")
f.Close()
defer os.Remove(f.Name())
db.Open(f.Name())
bmm := NewBookmarkManager(&db)
bm := entity.Bookmark{URL: ts.URL}
err := bmm.AddBookmark(&bm)
if err != nil {
t.Fatalf("error adding: %s", err)
}
if bm.ID == 0 {
t.Error("bookmark did not get an id")
}
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err := bmm.Search(SearchOptions{Query: "fox"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get one id")
}
// add a tag
bm.Tags = []string{"sloth"}
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err = bmm.Search(SearchOptions{Query: "sloth"})
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get one id for sloth")
}
}
func testBM() entity.Bookmark {
return entity.Bookmark{
ID: 1,
URL: "https://one.com",
Info: entity.PageInfo{
Fetched: time.Time{},
Title: "one web",
Size: 200,
StatusCode: 200,
RawText: "one web site is great for all humans",
},
Tags: []string{"hello", "big friends"},
PreserveTitle: false,
TimestampCreated: time.Time{},
TimestampLastScraped: time.Time{},
}
}
func TestMappings(t *testing.T) {
mapping := createIndexMapping()
idx, err := bleve.NewMemOnly(mapping)
if err != nil {
t.Error(err)
t.FailNow()
}
bm := testBM()
err = idx.Index("1", bm)
if err != nil {
panic(err)
}
type tc struct {
query query.Query
expHits int
}
tcs := []tc{
{query: bleve.NewMatchQuery("human"), expHits: 1},
{query: bleve.NewMatchQuery("humanoid"), expHits: 0},
{query: bleve.NewMatchQuery("hello"), expHits: 1},
{query: bleve.NewMatchQuery("big"), expHits: 0},
{query: bleve.NewMatchQuery("friends"), expHits: 0},
{query: bleve.NewMatchQuery("big friend"), expHits: 0},
{query: bleve.NewTermQuery("big friends"), expHits: 1},
{query: bleve.NewMatchQuery("web great"), expHits: 1},
}
for i := range tcs {
q := tcs[i].query
sr, err := idx.Search(bleve.NewSearchRequest(q))
if err != nil {
t.Error(err)
} else {
if len(sr.Hits) != tcs[i].expHits {
t.Errorf("wrong hits - expected %d got %d for %s", tcs[i].expHits, len(sr.Hits), tcs[i].query)
}
}
}
}
func TestMappingsDisjunctionQuery(t *testing.T) {
mapping := createIndexMapping()
idx, err := bleve.NewMemOnly(mapping)
if err != nil {
t.Error(err)
t.FailNow()
}
bm := testBM()
err = idx.Index("1", bm)
if err != nil {
panic(err)
}
type tc struct {
query string
expHits int
}
tcs := []tc{
{query: "human", expHits: 1},
{query: "humanoid", expHits: 0},
{query: "hello", expHits: 1},
{query: "big", expHits: 0},
{query: "friends", expHits: 0},
{query: "big friend", expHits: 0},
{query: "big friends", expHits: 1},
{query: "web great", expHits: 1},
}
for i := range tcs {
q := tcs[i].query
req := bleve.NewDisjunctionQuery(
bleve.NewMatchQuery(q),
bleve.NewTermQuery(q),
)
sr, err := idx.Search(bleve.NewSearchRequest(req))
if err != nil {
t.Error(err)
} else {
if len(sr.Hits) != tcs[i].expHits {
t.Errorf("wrong hits - expected %d got %d for %s", tcs[i].expHits, len(sr.Hits), tcs[i].query)
}
}
}
}