lundi 4 mai 2015

Unit test on custom django admin template

No clue how to deal with this situation. Recently start 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 which can verify 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 automaticaly load 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 how about when you want to proove that work's with a unit test? I'm able to test template when i can use a view and the function render_to_string 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 don't figure it out how to call the admin view with the associate template part. Here a start of what i found to add unit test on 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 ?

Thank you all.

Aucun commentaire:

Enregistrer un commentaire