database_test.go 8.14 KB
Newer Older
ale's avatar
ale committed
1 2 3
package liber

import (
ale's avatar
ale committed
4
	"bytes"
ale's avatar
ale committed
5
	"io"
ale's avatar
ale committed
6 7 8
	"io/ioutil"
	"os"
	"testing"
ale's avatar
ale committed
9

10 11
	"github.com/blevesearch/bleve"
	blevequery "github.com/blevesearch/bleve/search/query"
ale's avatar
ale committed
12 13 14
)

type testDatabase struct {
ale's avatar
ale committed
15 16 17
	db        *Database
	path      string
	refbookid BookId
ale's avatar
ale committed
18 19 20 21 22 23 24
}

func (td *testDatabase) Close() {
	td.db.Close()
	os.RemoveAll(td.path)
}

ale's avatar
ale committed
25 26 27 28 29 30 31 32 33
func newEmptyTestDatabase(t *testing.T) (*testDatabase, *Database) {
	path, _ := ioutil.TempDir("", "testdb-")
	db, err := NewDb(path)
	if err != nil {
		t.Fatalf("NewDb(): %v", err)
	}
	return &testDatabase{db: db, path: path}, db
}

ale's avatar
ale committed
34 35 36 37 38 39 40 41
func newTestDatabase(t *testing.T) (*testDatabase, *Database) {
	path, _ := ioutil.TempDir("", "testdb-")
	db, err := NewDb(path)
	if err != nil {
		t.Fatalf("NewDb(): %v", err)
	}

	book := testEbook()
ale's avatar
ale committed
42
	if err := db.PutBook(book); err != nil {
ale's avatar
ale committed
43 44
		t.Fatalf("PutBook(): %v", err)
	}
ale's avatar
ale committed
45
	if err := db.PutFile(testEpubFile(path, book.Id)); err != nil {
ale's avatar
ale committed
46 47
		t.Fatalf("PutFile(): %v", err)
	}
ale's avatar
ale committed
48

ale's avatar
ale committed
49 50 51
	return &testDatabase{db: db, path: path, refbookid: book.Id}, db
}

ale's avatar
ale committed
52 53 54 55 56 57 58 59 60
func newTestDatabase2(t *testing.T) (*testDatabase, *Database) {
	td, db := newTestDatabase(t)
	book := testEbook2()
	if err := db.PutBook(book); err != nil {
		t.Fatalf("PutBook(): %v", err)
	}
	return td, db
}

ale's avatar
ale committed
61 62 63 64 65 66 67 68 69 70
func testEpubFile(dir string, bookid BookId) *File {
	f, _ := ioutil.TempFile(dir, "ebook-")
	io.WriteString(f, "epub\n")
	f.Close()
	return &File{
		Id:       bookid,
		Path:     f.Name(),
		FileType: ".epub",
		Size:     4,
	}
ale's avatar
ale committed
71 72 73 74
}

func testEbook() *Book {
	return &Book{
ale's avatar
ale committed
75
		Id: NewID(),
ale's avatar
ale committed
76
		Metadata: &Metadata{
ale's avatar
ale committed
77
			Title:       "Twenty Thousand Leagues Under The Sea",
ale's avatar
ale committed
78
			Creator:     []string{"Jules Verne"},
ale's avatar
ale committed
79
			ISBN:        []string{"1234", "2345"},
ale's avatar
ale committed
80
			Description: "A pretty cool book.",
ale's avatar
ale committed
81 82 83 84
		},
	}
}

ale's avatar
ale committed
85 86 87 88 89 90 91 92 93 94 95 96
func testEbook2() *Book {
	return &Book{
		Id: NewID(),
		Metadata: &Metadata{
			Title:       "Around The World In Eighty Days",
			Creator:     []string{"Jules Verne"},
			ISBN:        []string{"5678"},
			Description: "It's about balloons.",
		},
	}
}

ale's avatar
ale committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
func TestDatabase_Get(t *testing.T) {
	td, db := newTestDatabase(t)
	defer td.Close()

	_, err := db.GetBook(td.refbookid)
	if err != nil {
		t.Fatalf("GetBook(%d): %v", td.refbookid, err)
	}
	files, err := db.GetBookFiles(td.refbookid)
	if err != nil {
		t.Fatalf("GetBookFiles(%d): %v", td.refbookid, err)
	}
	if len(files) != 1 {
		t.Fatalf("GetBookFiles(%d) bad result: %v", td.refbookid, files)
	}
}

func TestDatabase_BookFileRelation(t *testing.T) {
	td, db := newTestDatabase(t)
	defer td.Close()

	checkFiles := func(tag string, n int) []*File {
		files, err := db.GetBookFiles(td.refbookid)
		if err != nil {
			t.Fatalf("GetBookFiles@%s(%d): %v", tag, td.refbookid, err)
		}
		if len(files) != n {
			t.Fatalf("GetBookFiles@%s(%d) bad result (exp. len=%d): %v", tag, td.refbookid, n, files)
		}
		return files
	}

	files := checkFiles("init", 1)
	file0 := files[0]
	file1 := testEpubFile(td.path, td.refbookid)
	db.PutFile(file1)
	checkFiles("post_add", 2)

	db.DeleteFile(file1.Path)
	checkFiles("post_delete", 1)

	db.DeleteFile(file0.Path)
	checkFiles("post_delete_2", 0)

	if _, err := db.GetBook(td.refbookid); err == nil {
		t.Fatal("Book was not removed when n.files==0")
	}
}

ale's avatar
ale committed
146 147
func TestDatabase_BleveLowLevelSearch(t *testing.T) {
	td, db := newTestDatabase2(t)
ale's avatar
ale committed
148 149
	defer td.Close()

150
	doSearch := func(tag string, query blevequery.Query, numExpectedResults int) {
ale's avatar
ale committed
151 152 153 154 155 156 157 158
		req := bleve.NewSearchRequestOptions(query, 100, 0, false)
		result, err := db.index.Search(req)
		if err != nil {
			t.Fatalf("%s: %v", tag, err)
		}
		if int(result.Total) != numExpectedResults {
			t.Errorf("%s: got %d results, expected %d", tag, result.Total, numExpectedResults)
		}
ale's avatar
ale committed
159 160
	}

161 162 163 164 165 166 167 168 169 170 171
	matchQueryField := func(s, f string) blevequery.Query {
		q := bleve.NewMatchQuery(s)
		q.SetField(f)
		return q
	}
	termQueryField := func(t, f string) blevequery.Query {
		q := bleve.NewTermQuery(t)
		q.SetField(f)
		return q
	}

ale's avatar
ale committed
172 173 174 175
	doSearch("match_query",
		bleve.NewMatchQuery("Leagues Under The Sea"),
		1)
	doSearch("match_query_with_field",
176
		matchQueryField("Leagues Under The Sea", "title"),
ale's avatar
ale committed
177 178
		1)
	doSearch("match_query_with_field_2",
179
		matchQueryField("Jules Verne", "author"),
ale's avatar
ale committed
180 181 182 183 184
		2)
	//doSearch("match_query_with_field_2_AND",
	//	bleve.NewMatchQuery("Hugo Verne").SetField("author"),
	//	0)
	doSearch("match_query_with_wrong_field",
185
		matchQueryField("Leagues Under The Sea", "author"),
ale's avatar
ale committed
186 187 188 189 190
		0)
	doSearch("query_string_precise",
		bleve.NewQueryStringQuery("+title:Leagues +author:verne"),
		1)
	doSearch("isbn_term_query",
191
		termQueryField("1234", "isbn"),
ale's avatar
ale committed
192 193
		1)
	doSearch("isbn_term_query_2",
194
		termQueryField("2345", "isbn"),
ale's avatar
ale committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		1)
}

func TestDatabase_Search(t *testing.T) {
	td, db := newTestDatabase2(t)
	defer td.Close()

	doSearch := func(query string, numExpectedResults int) {
		r, err := db.Search(query, 0, 100)
		if err != nil {
			t.Fatalf("Search(%s) failed: %v", query, err)
		}
		if r.NumResults != numExpectedResults {
			t.Errorf("Search(%s): got %d results, expecting %d\n%#v", query, r.NumResults, numExpectedResults, r.Results)
		}
ale's avatar
ale committed
210
	}
ale's avatar
ale committed
211 212 213 214 215 216 217 218 219 220 221 222 223

	doSearch("jules verne", 2)
	doSearch("italo calvino", 0)

	doSearch("Twenty Thousand Leagues", 1)
	doSearch("\"Twenty Thousand Leagues\"", 1)
	doSearch("title:\"Twenty Thousand Leagues\"", 1)
	doSearch("author:\"Jules Verne\"", 2)

	doSearch("author:verne", 2)
	doSearch("+title:Leagues +author:Verne", 1)
	doSearch("title:verne", 0)
	doSearch("author:vernes", 0)
ale's avatar
ale committed
224 225 226
}

func TestDatabase_Find(t *testing.T) {
ale's avatar
ale committed
227
	td, db := newTestDatabase2(t)
ale's avatar
ale committed
228
	defer td.Close()
ale's avatar
ale committed
229 230
	book, _ := db.GetBook(td.refbookid)
	m := book.Metadata
ale's avatar
ale committed
231 232

	// Find using ISBN.
ale's avatar
ale committed
233 234 235 236
	if result, err := db.Find(m.Uniques()); err != nil {
		t.Errorf("With ISBN: %v", err)
	} else if result.Id != book.Id {
		t.Errorf("Bad match with ISBN: got=%d, expected=%d", result.Id, book.Id)
ale's avatar
ale committed
237 238 239
	}

	// Find using title/author.
ale's avatar
ale committed
240 241 242 243 244
	m.ISBN = nil
	if result, err := db.Find(m.Uniques()); err != nil {
		t.Errorf("With title/author: %v", err)
	} else if result.Id != book.Id {
		t.Errorf("Bad match with title/author: got=%d, expected=%d", result.Id, book.Id)
ale's avatar
ale committed
245 246 247
	}

	// Find only using title (should fail).
ale's avatar
ale committed
248 249 250
	m.Creator = nil
	if result, err := db.Find(m.Uniques()); err == nil {
		t.Errorf("Title only: no error, result = %v", result)
ale's avatar
ale committed
251 252 253
	}
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
func TestDatabase_Reindex(t *testing.T) {
	td, db := newTestDatabase2(t)
	defer td.Close()
	book, _ := db.GetBook(td.refbookid)
	m := book.Metadata

	doTest := func() {
		// Find the first time.
		if result, err := db.Find(m.Uniques()); err != nil {
			t.Errorf("Not found: %v", err)
		} else if result.Id != book.Id {
			t.Errorf("Bad match with ISBN: got=%d, expected=%d", result.Id, book.Id)
		}
	}

	doTest()
	if err := db.Reindex(); err != nil {
		t.Fatalf("Reindex: %v", err)
	}
	doTest()
}

ale's avatar
ale committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
func TestDatabase_DumpAndRestore(t *testing.T) {
	td, db := newTestDatabase2(t)
	defer td.Close()
	book, _ := db.GetBook(td.refbookid)
	m := book.Metadata

	var buf bytes.Buffer
	if err := db.Dump(&buf); err != nil {
		t.Fatalf("Dump: %v", err)
	}

	td2, db2 := newEmptyTestDatabase(t)
	defer td2.Close()

	if err := db2.Restore(&buf); err != nil {
		t.Fatalf("Restore: %v", err)
	}

	// Find the sample book.
	if result, err := db2.Find(m.Uniques()); err != nil {
		t.Errorf("Not found: %v", err)
	} else if result.Id != book.Id {
		t.Errorf("Bad match with ISBN: got=%d, expected=%d", result.Id, book.Id)
	}
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
// func TestDatabase_Find2(t *testing.T) {
// 	td, db := newTestDatabase(t)
// 	defer td.Close()

// 	testdata := []*Metadata{
// 		&Metadata{Title: "PHILIP K", Creator: []string{"Antonio Marigonda"}, Language: []string{"it"}, Sources: []MetadataSource{MetadataSource{Name: "opf", ID: "6809f8b6-3151-41ef-b05a-0eb113746674"}}},
// 	}

// 	for _, m := range testdata {
// 		t.Logf("uniques = %v", m.Uniques())
// 		id := NewID()
// 		db.PutBook(&Book{Id: id, Metadata: m})
// 		if _, err := db.Find(m.Uniques()); err != nil {
// 			t.Errorf("Could not find book: %#v", m)
// 		}
// 	}
// }

320
func TestDatabase_Suggest(t *testing.T) {
ale's avatar
ale committed
321 322 323
	td, db := newTestDatabase(t)
	defer td.Close()

324 325 326 327 328 329 330 331
	for _, s := range []string{"jul", "jule", "jules", "ver", "vern", "verne", "twent", "thous"} {
		r, err := db.Suggest(s)
		if err != nil {
			t.Fatal(err)
		}
		if r.NumResults == 0 {
			t.Errorf("No results for '%s'", s)
		}
ale's avatar
ale committed
332 333
	}

334
	r, err := db.Suggest("foo")
ale's avatar
ale committed
335 336 337 338 339 340 341
	if err != nil {
		t.Fatal(err)
	}
	if r.NumResults != 0 {
		t.Errorf("Unexpected results: %v", r)
	}
}