Files
satbd-explorer/bleve_indexer_test.go
Alexandre Tuleu 0d3e8b922a Refactors the indexing and the syntax
Human queries are now in french. Sorry for that
2016-01-21 16:36:45 +01:00

97 lines
3.0 KiB
Go

package main
import (
"log"
"path/filepath"
"time"
. "gopkg.in/check.v1"
)
type BleveIndexerSuite struct {
i Indexer
}
var _ = Suite(&BleveIndexerSuite{})
func (s *BleveIndexerSuite) SetUpSuite(c *C) {
var err error
start := time.Now()
s.i, err = NewBleveIndexer(filepath.Join(c.MkDir(), "satbd-test.bar.satellite"))
c.Assert(err, IsNil)
for _, a := range albumsDataTest {
c.Assert(s.i.Index(&a), IsNil)
}
log.Printf("Indexing took %s", time.Since(start))
}
func (s *BleveIndexerSuite) TestCanRetrieveAlbum(c *C) {
start := time.Now()
for _, a := range albumsDataTest {
fromIndex, err := s.i.Get(a.ID)
if c.Check(err, IsNil) == true {
c.Check(*fromIndex, DeepEquals, a)
}
}
log.Printf("%s: %s", c.TestName(), time.Since(start))
}
func (s *BleveIndexerSuite) TestCanDeleteAlbum(c *C) {
c.Check(s.i.Delete(albumsDataTest[0].ID), IsNil)
c.Check(s.i.Index(&(albumsDataTest[0])), IsNil)
c.Check(s.i.Delete(0), ErrorMatches, "No album 0 in the index")
}
func (s *BleveIndexerSuite) TestCanSearch(c *C) {
data := map[string]map[AlbumID]bool{
// Research by amny keyword (here in the description
"frégate": map[AlbumID]bool{84448: true},
// Research by editor
"éditeur:Dupuis": map[AlbumID]bool{8179: true, 9935: true, 160366: true},
// Research by Title
"titre:\"Mortes saisons\"": map[AlbumID]bool{8179: true},
// Keyword can appear in both Title and Description
"pour mourir": map[AlbumID]bool{44989: true, 84448: true, 160366: true, 19762: true},
// Or in the collection
"écho savanes": map[AlbumID]bool{15875: true},
// May not find anything, but never fails
"FOOOOOOOBAAAAAR": map[AlbumID]bool{},
// Or we can only look for collection field
"collection:Dupuis": map[AlbumID]bool{160366: true},
// Maybe we are only inderested in scenarist
"scenario:plantu": map[AlbumID]bool{58595: true},
// Or designer
"dessins:berthet": map[AlbumID]bool{8179: true},
// or colorist, but Trondheim does not does color ;)
"couleurs:trondheim": map[AlbumID]bool{},
// or any of that fields, yes Trondheim do draw some comics ;)
"trondheim": map[AlbumID]bool{160366: true},
// or we just want to look up a reference
"CHI-23": map[AlbumID]bool{46005: true},
// or more explicitely
"ref:BERT-9": map[AlbumID]bool{8179: true},
// or we are just interested in good comics
"Note:>4": map[AlbumID]bool{52100: true, 19762: true, 160366: true},
// or bad ones
"Note:<2": map[AlbumID]bool{754: true, 32043: true},
}
start := time.Now()
for q, expected := range data {
res, err := s.i.Search(q)
if c.Check(err, IsNil) == true {
if c.Check(len(res), Equals, len(expected), Commentf("Query: %s", q)) == true {
for _, resAlbum := range res {
_, ok := expected[resAlbum.ID]
c.Check(ok, Equals, true, Commentf("Query: %s, got %d instead of %v", q, resAlbum.ID, expected))
}
} else {
log.Printf("Got %d result(s)", len(res))
for i, a := range res {
log.Printf(" - %d: %d: %s", i, a.ID, a.Title)
}
}
}
}
log.Printf("%s: %s", c.TestName(), time.Since(start))
}