diff options
author | Bruno Wagner <bwagner@thoughtworks.com> | 2014-10-16 11:53:56 +0200 |
---|---|---|
committer | Bruno Wagner <bwagner@thoughtworks.com> | 2014-10-16 11:53:56 +0200 |
commit | eacd5fd5f7f47791ddc2e5568b8e4622420d8562 (patch) | |
tree | f303e7593c8eec8cc626b0df489b7e81128ca6dd /service | |
parent | 693957054255b1807ae9c09478a06e191114b7fb (diff) |
#96 Mail searches are now paginated, the pages number start at 1 now
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/search.py | 9 | ||||
-rw-r--r-- | service/pixelated/controllers/mails_controller.py | 2 | ||||
-rw-r--r-- | service/test/integration/search_test.py | 30 | ||||
-rw-r--r-- | service/test/support/integration_helper.py | 4 |
4 files changed, 38 insertions, 7 deletions
diff --git a/service/pixelated/adapter/search.py b/service/pixelated/adapter/search.py index 72e714e9..6b579d87 100644 --- a/service/pixelated/adapter/search.py +++ b/service/pixelated/adapter/search.py @@ -116,16 +116,17 @@ class SearchEngine(object): results = searcher.search(query, **options) return results - def search(self, query): - options = {'limit': 100} + def search(self, query, window, page): + page = int(page) if (page is not None and int(page) > 0) else 1 + window = int(window) or 25 query = query.replace('\"', '') query = query.replace('-in:', 'AND NOT tag:') query = query.replace('in:all', '*') with self._index.searcher() as searcher: - query = QueryParser('body', self._index.schema).parse(query) - results = searcher.search(query, **options) + query = QueryParser('raw', self._index.schema).parse(query) + results = searcher.search_page(query, page, pagelen=window) return [mail['ident'] for mail in results] def remove_from_index(self, mail_id): diff --git a/service/pixelated/controllers/mails_controller.py b/service/pixelated/controllers/mails_controller.py index 6765fc78..6bd2fe99 100644 --- a/service/pixelated/controllers/mails_controller.py +++ b/service/pixelated/controllers/mails_controller.py @@ -29,7 +29,7 @@ class MailsController: self._search_engine = search_engine def mails(self, _request=request): - mail_ids = self._search_engine.search(_request.args.get('q')) + mail_ids = self._search_engine.search(_request.args.get('q'), _request.args.get('w'), _request.args.get('p')) mails = self._mail_service.mails(mail_ids) mails = sorted(mails, key=lambda mail: dateparser.parse(mail.date), reverse=True) diff --git a/service/test/integration/search_test.py b/service/test/integration/search_test.py index 7323866d..81d1fad2 100644 --- a/service/test/integration/search_test.py +++ b/service/test/integration/search_test.py @@ -58,3 +58,33 @@ class SearchTest(unittest.TestCase, SoledadTestBase): all_tag_names = [t['name'] for t in all_tags] self.assertEqual(1, len(all_tag_names)) self.assertTrue('sometag' in all_tag_names) + + def test_search_mails_different_window(self): + input_mail = MailBuilder().build_input_mail() + input_mail2 = MailBuilder().build_input_mail() + self.add_mail_to_inbox(input_mail) + self.add_mail_to_inbox(input_mail2) + + first_page = self.get_mails_by_tag('inbox', page=1, window=1) + + self.assertEqual(len(first_page), 1) + + def test_search_mails_with_multiple_pages(self): + input_mail = MailBuilder().build_input_mail() + input_mail2 = MailBuilder().build_input_mail() + self.add_mail_to_inbox(input_mail) + self.add_mail_to_inbox(input_mail2) + + first_page = self.get_mails_by_tag('inbox', page=1, window=1) + second_page = self.get_mails_by_tag('inbox', page=2, window=1) + + idents = [input_mail.ident, input_mail2.ident] + + self.assertIn(first_page[0].ident, idents) + self.assertIn(second_page[0].ident, idents) + + def test_page_zero_fetches_first_page(self): + input_mail = MailBuilder().build_input_mail() + self.add_mail_to_inbox(input_mail) + page = self.get_mails_by_tag('inbox', page=0, window=1) + self.assertEqual(page[0].ident, input_mail.ident) diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py index 268eb812..f0f3b02a 100644 --- a/service/test/support/integration_helper.py +++ b/service/test/support/integration_helper.py @@ -173,8 +173,8 @@ class SoledadTestBase: app_factory._setup_routes(self.client.application, home_controller, mails_controller, tags_controller, features_controller) - def get_mails_by_tag(self, tag): - response = json.loads(self.client.get("/mails?q=tag:" + tag).data) + def get_mails_by_tag(self, tag, page=1, window=100): + response = json.loads(self.client.get("/mails?q=tag:%s&w=%s&p=%s" % (tag, window, page)).data) return [ResponseMail(m) for m in response['mails']] def post_mail(self, data): |