0

In one of my templates for detailed view of a device assignment I had logic pertaining to if buttons should appear depending on the user's permission. I wrote tests for these buttons and they work as expected.

Today I wanted to reuse these buttons elsewhere in another template so I extracted it into its own template file and put it in a "partials" folder.

|-templates
  |  |-assignments
  |  |  |-deviceassignment_form.html
  |  |  |-deviceassignment_detail.html
  |  |  |-deviceassignment_confirm_delete.html
  |  |  |-deviceassignment_list.html
  |  |  |-partials
  |  |  |  |-deviceassignment_control_buttons.html
  |  |  |  |-deviceassignment_infobox.html

Obviously I can continue testing deviceassignment_detail.html which includes the control buttons and checks to make sure they work (appearing or not based on permissions). But then when I include control buttons elsewhere I might be tempted to test them again...

Instead couldn't I write a test ONLY for the deviceassignment_control_buttons.html template? I want to render the template directly with a basic view context and check that everything works as expected. Is this possible?

Bil1
  • 440
  • 2
  • 18

1 Answers1

0

I finally found a video that explained it. Here is an answer for anyone coming across this in the future. I'm using Beautiful Soup to easily check that certain elements in the links I'm searching for really exist. I'm creating a context to match what permissions would be available to the template.

from django.test import SimpleTestCase
from django.template import Context, Template
from bs4 import BeautifulSoup
import copy

list_link_selector = 'a[href="/assignments/"]'
update_link_selector = 'a[href="/assignments/1/edit/"]'
delete_link_selector = 'a[href="/assignments/1/delete/"]'

default_context = Context(
    {
        "deviceassignment": {"id": 1},
        "perms": {
            "assignments": {
                "view_deviceassignment": True,
                "delete_deviceassignment": True,
                "change_deviceassignment": True,
            }
        },
    }
)

default_template = Template(
    "{% include  'assignments/partials/deviceassignment_control_buttons.html'%}"
)


class DeviceAssignmentControlButtonsSuperuserTest(SimpleTestCase):
    def test_all_links_exist(self):
        context = copy.deepcopy(default_context)
        template = copy.deepcopy(default_template)
        rendered = template.render(context)
        soup = BeautifulSoup(rendered, "html.parser")

        list_link = soup.select(list_link_selector)
        update_link = soup.select(update_link_selector)
        delete_link = soup.select(delete_link_selector)

        self.assertEqual(len(list_link), 1)
        self.assertEqual(len(update_link), 1)
        self.assertEqual(len(delete_link), 1)

        self.assertEqual(list_link[0].contents[0], "Assignment List")
        self.assertEqual(update_link[0].contents[0], "Edit Assignment")
        self.assertEqual(delete_link[0].contents[0], "Delete Assignment")
Bil1
  • 440
  • 2
  • 18