added lxml & cssselect to req'ments; this allowed more precise targeting of html attrs in apps.dashboard.tests'

This commit is contained in:
Disco DeDisco
2026-01-03 21:12:28 -05:00
parent 08305532c2
commit e3e753369f
2 changed files with 11 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
from django.test import TestCase from django.test import TestCase
from .models import Item, List from .models import Item, List
import lxml.html
class HomePageTest(TestCase): class HomePageTest(TestCase):
def test_uses_home_template(self): def test_uses_home_template(self):
@@ -8,12 +9,11 @@ class HomePageTest(TestCase):
def test_renders_input_form(self): def test_renders_input_form(self):
response = self.client.get('/') response = self.client.get('/')
self.assertContains(response, '<form method="POST" action="/apps/dashboard/newlist">') parsed = lxml.html.fromstring(response.content)
self.assertContains( [form] = parsed.cssselect('form[method=POST]')
response, self.assertEqual(form.get('action'), '/apps/dashboard/newlist')
'<input name="item_text" id="id-new-item" placeholder="Enter a to-do item" />', inputs = form.cssselect('input')
html=True, self.assertIn('item_text', [input.get('name') for input in inputs])
)
class ListAndItemModelsTest(TestCase): class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self): def test_saving_and_retrieving_items(self):
@@ -52,15 +52,11 @@ class DashViewTest(TestCase):
def test_renders_input_form(self): def test_renders_input_form(self):
mylist = List.objects.create() mylist = List.objects.create()
response = self.client.get(f'/apps/dashboard/{mylist.id}/') response = self.client.get(f'/apps/dashboard/{mylist.id}/')
self.assertContains( parsed = lxml.html.fromstring(response.content)
response, [form] = parsed.cssselect('form[method=POST]')
f'<form method="POST" action="/apps/dashboard/{mylist.id}/add-item">', self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/add-item")
) inputs = form.cssselect('input')
self.assertContains( self.assertIn('item_text', [input.get('name') for input in inputs])
response,
'<input name="item_text" id="id-new-item" placeholder="Enter a to-do item" />',
html=True,
)
def test_displays_only_items_for_that_list(self): def test_displays_only_items_for_that_list(self):
# Given/Arrange # Given/Arrange

Binary file not shown.