PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/_tests/test_thread.py

Relevant Search: With Applications for Solr and Elasticsearch

For more in depth reading about search, ranking and generally everything you could ever want to know about how lucene, elasticsearch or solr work under the hood I highly suggest this book. Easily one of the most interesting technical books I have read in a long time. If you are tasked with solving search relevance problems even if not in Solr or Elasticsearch it should be your first reference. Amazon Affiliate Link
http://django-search-lucene.googlecode.com/
Python | 127 lines | 67 code | 27 blank | 33 comment | 16 complexity | 9157ef86abe9b875df31acb657f728c0 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2#    Copyright 2005,2006,2007,2008 Spike^ekipS <spikeekips@gmail.com>
  3#
  4#       This program is free software; you can redistribute it and/or modify
  5#    it under the terms of the GNU General Public License as published by
  6#    the Free Software Foundation; either version 2 of the License, or
  7#    (at your option) any later version.
  8#
  9#    This program is distributed in the hope that it will be useful,
 10#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 11#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12#    GNU General Public License for more details.
 13#
 14#    You should have received a copy of the GNU General Public License
 15#    along with this program; if not, write to the Free Software
 16#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 17
 18import sys, sqlite3, time, threading, datetime, random, unittest
 19
 20class PyLuceneThreadTestCase (unittest.TestCase):
 21
 22    def setUp (self) :
 23        self.from_model = models_tests.doc.objects
 24        self.from_indexed = sys.MODELS_REGISTERED.get("tests.doc")[0].objects
 25
 26    def update_document_without_save (self, mainThread=False, ) :
 27        if not mainThread :
 28            pylucene.initialize_vm()
 29
 30        self.documents = list(self.from_model.all())
 31        random.shuffle(self.documents)
 32        for o in self.documents :
 33            __title = str(o.pk) + " : " + o.title + str(random.random() * 1000)
 34            __summary = o.summary + str(random.random() * 1000)
 35
 36            o.title = __title
 37            o.summary = __summary
 38
 39            sys.INDEX_MANAGER.execute("index_update", o)
 40
 41            o_n = self.from_indexed.get(pk=o.pk)
 42            self.assertEqual(o.title, o_n.title, )
 43            self.assertEqual(o.summary, o_n.summary, )
 44
 45    def update_document_using_save (self, mainThread=False, ) :
 46        """
 47        In SQLite, there will be a thread-lock problems.
 48        """
 49        if not mainThread :
 50            pylucene.initialize_vm()
 51
 52        self.documents = list(self.from_model.all())
 53        random.shuffle(self.documents)
 54        for o in self.documents :
 55            __title = str(o.pk) + " : " + o.title + str(random.random() * 1000)
 56            __summary = o.summary + str(random.random() * 1000)
 57
 58            o.title = __title
 59            o.summary = __summary
 60
 61            try :
 62                o.save()
 63            except ObjectDoesNotExist :
 64                return
 65            except sqlite3.OperationalError:
 66                return
 67            except Exception, e :
 68                print "[EE] Save error,", e
 69                raise
 70            else :
 71                try :
 72                    o_n = self.from_indexed.get(pk=o.pk)
 73                except ObjectDoesNotExist :
 74                    pass
 75                except Exception, e :
 76                    print "[EE] objects_search,", e
 77
 78    def test_thread(self):
 79        threads = []
 80        for i in xrange(30) :
 81            threads.append(threading.Thread(target=self.update_document_without_save))
 82
 83        [thread.start() for thread in threads]
 84        [thread.join() for thread in threads]
 85
 86
 87if __name__ == "__main__" :
 88    from django.conf import settings
 89    settings.SEARCH_STORAGE_PATH = settings.SEARCH_STORAGE_PATH  + "_test"
 90    settings.SEARCH_STORAGE_TYPE = "fs"
 91    settings.DEBUG = False
 92
 93    from django.contrib.webdesign.lorem_ipsum import words, paragraphs
 94    from django.db.models import ObjectDoesNotExist
 95
 96    import pylucene, core
 97    import models as models_tests
 98    models_tests.add()
 99
100    models_tests.cleanup_index()
101    models_tests.cleanup_docs()
102    models_tests.insert_docs(10)
103
104    unittest.main(testRunner=models_tests.SearcherTestRunner(verbosity=2))
105    sys.exit()
106
107
108"""
109Description
110-----------
111
112
113ChangeLog
114---------
115
116
117Usage
118-----
119
120
121"""
122
123__author__ =  "Spike^ekipS <spikeekips@gmail.com>"
124
125
126
127