| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2026, Hassan Raza <raihassanraza10 at gmail.com> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from book_list.local_books import ( |
| 6 |
matching_book_indices_for_query, metadata_search_text, |
| 7 |
normalize_local_search_query |
| 8 |
) |
| 9 |
from testing import assert_equal, test |
| 10 |
|
| 11 |
|
| 12 |
def sample_books(): |
| 13 |
return { |
| 14 |
0: {'metadata': { |
| 15 |
'title': 'Dune Messiah', |
| 16 |
'authors': ['Frank Herbert'], |
| 17 |
'tags': ['Science Fiction', 'Arrakis'], |
| 18 |
'formats': ['EPUB'], |
| 19 |
'identifiers': {'isbn': '9780593098233'}, |
| 20 |
}}, |
| 21 |
1: {'metadata': { |
| 22 |
'title': 'The Left Hand of Darkness', |
| 23 |
'authors': ['Ursula K. Le Guin'], |
| 24 |
'publisher': 'Ace Books', |
| 25 |
'series': 'Hainish Cycle', |
| 26 |
'formats': ['AZW3'], |
| 27 |
}}, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
@test |
| 32 |
def local_book_search_query_normalization(): |
| 33 |
assert_equal(normalize_local_search_query(' Dune Messiah '), 'dune messiah') |
| 34 |
assert_equal(normalize_local_search_query(None), '') |
| 35 |
|
| 36 |
|
| 37 |
@test |
| 38 |
def local_book_search_metadata_text(): |
| 39 |
text = metadata_search_text({ |
| 40 |
'title': 'Title', |
| 41 |
'authors': ['Author'], |
| 42 |
'identifiers': {'isbn': '123'}, |
| 43 |
'lang_names': {'eng': 'English'}, |
| 44 |
'comments': 'not searchable', |
| 45 |
'unknown_field': 'hidden value', |
| 46 |
}) |
| 47 |
assert_equal(text.indexOf('title') >= 0, True) |
| 48 |
assert_equal(text.indexOf('author') >= 0, True) |
| 49 |
assert_equal(text.indexOf('isbn') >= 0, True) |
| 50 |
assert_equal(text.indexOf('123') >= 0, True) |
| 51 |
assert_equal(text.indexOf('english') >= 0, True) |
| 52 |
assert_equal(text.indexOf('authors'), -1) |
| 53 |
assert_equal(text.indexOf('not searchable'), -1) |
| 54 |
assert_equal(text.indexOf('hidden value'), -1) |
| 55 |
|
| 56 |
|
| 57 |
@test |
| 58 |
def local_book_search_matches_all_terms(): |
| 59 |
books = sample_books() |
| 60 |
indices = [0, 1] |
| 61 |
assert_equal(matching_book_indices_for_query(books, indices, ''), indices) |
| 62 |
assert_equal(matching_book_indices_for_query(books, indices, 'dune frank'), [0]) |
| 63 |
assert_equal(matching_book_indices_for_query(books, indices, 'science epub'), [0]) |
| 64 |
assert_equal(matching_book_indices_for_query(books, indices, 'ursula hainish'), [1]) |
| 65 |
assert_equal(matching_book_indices_for_query(books, indices, 'missing'), []) |