1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
from django.core.management import call_command
from django.test import TransactionTestCase
from haystack.query import SearchQuerySet
from .models import Note
class QueuedSearchIndexTestCase(TransactionTestCase):
def assertSearchResultLength(self, count):
self.assertEqual(count, len(SearchQuerySet()))
def assertSearchResultContains(self, pk, text):
results = SearchQuerySet().filter(id='tests.note.%s' % pk)
self.assertTrue(results)
self.assertTrue(text in results[0].text)
def setUp(self):
# Nuke the index.
call_command('clear_index', interactive=False, verbosity=0)
# Throw away all Notes
Note.objects.all().delete()
def test_update(self):
self.assertSearchResultLength(0)
note1 = Note.objects.create(content='Because everyone loves tests.')
self.assertSearchResultLength(1)
self.assertSearchResultContains(note1.pk, 'loves')
note2 = Note.objects.create(content='More test data.')
self.assertSearchResultLength(2)
self.assertSearchResultContains(note2.pk, 'More')
note3 = Note.objects.create(content='The test data. All done.')
self.assertSearchResultLength(3)
self.assertSearchResultContains(note3.pk, 'All done')
note3.content = 'Final test note FOR REAL'
note3.save()
self.assertSearchResultLength(3)
self.assertSearchResultContains(note3.pk, 'FOR REAL')
def test_delete(self):
note1 = Note.objects.create(content='Because everyone loves tests.')
note2 = Note.objects.create(content='More test data.')
note3 = Note.objects.create(content='The test data. All done.')
self.assertSearchResultLength(3)
note1.delete()
self.assertSearchResultLength(2)
note2.delete()
self.assertSearchResultLength(1)
note3.delete()
self.assertSearchResultLength(0)
def test_complex(self):
note1 = Note.objects.create(content='Because everyone loves test.')
self.assertSearchResultLength(1)
Note.objects.create(content='More test data.')
self.assertSearchResultLength(2)
note1.delete()
self.assertSearchResultLength(1)
note3 = Note.objects.create(content='The test data. All done.')
self.assertSearchResultLength(2)
note3.title = 'Final test note FOR REAL'
note3.save()
self.assertSearchResultLength(2)
note3.delete()
self.assertSearchResultLength(1)
|