def test_remove_object(self): self.mi.update() self.assertEqual(self.sb.search('*')['hits'], 3) mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.sb.search('*')['hits'], 4) self.mi.remove_object(mock) self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'1'), (u'core.mockmodel', u'2'), (u'core.mockmodel', u'3')]) # Put it back so we can test passing kwargs. mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.sb.search('*')['hits'], 4) self.mi.remove_object(mock, commit=False) self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'1'), (u'core.mockmodel', u'2'), (u'core.mockmodel', u'3'), (u'core.mockmodel', u'20')]) self.sb.clear()
def test_thread_safety(self): # This is a regression. ``SearchIndex`` used to write to # ``self.prepared_data``, which would leak between threads if things # went too fast. exceptions = [] def threaded_prepare(index_queue, index, model): try: index.queue = index_queue prepped = index.prepare(model) except Exception as e: exceptions.append(e) raise class ThreadedSearchIndex(GoodMockSearchIndex): def prepare_author(self, obj): if obj.pk == 20: time.sleep(0.1) else: time.sleep(0.5) index_queue.put(self.prepared_data['author']) return self.prepared_data['author'] tmi = ThreadedSearchIndex() index_queue = queue.Queue() mock_1 = MockModel() mock_1.pk = 20 mock_1.author = 'foo' mock_1.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) mock_2 = MockModel() mock_2.pk = 21 mock_2.author = 'daniel%s' % mock_2.id mock_2.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) th1 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_1)) th2 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_2)) th1.start() th2.start() th1.join() th2.join() mock_1_result = index_queue.get() mock_2_result = index_queue.get() self.assertEqual(mock_1_result, u'foo') self.assertEqual(mock_2_result, u'daniel21')
def test_thread_safety(self): # This is a regression. ``SearchIndex`` used to write to # ``self.prepared_data``, which would leak between threads if things # went too fast. exceptions = [] def threaded_prepare(index_queue, index, model): try: index.queue = index_queue prepped = index.prepare(model) except Exception as e: exceptions.append(e) raise class ThreadedSearchIndex(GoodMockSearchIndex): def prepare_author(self, obj): if obj.pk == 20: time.sleep(0.1) else: time.sleep(0.5) index_queue.put(self.prepared_data["author"]) return self.prepared_data["author"] tmi = ThreadedSearchIndex() index_queue = queue.Queue() mock_1 = MockModel() mock_1.pk = 20 mock_1.author = "foo" mock_1.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) mock_2 = MockModel() mock_2.pk = 21 mock_2.author = "daniel%s" % mock_2.id mock_2.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) th1 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_1)) th2 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_2)) th1.start() th2.start() th1.join() th2.join() mock_1_result = index_queue.get() mock_2_result = index_queue.get() self.assertEqual(mock_1_result, "foo") self.assertEqual(mock_2_result, "daniel21")
def test_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 7) self.assertEqual(sorted(self.mi.prepare(mock).keys()), ['author', 'django_ct', 'django_id', 'extra', 'id', 'pub_date', 'text'])
def test_remove_object(self): self.mi.update() self.assertEqual(self.sb.search("*")["hits"], 3) mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.sb.search("*")["hits"], 4) self.mi.remove_object(mock) self.assertEqual( [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]], [("core.mockmodel", "1"), ("core.mockmodel", "2"), ("core.mockmodel", "3")], ) # Put it back so we can test passing kwargs. mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.sb.search("*")["hits"], 4) self.mi.remove_object(mock, commit=False) self.assertEqual( [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]], [ ("core.mockmodel", "1"), ("core.mockmodel", "2"), ("core.mockmodel", "3"), ("core.mockmodel", "20"), ], ) self.sb.clear()
def test_custom_index_fieldname(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) cofnmi = GoodOverriddenFieldNameMockSearchIndex() self.assertEqual(len(cofnmi.prepare(mock)), 6) self.assertEqual(sorted(cofnmi.prepare(mock).keys()), ['django_ct', 'django_id', 'hello', 'id', 'more_content', 'name_s']) self.assertEqual(cofnmi.prepared_data['name_s'], u'daniel20') self.assertEqual(cofnmi.get_content_field(), 'more_content')
def test_custom_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee']) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee'])
def test_prepare(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 7) self.assertEqual( sorted(self.mi.prepare(mock).keys()), ["author", "django_ct", "django_id", "extra", "id", "pub_date", "text"], )
def test_update_object(self): self.sb.clear() self.assertEqual(self.sb.search('*')['hits'], 0) mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'20')]) self.sb.clear()
def test_proper_field_resolution(self): mrofsc = MROFieldsSearchChild() mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) mock.test_a = "This is A" mock.test_b = "This is B" self.assertEqual(len(mrofsc.fields), 1) prepped_data = mrofsc.prepare(mock) self.assertEqual(len(prepped_data), 4) self.assertEqual(prepped_data["text"], "This is A")
def test_proper_field_resolution(self): mrofsc = MROFieldsSearchChild() mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) mock.test_a = 'This is A' mock.test_b = 'This is B' self.assertEqual(len(mrofsc.fields), 1) prepped_data = mrofsc.prepare(mock) self.assertEqual(len(prepped_data), 4) self.assertEqual(prepped_data['text'], 'This is A')
def test_nullable(self): mock = MockModel() mock.pk = 20 mock.author = None mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.cnmi.prepare(mock) self.assertEqual(len(prepared_data), 6) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'id', 'text']) prepared_data = self.cnmi.full_prepare(mock) self.assertEqual(len(prepared_data), 4) self.assertEqual(sorted(prepared_data.keys()), ['django_ct', 'django_id', 'id', 'text'])
def test_custom_index_fieldname(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) cofnmi = GoodOverriddenFieldNameMockSearchIndex() self.assertEqual(len(cofnmi.prepare(mock)), 6) self.assertEqual( sorted(cofnmi.prepare(mock).keys()), ["django_ct", "django_id", "hello", "id", "more_content", "name_s"], ) self.assertEqual(cofnmi.prepared_data["name_s"], "daniel20") self.assertEqual(cofnmi.get_content_field(), "more_content")
def test_update_object(self): self.sb.clear() self.assertEqual(self.sb.search("*")["hits"], 0) mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual( [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]], [("core.mockmodel", "20")], ) self.sb.clear()
def test_custom_facet_fields(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel' mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.gfmsi.prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact', 'text']) prepared_data = self.gfmsi.full_prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact', 'text']) self.assertEqual(prepared_data['author_foo'], u"Hi, I'm daniel") self.assertEqual(prepared_data['pub_date_exact'], '2010-10-26T01:54:32')
def test_custom_prepare_author(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual( sorted(self.cmi.prepare(mock).keys()), [ "author", "author_exact", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "pub_date_exact", "text", "whee", ], ) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual( sorted(self.cmi.full_prepare(mock).keys()), [ "author", "author_exact", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "pub_date_exact", "text", "whee", ], ) self.assertEqual(self.cmi.prepared_data["author"], "Hi, I'm daniel20") self.assertEqual(self.cmi.prepared_data["author_exact"], "Hi, I'm daniel20")
def test_custom_facet_fields(self): mock = MockModel() mock.pk = 20 mock.author = "daniel" mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.gfmsi.prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual( sorted(prepared_data.keys()), [ "author", "author_foo", "django_ct", "django_id", "id", "pub_date", "pub_date_exact", "text", ], ) prepared_data = self.gfmsi.full_prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual( sorted(prepared_data.keys()), [ "author", "author_foo", "django_ct", "django_id", "id", "pub_date", "pub_date_exact", "text", ], ) self.assertEqual(prepared_data["author_foo"], "Hi, I'm daniel") self.assertEqual(prepared_data["pub_date_exact"], "2010-10-26T01:54:32")