Files
Django_TasksApp/main/planner/test_health_page.py
T
2026-10-02 15:48:06 +02:00

67 lines
3.7 KiB
Python

from datetime import date
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from .models import BowelHealthCheckIn, NeckHeadacheCheckIn, SleepCheckIn
class HealthPageTests(TestCase):
def setUp(self):
User = get_user_model()
self.user = User.objects.create_user(username='health-page-user', password='password')
self.other = User.objects.create_user(username='other-health-page-user', password='password')
self.day = date(2026, 10, 2)
@patch('planner.views.localdate_for_user', return_value=date(2026, 10, 2))
def test_page_reuses_both_panels_and_keeps_records_private(self, _):
url = reverse('planner:health')
self.assertEqual(url, '/health/')
self.assertEqual(self.client.get(url).status_code, 302)
NeckHeadacheCheckIn.objects.create(user=self.user, check_in_date=self.day,
morning_neck_pain_severity=1, morning_headache_severity=2, morning_note='My neck note')
SleepCheckIn.objects.create(user=self.user, wake_up_date=self.day, rested_score=7, note='My sleep note')
BowelHealthCheckIn.objects.create(user=self.user, check_in_date=self.day,
morning_severity=3, morning_note='My bowel note')
NeckHeadacheCheckIn.objects.create(user=self.other, check_in_date=self.day,
morning_neck_pain_severity=9, morning_headache_severity=9, morning_note='Other neck note')
BowelHealthCheckIn.objects.create(user=self.other, check_in_date=self.day,
morning_severity=9, morning_note='Other bowel note')
self.client.force_login(self.user)
response = self.client.get(url, {'wellbeing_tab': 'sleep', 'check_in_date': self.day.isoformat(),
'bowel_date': self.day.isoformat()})
self.assertContains(response, 'id="neck-headache-checkin"')
self.assertContains(response, 'id="bowel-health-checkin"')
self.assertContains(response, 'My bowel note')
self.assertContains(response, 'Sleep scores over time')
self.assertNotContains(response, 'Other neck note')
self.assertNotContains(response, 'Other bowel note')
self.assertContains(response, 'href="/health/?check_in_date=')
dashboard = self.client.get(reverse('planner:task-list'))
self.assertContains(dashboard, 'href="/health/"')
self.assertNotContains(dashboard, 'id="neck-headache-checkin"')
self.assertNotContains(dashboard, 'id="bowel-health-checkin"')
@patch('planner.views.localdate_for_user', return_value=date(2026, 10, 2))
@patch('planner.forms.localdate_for_user', return_value=date(2026, 10, 2))
def test_non_htmx_saves_return_to_health(self, _form_today, _view_today):
self.client.force_login(self.user)
pain = self.client.post(reverse('planner:neck-headache-checkin-save'), {
'check_in_date': '2026-10-02', 'morning_neck_pain_severity': '1',
'morning_headache_severity': '2',
})
self.assertEqual(pain.status_code, 302)
self.assertTrue(pain['Location'].startswith('/health/?check_in_date=2026-10-02'))
sleep = self.client.post(reverse('planner:sleep-checkin-save'), {
'wake_up_date': '2026-10-02', 'rested_score': '7',
})
self.assertEqual(sleep.status_code, 302)
self.assertIn('wellbeing_tab=sleep', sleep['Location'])
bowel = self.client.post(reverse('planner:bowel-health-save'), {
'check_in_date': '2026-10-02', 'morning_severity': '3',
})
self.assertEqual(bowel.status_code, 302)
self.assertTrue(bowel['Location'].startswith('/health/?bowel_date=2026-10-02'))