~launchpad-pqm/launchpad/devel

9087.4.9 by Guilherme Salgado
Fix a couple existing tests and tweak my implementation to not break others
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
__metaclass__ = type
5
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
6
from lp.services.webapp.publisher import canonical_url
14612.2.1 by William Grant
format-imports on lib/. So many imports.
7
from lp.testing import TestCaseWithFactory
14604.1.1 by Curtis Hovey
Separate test-authoring classes from test-running classes.
8
from lp.testing.layers import DatabaseFunctionalLayer
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
9
from lp.testing.publication import test_traverse
10762.3.3 by Tim Penhey
Some breadcrumb test framework.
10
from lp.testing.views import create_initialized_view
9087.4.9 by Guilherme Salgado
Fix a couple existing tests and tweak my implementation to not break others
11
12
13
class BaseBreadcrumbTestCase(TestCaseWithFactory):
14
15
    layer = DatabaseFunctionalLayer
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
16
17
    def assertBreadcrumbs(self, expected, obj, view_name=None, rootsite=None):
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
18
        """Assert that the breadcrumbs for obj match the expected values.
19
20
        :param expected: A list of tuples containing (text, url) pairs.
21
        """
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
22
        crumbs = self.getBreadcrumbsForObject(obj, view_name, rootsite)
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
23
        self.assertEqual(
24
            expected,
25
            [(crumb.text, crumb.url) for crumb in crumbs])
26
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
27
    def assertBreadcrumbTexts(self, expected, obj, view_name=None,
28
                              rootsite=None):
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
29
        """The text of the breadcrumbs for obj match the expected values."""
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
30
        crumbs = self.getBreadcrumbsForObject(obj, view_name, rootsite)
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
31
        self.assertEqual(expected, [crumb.text for crumb in crumbs])
32
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
33
    def assertBreadcrumbUrls(self, expected, obj, view_name=None,
34
                             rootsite=None):
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
35
        """The urls of the breadcrumbs for obj match the expected values."""
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
36
        crumbs = self.getBreadcrumbsForObject(obj, view_name, rootsite)
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
37
        self.assertEqual(expected, [crumb.url for crumb in crumbs])
38
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
39
    def getBreadcrumbsForObject(self, obj, view_name=None, rootsite=None):
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
40
        """Get the breadcrumbs for the specified object.
41
42
        Traverse to the canonical_url of the object, and use the request from
43
        that to feed into the initialized hierarchy view so we get the
44
        traversed objects.
45
        """
10762.3.13 by Tim Penhey
Fix the tests for the method renames.
46
        url = canonical_url(obj, view_name=view_name, rootsite=rootsite)
47
        return self.getBreadcrumbsForUrl(url)
48
49
    def getBreadcrumbsForUrl(self, url):
10762.3.5 by Tim Penhey
Move the breadcrumb testcase.
50
        obj, view, request = test_traverse(url)
51
        view = create_initialized_view(obj, '+hierarchy', request=request)
10762.3.3 by Tim Penhey
Some breadcrumb test framework.
52
        return view.items