Unit test on custom django admin template

Not sure how to handle this situation. Recently started a unit test with django. In my project I have a custom change_list.html to add a button on the admin page.

I'm looking for a unit test that can test this custom button. Here is my code in admin.py:

class LocationAdmin(OSMGeoAdmin):

    def get_urls(self):
        urls = patterns('', url(
            r'^import/$',
            self.admin_site.admin_view(self.import_process)
        ))

        super_urls = super(LocationAdmin, self).get_urls()

        return urls + super_urls

    def import_process(self, request):
        pass

admin.site.register(Location, LocationAdmin)

      

This code automatically loads the template in app / admin / app / app / change_list.html Which is:

{% extends "admin/change_list.html" %}
{% load staticfiles %}
{% block extrahead %}
    <link rel="stylesheet" href="{% static "css/custom_upload.css" %}">
{% endblock %}
{% block object-tools-items %}
    {{ block.super }}
    <li><a role="button" href="import/" class="btn btn-primary btn-margin-left">Import</a></li>

{% endblock %}

      

But what about when you want to create this work with a unit test? I can test the template when I can use the view and render_to_string function like this:

response = my_view(HttpRequest())
expected_html = render_to_string(
    'template.html',
    {'arg_key': arg}
)
self.assertEqual(response.content.decode(), expected_html)

      

But here I am not understanding how to call the admin view using the linked part of the template. Here is the start of what I found to add a unit test on the admin page.

class MockRequest(object):
    pass


class MockSuperUser(object):

    def has_perm(self, perm):
        return True

request = MockRequest()
request.user = MockSuperUser()


class ModelAdminTests(TestCase):

    def setUp(self):
        self.site = AdminSite()

    def test_admin_import_button_on_location_admin_page(self):
        ma = ModelAdmin(Location, self.site)
        #Here the correct assert ?

      

Thanks everyone.

+3


source to share


1 answer


If you've used Pytest with pytest-django (which you obviously don't use, and I don't have enough reputation to comment), you are using the admin_client attribute as described: http://pytest-django.readthedocs.org/en/latest/helpers .html # admin-client-django-test-client-logged-in-as-admin



0


source







All Articles