reverse() doesn't work in menu.CustomMenu during unit testing

Issue #27 resolved
Mark Cahill
created an issue

When creating a custom menu via manage.py, the first child will fail on "reverse('admin:index')". Further experimentation showed that any reference to reverse() causes this child to fail. Removing reverse altogether caused all unit tests to pass.

The reverse() works fine when actually using the application. No exceptions are thrown.

An example of a failed test is attached.

Comments (9)

  1. Mark Cahill reporter

    Any ideas why it only happens during unit testing? If it's a problem with Django's unit test framework, I'd be more than happy to submit an upstream bug.

  2. Alex Robbins
    • changed status to open

    Mark,

    I was just working through some unit test issues myself and ran into this. If you look, you'll notice that the problem is happening during the auth unit tests. The reason is that these unit tests specify a urls.py, which doesn't include the admin. Unfortunately, the menu is instantiated and reverse is called during these tests.

    I fixed it in my custom menu by doing this in the init method:

    try:
        admin_url = reverse("admin:index")
    except NoReverseException:
        admin_url = None
    if admin_url is not None:
        self.add_child(forget_the_exact_code)
    

    (The problem shows up because the menu shows on the change password page, which inherits the admin template.)

    Maybe we should add that code into the default menu.py?

  3. Mark Cahill reporter

    The auth unit tests use a custom urls.py? So we can safely skip entries with this Exception? If that's the case, I'll wrap the entire menu creation in a try block, and return an empty menu if there's an error.

  4. Mark Cahill reporter

    I added this patch to the menu.py and it allowed the unit tests to pass:

    from django.core.urlresolvers import NoReverseMatch
    ...
            try:
                self.children.append(MenuItem(
                    title=_('Dashboard'),
                    url=reverse("admin:index")
                ))
            except NoReverseMatch:
                pass
    
  5. Alex Robbins

    Glad to hear that fixed it for you. There was also a unit test failure inside admin_tools that was reversing a url in the bookmark form template. I submitted a fix for that, in case you come across that. It was raising a TemplateSyntaxError in the unit tests for the same reason: custom urls.py.

    Alex

  6. Log in to comment