~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/browser/tests/test_bugtask.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-10-31 14:42:32 UTC
  • mfrom: (14128.3.63 navigate-batches)
  • Revision ID: launchpad@pqm.canonical.com-20111031144232-hnxlaf3l7cf857ah
[r=deryck][bug=882264] AJAX batch navigation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
from contextlib import contextmanager
7
7
from datetime import datetime
8
8
import re
 
9
import urllib
9
10
 
10
11
from lazr.lifecycle.event import ObjectModifiedEvent
11
12
from lazr.restful.interfaces import IJSONRequestCache
1286
1287
 
1287
1288
    layer = DatabaseFunctionalLayer
1288
1289
 
1289
 
    server_listing = soupmatchers.Tag(
1290
 
        'Server', 'em', text='Server-side mustache')
1291
 
 
1292
1290
    client_listing = soupmatchers.Tag(
1293
1291
        'client-listing', True, attrs={'id': 'client-listing'})
1294
1292
 
1295
 
    def makeView(self, bugtask=None):
1296
 
        request = LaunchpadTestRequest()
 
1293
    def makeView(self, bugtask=None, size=None, memo=None, orderby=None,
 
1294
                 forwards=True):
 
1295
        """Make a BugTaskSearchListingView.
 
1296
 
 
1297
        :param bugtask: The task to use for searching.
 
1298
        :param size: The size of the batches.  Required if forwards is False.
 
1299
        :param memo: Batch identifier.
 
1300
        :param orderby: The way to order the batch.
 
1301
        :param forwards: If true, walk forwards from the memo.  Else walk
 
1302
            backwards.
 
1303
 
 
1304
        """
 
1305
        query_vars = {}
 
1306
        if size is not None:
 
1307
            query_vars['batch'] = size
 
1308
        if memo is not None:
 
1309
            query_vars['memo'] = memo
 
1310
            if forwards:
 
1311
                query_vars['start'] = memo
 
1312
            else:
 
1313
                query_vars['start'] = int(memo) - size
 
1314
        if not forwards:
 
1315
            query_vars['direction'] = 'backwards'
 
1316
        query_string = urllib.urlencode(query_vars)
 
1317
        request = LaunchpadTestRequest(
 
1318
            QUERY_STRING=query_string, orderby=orderby)
1297
1319
        if bugtask is None:
1298
1320
            bugtask = self.factory.makeBugTask()
1299
1321
        view = BugTaskSearchListingView(bugtask.target, request)
1302
1324
 
1303
1325
    @contextmanager
1304
1326
    def dynamic_listings(self):
 
1327
        """Context manager to enable new bug listings."""
1305
1328
        with feature_flags():
1306
1329
            set_feature_flag(u'bugs.dynamic_bug_listings.enabled', u'on')
1307
1330
            yield
1327
1350
        self.assertEqual(1, len(bugtasks))
1328
1351
        self.assertEqual(item.model, bugtasks[0])
1329
1352
 
 
1353
    def test_no_next_prev_for_single_batch(self):
 
1354
        """The IJSONRequestCache should contain data about ajacent batches.
 
1355
 
 
1356
        mustache_model should contain bugtasks, the BugTaskListingItem.model
 
1357
        for each BugTask.
 
1358
        """
 
1359
        owner, item = make_bug_task_listing_item(self.factory)
 
1360
        self.useContext(person_logged_in(owner))
 
1361
        with self.dynamic_listings():
 
1362
            view = self.makeView(item.bugtask)
 
1363
        cache = IJSONRequestCache(view.request)
 
1364
        self.assertIs(None, cache.objects.get('next'))
 
1365
        self.assertIs(None, cache.objects.get('prev'))
 
1366
 
 
1367
    def test_next_for_multiple_batch(self):
 
1368
        """The IJSONRequestCache should contain data about the next batch.
 
1369
 
 
1370
        mustache_model should contain bugtasks, the BugTaskListingItem.model
 
1371
        for each BugTask.
 
1372
        """
 
1373
        task = self.factory.makeBugTask()
 
1374
        self.factory.makeBugTask(target=task.target)
 
1375
        with self.dynamic_listings():
 
1376
            view = self.makeView(task, size=1)
 
1377
        cache = IJSONRequestCache(view.request)
 
1378
        self.assertEqual({'memo': '1', 'start': 1}, cache.objects.get('next'))
 
1379
 
 
1380
    def test_prev_for_multiple_batch(self):
 
1381
        """The IJSONRequestCache should contain data about the next batch.
 
1382
 
 
1383
        mustache_model should contain bugtasks, the BugTaskListingItem.model
 
1384
        for each BugTask.
 
1385
        """
 
1386
        task = self.factory.makeBugTask()
 
1387
        task2 = self.factory.makeBugTask(target=task.target)
 
1388
        with self.dynamic_listings():
 
1389
            view = self.makeView(task2, size=1, memo=1)
 
1390
        cache = IJSONRequestCache(view.request)
 
1391
        self.assertEqual({'memo': '1', 'start': 0}, cache.objects.get('prev'))
 
1392
 
 
1393
    def test_default_order_by(self):
 
1394
        """order_by defaults to '-importance in JSONRequestCache"""
 
1395
        task = self.factory.makeBugTask()
 
1396
        with self.dynamic_listings():
 
1397
            view = self.makeView(task)
 
1398
        cache = IJSONRequestCache(view.request)
 
1399
        self.assertEqual('-importance', cache.objects['order_by'])
 
1400
 
 
1401
    def test_order_by_importance(self):
 
1402
        """order_by follows query params in JSONRequestCache"""
 
1403
        task = self.factory.makeBugTask()
 
1404
        with self.dynamic_listings():
 
1405
            view = self.makeView(task, orderby='importance')
 
1406
        cache = IJSONRequestCache(view.request)
 
1407
        self.assertEqual('importance', cache.objects['order_by'])
 
1408
 
 
1409
    def test_cache_has_all_batch_vars_defaults(self):
 
1410
        """Cache has all the needed variables.
 
1411
 
 
1412
        order_by, memo, start, forwards.  These default to sane values.
 
1413
        """
 
1414
        task = self.factory.makeBugTask()
 
1415
        with self.dynamic_listings():
 
1416
            view = self.makeView(task)
 
1417
        cache = IJSONRequestCache(view.request)
 
1418
        self.assertEqual('-importance', cache.objects['order_by'])
 
1419
        self.assertIs(None, cache.objects['memo'])
 
1420
        self.assertEqual(0, cache.objects['start'])
 
1421
        self.assertTrue(cache.objects['forwards'])
 
1422
 
 
1423
    def test_cache_has_all_batch_vars_specified(self):
 
1424
        """Cache has all the needed variables.
 
1425
 
 
1426
        order_by, memo, start, forwards.  These are calculated appropriately.
 
1427
        """
 
1428
        task = self.factory.makeBugTask()
 
1429
        with self.dynamic_listings():
 
1430
            view = self.makeView(task, memo=1, forwards=False, size=1)
 
1431
        cache = IJSONRequestCache(view.request)
 
1432
        self.assertEqual('1', cache.objects['memo'])
 
1433
        self.assertEqual(0, cache.objects['start'])
 
1434
        self.assertFalse(cache.objects['forwards'])
 
1435
        self.assertEqual(0, cache.objects['last_start'])
 
1436
 
1330
1437
    def getBugtaskBrowser(self):
 
1438
        """Return a browser for a new bugtask."""
1331
1439
        bugtask = self.factory.makeBugTask()
1332
1440
        with person_logged_in(bugtask.target.owner):
1333
1441
            bugtask.target.official_malone = True
1336
1444
        return bugtask, browser
1337
1445
 
1338
1446
    def assertHTML(self, browser, *tags, **kwargs):
 
1447
        """Assert something about a browser's HTML."""
1339
1448
        matcher = soupmatchers.HTMLContains(*tags)
1340
1449
        if kwargs.get('invert', False):
1341
1450
            matcher = Not(matcher)
1353
1462
        number_tag = self.getBugNumberTag(bug_task)
1354
1463
        self.assertHTML(browser, number_tag, invert=True)
1355
1464
        self.assertHTML(browser, self.client_listing, invert=True)
1356
 
        self.assertHTML(browser, self.server_listing, invert=True)
1357
1465
 
1358
1466
    def test_mustache_rendering(self):
1359
1467
        """If the flag is present, then all mustache features appear."""
1360
1468
        with self.dynamic_listings():
1361
1469
            bug_task, browser = self.getBugtaskBrowser()
1362
1470
        bug_number = self.getBugNumberTag(bug_task)
1363
 
        self.assertHTML(
1364
 
            browser, self.client_listing, self.server_listing, bug_number)
 
1471
        self.assertHTML(browser, self.client_listing, bug_number)
1365
1472
 
1366
1473
 
1367
1474
class TestBugTaskListingItem(TestCaseWithFactory):