from django.test import TestCase from tempfile import TemporaryDirectory from library.models import Collection, Work from library.indexer import index_works, model_search from library.indexer import whoosh JAZZ_STANDARDS = ( ["But Not For Me", "Gershwin, George & Ira"], ["Autumn Leaves", "Kosma, Joseph"], ["Best of Gershwin", "Compilation"], ) CLASSICAL_WORKS = ( ["Symphony No.5", "Beethoven, L"], ["March from Aieda", "Verdi"], ) class WhooshIndexTestCase(TestCase): @classmethod def setUpTestData(cls): jazz = Collection.objects.create(name="Jazz Standards", prefix="jazz") classical = Collection.objects.create( name="Classical Music", prefix="classical" ) for name, composer in JAZZ_STANDARDS: jazz.works.create(name=name, composer=composer) for name, composer in CLASSICAL_WORKS: classical.works.create(name=name, composer=composer) def test_setup(self): self.assertEqual(Collection.objects.all().count(), 2) self.assertEqual(Work.objects.all().count(), 5) def test_indexer(self): expected = [ ("beethoven", [], ["Symph"]), ("Ira", [], ["But N"]), ("bethoven", [], ["Symph"]), ("George", [1], ["But N"]), ("George", [2], []), ("George", [1, 2], ["But N"]), ("not for me", [], ["But N"]), ("Gershwin", [], ["Best ", "But N"]), ("composer:Gershwin", [], ["But N"]), ] with TemporaryDirectory() as d: whoosh.index_path = d index_works(Work.objects.all()) for query, collections, result in expected: self.assertListEqual( [x.name[:5] for x in model_search(query, collections)], result )