From 83679a7775a3745782afa113444916a0ac135c40 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 16 Jul 2016 19:07:07 -0700 Subject: [PATCH] Test search queries and highlight_string(). This increases test coverage by exercising highlight_string(). It also gives deeper test coverage to NarrowBuilder.by_search(), which had test coverage before, but only in terms of inspecting the SQL that was generated. This test actually runs the SQL under the hood. This partly fixes #1006. --- zerver/tests/test_narrow.py | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index fb384c64f8..8c47d2d082 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -1,5 +1,6 @@ from __future__ import absolute_import from __future__ import print_function +from django.db import connection from sqlalchemy.sql import ( and_, select, column, compiler ) @@ -468,6 +469,68 @@ class GetOldMessagesTest(AuthedTestCase): for message in result["messages"]: self.assertEqual(message["sender_email"], "othello@zulip.com") + def test_get_old_messages_with_search(self): + self.login("cordelia@zulip.com") + + messages_to_search = [ + ('breakfast', 'there are muffins in the conference room'), + ('lunch plans', 'I am hungry!'), + ('meetings', 'discuss lunch after lunch'), + ('meetings', 'please bring your laptops to take notes'), + ('dinner', 'Anybody staying late tonight?'), + ] + + for topic, content in messages_to_search: + self.send_message( + sender_name="cordelia@zulip.com", + raw_recipients="Verona", + message_type=Recipient.STREAM, + content=content, + subject=topic, + ) + + # We use brute force here and update our text search index + # for the entire zerver_message table (which is small in test + # mode). In production there is an async process which keeps + # the search index up to date. + with connection.cursor() as cursor: + cursor.execute(""" + UPDATE zerver_message SET + search_tsvector = to_tsvector('zulip.english_us_search', + subject || rendered_content) + """) + + narrow = [ + dict(operator='sender', operand='cordelia@zulip.com'), + dict(operator='search', operand='lunch'), + ] + result = self.post_with_params(dict( + narrow=ujson.dumps(narrow), + anchor=0, + num_after=10, + )) + self.check_well_formed_messages_response(result) + self.assertEqual(len(result['messages']), 2) + messages = result['messages'] + + meeting_message = [m for m in messages if m['subject'] == 'meetings'][0] + self.assertEqual( + meeting_message['match_subject'], + 'meetings') + self.assertEqual( + meeting_message['match_content'], + '

discuss lunch after ' + + 'lunch

') + + meeting_message = [m for m in messages if m['subject'] == 'lunch plans'][0] + self.assertEqual( + meeting_message['match_subject'], + 'lunch plans') + self.assertEqual( + meeting_message['match_content'], + '

I am hungry!

') + + def test_get_old_messages_with_only_searching_anchor(self): """ Test that specifying an anchor but 0 for num_before and num_after