← Back to team overview

launchpad-dev team mailing list archive

SOLVED: Re: HELP NEEDED: test traversal function

 

On Sat, 01 May 2010 04:25:12 Jonathan Lange wrote:
> On Tue, Apr 27, 2010 at 3:41 AM, Tim Penhey <tim.penhey@xxxxxxxxxxxxx> 
wrote:
> > Hi All,
> > 
> > In a desire to make breadcrumbs easier to test, and to provide a useful
> > test function, I've been attempting to write a function that takes a
> > canonical_url of an object, and returns the object that was traversed to
> > and a request object.
>
> You've probably already solved the problem though. If you have, please
> share how!

Hi All,

Yes as Jono guessed, I have solved the problem now.  The browser traversal 
adapters were not loaded in the 'make harness' so something that should have 
worked didn't.  The harness code has now been updated, so this function now 
works there too.

OK, so here is what we have:

from lp.testing.publication import test_traverse


def test_traverse(url):
    """Traverse the url in the same way normal publishing occurs.

    Returns a tuple of (object, view, request) where:
      object is the last model object in the traversal chain
      view is the defined view for the object at the specified url (if
        the url didn't directly specify a view, then the view is the
        default view for the object.
      request is the request object resulting from the traversal.  This
        contains a populated traversed_objects list just as a browser
        request would from a normal call into the app servers.

    This call uses the currently logged in user, and does not start a new
    transaction.
    """

This new code is used in the base breadcrumb test case, which as part of my 
branch I moved to lp.testing.breadcrumb.

BaseBreadcrumbTestCase now has several (IMO) cool methods.

here is an example from the translations test_breadcrumbs (which I hacked the 
guts out of):

class TestTranslationsVHostBreadcrumb(BaseBreadcrumbTestCase):

    def test_product(self):
        product = self.factory.makeProduct(
            name='crumb-tester', displayname="Crumb Tester")
        self.assertBreadcrumbs(
            [("Crumb Tester", 'http://launchpad.dev/crumb-tester'),
             ("Translations", 'http://translations.launchpad.dev/crumb-
tester')],
            product, rootsite='translations')

How easy is that?  There is also:

    def getBreadcrumbsForObject(self, obj, view_name=None, rootsite=None):
        """Get the breadcrumbs for the specified object.

        Traverse to the canonical_url of the object, and use the request from
        that to feed into the initialized hierarchy view so we get the
        traversed objects.
        """

and:

    def getBreadcrumbsForUrl(self, url):

Please use and enhance as necessary.

Tim



References