Fix 500 in sleep pattern (#487)

This commit is contained in:
Alpha Chen 2022-06-17 06:40:28 -07:00 committed by GitHub
parent a84087edf3
commit c98f6cd433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View File

@ -26,7 +26,9 @@ def sleep_pattern(sleeps):
last_end_time = None
adjustment = None
days = _init_days(sleeps.first().start, sleeps.last().end)
first_day = timezone.localtime(sleeps.first().start)
last_day = timezone.localtime(sleeps.last().end)
days = _init_days(first_day, last_day)
for sleep in sleeps:
start_time = timezone.localtime(sleep.start)

View File

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
import datetime as dt
from django.test import TestCase
from django.utils import timezone
from core import models
from reports.graphs import sleep_pattern
class SleepPatternTestCase(TestCase):
def setUp(self):
self.original_tz = timezone.get_current_timezone()
self.tz = dt.timezone(dt.timedelta(days=-1, hours=1))
timezone.activate(self.tz)
def tearDown(self):
timezone.activate(self.original_tz)
def test_sleep_pattern(self):
c = models.Child(birth_date=dt.datetime.now())
c.save()
models.Sleep.objects.create(
child=c,
start=dt.datetime(2000, 1, 1, 0, 0, tzinfo=timezone.utc),
end=dt.datetime(2000, 1, 1, 0, 1, tzinfo=timezone.utc),
)
sleep_pattern(models.Sleep.objects.order_by("start"))