fix
[wolnelektury.git] / src / search / mock_search.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from unittest.mock import Mock
5 from catalogue.models import Book, Tag
6 from random import randint, choice
7
8
9 class Search(Mock):
10     """
11     Search mock for development without setting up Solr.
12
13     Instead of connecting to an actual search server, it returns
14     some random results for any query.
15     """
16     class MockIndex(Mock):
17         def analyze(*args, **kwargs):
18             return []
19
20     index = MockIndex()
21
22     def search_words(self, words, fields, required=None, book=True, picture=False):
23         from .index import SearchResult
24
25         max_results = 20
26         
27         if picture: return []
28
29         qs = Book.objects.filter(findable=True).order_by('?')
30         results = []
31         for book in qs[:randint(1, max_results)]:
32             doc = {
33                 'score': randint(0, 100),
34                 'book_id': book.pk,
35                 'published_date': randint(1000, 1920),
36                 }
37             res = SearchResult(doc, how_found='mock', query_terms=words)
38             results.append(res)
39         return results
40