Package trac :: Package search :: Package tests :: Module web_ui

Source Code for Module trac.search.tests.web_ui

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2015-2020 Edgewall Software 
  4  # All rights reserved. 
  5  # 
  6  # This software is licensed as described in the file COPYING, which 
  7  # you should have received as part of this distribution. The terms 
  8  # are also available at https://trac.edgewall.org/wiki/TracLicense. 
  9  # 
 10  # This software consists of voluntary contributions made by many 
 11  # individuals. For the exact contribution history, see the revision 
 12  # history and logs, available at https://trac.edgewall.org/log/. 
 13   
 14  import os 
 15  import pkg_resources 
 16  import unittest 
 17   
 18  from trac.search.web_ui import SearchModule 
 19  from trac.test import EnvironmentStub, MockRequest 
 20  from trac.ticket.model import Ticket 
 21  from trac.ticket.web_ui import TicketModule 
 22  from trac.wiki.admin import WikiAdmin 
 23  from trac.wiki.web_ui import WikiModule 
 24  from trac.web.api import RequestDone 
 25   
 26   
27 -class SearchModuleTestCase(unittest.TestCase):
28
29 - def setUp(self):
30 self.env = EnvironmentStub() 31 self.search_module = SearchModule(self.env) 32 pages_dir = pkg_resources.resource_filename('trac.wiki', 33 'default-pages') 34 for page_name in ('WikiStart', 'TracModWSGI'): 35 page = os.path.join(pages_dir, page_name) 36 WikiAdmin(self.env).import_page(page, page_name)
37
38 - def tearDown(self):
39 self.env.reset_db()
40
41 - def _insert_ticket(self, **kw):
42 """Helper for inserting a ticket into the database""" 43 ticket = Ticket(self.env) 44 for k, v in kw.items(): 45 ticket[k] = v 46 return ticket.insert()
47
49 for _ in range(0, 21): 50 self._insert_ticket(summary="Trac") 51 req = MockRequest(self.env, 52 args={'page': '3', 'q': 'Trac', 'ticket': 'on'}) 53 54 data = self.search_module.process_request(req)[1] 55 56 self.assertEqual([], req.chrome['warnings']) 57 self.assertEqual(2, data['results'].page)
58
60 """Out of range value for page defaults to page 1.""" 61 for _ in range(0, 20): 62 self._insert_ticket(summary="Trac") 63 req = MockRequest(self.env, 64 args={'page': '3', 'q': 'Trac', 'ticket': 'on'}) 65 66 data = self.search_module.process_request(req)[1] 67 68 self.assertIn("Page 3 is out of range.", req.chrome['warnings']) 69 self.assertEqual(0, data['results'].page)
70
71 - def test_camelcase_quickjump(self):
72 """CamelCase word does quick-jump.""" 73 req = MockRequest(self.env, args={'q': 'WikiStart'}) 74 75 self.assertRaises(RequestDone, 76 self.search_module.process_request, req) 77 78 self.assertEqual('http://example.org/trac.cgi/wiki/WikiStart', 79 req.headers_sent['Location']) 80 self.assertIn("You arrived here through", req.chrome['notices'][0]) 81 self.assertIn('<a href="/trac.cgi/search?' 82 'q=WikiStart&amp;noquickjump=1">here</a>', 83 req.chrome['notices'][0])
84
86 """Non-CamelCase word does not quick-jump.""" 87 req = MockRequest(self.env, args={'q': 'TracModWSGI'}) 88 89 data = self.search_module.process_request(req)[1] 90 91 results = list(data['results']) 92 self.assertIsNone(data['quickjump']) 93 self.assertEqual('TracModWSGI', data['query']) 94 self.assertEqual(1, len(results)) 95 self.assertEqual('/trac.cgi/wiki/TracModWSGI', results[0]['href']) 96 self.assertEqual([], req.chrome['notices'])
97 98
99 -def test_suite():
100 suite = unittest.TestSuite() 101 suite.addTest(unittest.makeSuite(SearchModuleTestCase)) 102 return suite
103 104 105 if __name__ == '__main__': 106 unittest.main(defaultTest='test_suite') 107