Django 1.7, working version.
[wolnelektury.git] / apps / search / tests / index.py
index ef253e4..b8d4472 100644 (file)
@@ -1,78 +1,56 @@
 # -*- coding: utf-8 -*-
-
-from __future__ import with_statement
-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from unittest import skipIf
 from django.conf import settings
-from search import Index, Search, IndexStore, JVM, SearchResult
-from catalogue import models
-from catalogue.test_utils import WLTestCase
-from lucene import PolishAnalyzer, Version
-#from nose.tools import raises
+from django.test.utils import override_settings
+from catalogue.test_utils import WLTestCase, get_fixture
 from os import path
-
-
+import tempfile
+from catalogue.models import Book, Tag
+from search.index import Index, Search, SearchResult
+import catalogue
+import opds
+
+
+@override_settings(
+    SEARCH_INDEX = tempfile.mkdtemp(prefix='djangotest_search_'),
+)
+@skipIf(getattr(settings, 'NO_SEARCH_INDEX', False),
+    u'Requires search server and NO_SEARCH_INDEX=False.')
 class BookSearchTests(WLTestCase):
     def setUp(self):
-        JVM.attachCurrentThread()
         WLTestCase.setUp(self)
-        settings.NO_SEARCH_INDEX = False
-        settings.SEARCH_INDEX = path.join(settings.MEDIA_ROOT, 'search')
-
-        txt = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
-        self.book = models.Book.from_xml_file(txt)
 
         index = Index()
-        index.open()
-        try:
-            index.index_book(self.book)
-        except:
-            index.close()
-
         self.search = Search()
+        index.delete_query(self.search.index.query(uid="*"))
+        index.index.commit()
+
+        self.do_doktora = Book.from_xml_file(
+            get_fixture('do-doktora.xml', opds))
+        self.do_anusie = Book.from_xml_file(
+            get_fixture('fraszka-do-anusie.xml', catalogue))
 
     def test_search_perfect_book_author(self):
-        books = self.search.search_perfect_book("sęp szarzyński")
+        books = self.search.search_books(self.search.index.query(authors=u"sęp szarzyński"))
         assert len(books) == 1
-        assert books[0].book_id == self.book.id
+        assert books[0].id == self.do_anusie.id
 
+        # here we lack slop functionality as well
     def test_search_perfect_book_title(self):
-        books = self.search.search_perfect_book("fraszka anusie")
+        books = self.search.search_books(self.search.index.query(title=u"fraszka do anusie"))
         assert len(books) == 1
-        assert books[0].book_id == self.book.id
-
-    def test_search_perfect_parts(self):
-        books = self.search.search_perfect_parts("Jakoż hamować")
-        assert len(books) == 2
-        for b in books:
-            b.book_id == self.book.id
-        a = SearchResult.aggregate(books)
-        # just one fragment hit.
-        assert len(filter(lambda x: x[1], a[0].hits)) == 1
-        print a[0].process_hits()
-
-    def test_search_perfect_author_title(self):
-        books = self.search.search_perfect_book("szarzyński anusie")
-        assert books == []
-
-        books = self.search.search_book("szarzyński anusie")
-        assert len(books) == 1
-
-        books = self.search.search_book("szarzyński fraszka")
-        assert len(books) == 1
-
-    def test_search_everywhere(self):
-        books = self.search.search_everywhere("szarzyński kochanek")
-        print 'szarzyński kochanek %s' % [b.hits for b in books]
-
-        books = self.search.search_everywhere("szarzyński narcyz")
-        print 'szarzyński narcyz %s' % [b.hits for b in books]
-
-        books = self.search.search_everywhere("anusie narcyz")
-        print 'anusie narcyz %s' % [b.hits for b in books]
-
-        # theme content cross
-        books = self.search.search_everywhere("wzrok  boginie")
-        print 'wzrok boginie %s' % [b.hits for b in books]
+        assert books[0].id == self.do_anusie.id
+
+    # TODO: Add slop option to sunburnt
+    # def test_search_perfect_parts(self):
+    #     books = self.search.search_phrase(u"Jakoż hamować")
+    #     assert len(books) == 2
+    #     for b in books:
+    #         b.book_id == self.book.id
+    #     a = SearchResult.aggregate(books)
+    #     # just one fragment hit.
+    #     assert len(a[0].hits) == 1
 
-        books = self.search.search_everywhere("anusie płynęły zdroje")
-        print 'anusie płynęły zdroje %s' % [b.hits for b in books]