Merge branch 'production' 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.SEARCH_INDEX = path.join(settings.MEDIA_ROOT, 'search')
19
20         txt = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
21         self.book = models.Book.from_xml_file(txt)
22
23         index = Index()
24         index.open()
25         try:
26             index.index_book(self.book)
27         except:
28             index.close()
29
30         self.search = Search()
31
32     def test_search_perfect_book_author(self):
33         books = self.search.search_perfect_book("sęp szarzyński")
34         assert len(books) == 1
35         assert books[0].book_id == self.book.id
36
37     def test_search_perfect_book_title(self):
38         books = self.search.search_perfect_book("fraszka anusie")
39         assert len(books) == 1
40         assert books[0].book_id == self.book.id
41
42     def test_search_perfect_parts(self):
43         books = self.search.search_perfect_parts("Jakoż hamować")
44         assert len(books) == 2
45         for b in books:
46             b.book_id == self.book.id
47         a = SearchResult.aggregate(books)
48         # just one fragment hit.
49         assert len(filter(lambda x: x[1], a[0].hits)) == 1
50         print a[0].process_hits()
51
52     def test_search_perfect_author_title(self):
53         books = self.search.search_perfect_book("szarzyński anusie")
54         assert books == []
55
56         books = self.search.search_book("szarzyński anusie")
57         assert len(books) == 1
58
59         books = self.search.search_book("szarzyński fraszka")
60         assert len(books) == 1
61
62     def test_search_everywhere(self):
63         books = self.search.search_everywhere("szarzyński kochanek")
64         print 'szarzyński kochanek %s' % [b.hits for b in books]
65
66         books = self.search.search_everywhere("szarzyński narcyz")
67         print 'szarzyński narcyz %s' % [b.hits for b in books]
68
69         books = self.search.search_everywhere("anusie narcyz")
70         print 'anusie narcyz %s' % [b.hits for b in books]
71
72         # theme content cross
73         books = self.search.search_everywhere("wzrok  boginie")
74         print 'wzrok boginie %s' % [b.hits for b in books]
75
76         books = self.search.search_everywhere("anusie płynęły zdroje")
77         print 'anusie płynęły zdroje %s' % [b.hits for b in books]