Merge branch 'pretty' of https://github.com/fnp/wolnelektury into pretty
[wolnelektury.git] / apps / search / tests / index.py
1 # -*- coding: utf-8 -*-
2
3 from __future__ import with_statement
4
5 from django.conf import settings
6 from search import Index, Search, IndexStore, JVM, SearchResult
7 from catalogue import models
8 from catalogue.test_utils import WLTestCase
9 from lucene import PolishAnalyzer, Version
10 #from nose.tools import raises
11 from os import path
12
13
14 class BookSearchTests(WLTestCase):
15     def setUp(self):
16         JVM.attachCurrentThread()
17         WLTestCase.setUp(self)
18         settings.NO_SEARCH_INDEX = False
19         settings.SEARCH_INDEX = path.join(settings.MEDIA_ROOT, 'search')
20
21         txt = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
22         self.book = models.Book.from_xml_file(txt)
23
24         index = Index()
25         index.open()
26         try:
27             index.index_book(self.book)
28         except:
29             index.close()
30
31         self.search = Search()
32
33     def test_search_perfect_book_author(self):
34         books = self.search.search_perfect_book("sęp szarzyński")
35         assert len(books) == 1
36         assert books[0].book_id == self.book.id
37
38     def test_search_perfect_book_title(self):
39         books = self.search.search_perfect_book("fraszka anusie")
40         assert len(books) == 1
41         assert books[0].book_id == self.book.id
42
43     def test_search_perfect_parts(self):
44         books = self.search.search_perfect_parts("Jakoż hamować")
45         assert len(books) == 2
46         for b in books:
47             b.book_id == self.book.id
48         a = SearchResult.aggregate(books)
49         # just one fragment hit.
50         assert len(filter(lambda x: x[1], a[0].hits)) == 1
51         print a[0].process_hits()
52
53     def test_search_perfect_author_title(self):
54         books = self.search.search_perfect_book("szarzyński anusie")
55         assert books == []
56
57         books = self.search.search_book("szarzyński anusie")
58         assert len(books) == 1
59
60         books = self.search.search_book("szarzyński fraszka")
61         assert len(books) == 1
62
63     def test_search_everywhere(self):
64         books = self.search.search_everywhere("szarzyński kochanek")
65         print 'szarzyński kochanek %s' % [b.hits for b in books]
66
67         books = self.search.search_everywhere("szarzyński narcyz")
68         print 'szarzyński narcyz %s' % [b.hits for b in books]
69
70         books = self.search.search_everywhere("anusie narcyz")
71         print 'anusie narcyz %s' % [b.hits for b in books]
72
73         # theme content cross
74         books = self.search.search_everywhere("wzrok  boginie")
75         print 'wzrok boginie %s' % [b.hits for b in books]
76
77         books = self.search.search_everywhere("anusie płynęły zdroje")
78         print 'anusie płynęły zdroje %s' % [b.hits for b in books]