Add specific, fixtures-based tests for Dashboard cards.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-19 11:13:35 -05:00
parent 5a57ac7a48
commit 452ce190ae
3 changed files with 475 additions and 0 deletions

View File

@ -1,5 +1,7 @@
[run] [run]
omit = omit =
*/migrations/*
babybuddy/management/*
babybuddy/settings/* babybuddy/settings/*
babybuddy/wsgi.py babybuddy/wsgi.py
source = source =

View File

@ -0,0 +1,336 @@
[
{
"model": "core.child",
"pk": 1,
"fields":
{
"first_name": "Fake",
"last_name": "Child",
"birth_date": "2017-11-11",
"slug": "fake-child",
"picture": ""
}
},
{
"model": "core.diaperchange",
"pk": 2,
"fields":
{
"child": 1,
"time": "2017-11-18T16:00:00Z",
"wet": true,
"solid": true,
"color": "yellow"
}
},
{
"model": "core.diaperchange",
"pk": 3,
"fields":
{
"child": 1,
"time": "2017-11-18T19:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 4,
"fields":
{
"child": 1,
"time": "2017-11-17T13:00:00Z",
"wet": true,
"solid": true,
"color": "green"
}
},
{
"model": "core.diaperchange",
"pk": 5,
"fields":
{
"child": 1,
"time": "2017-11-17T16:00:00Z",
"wet": false,
"solid": true,
"color": "green"
}
},
{
"model": "core.diaperchange",
"pk": 6,
"fields":
{
"child": 1,
"time": "2017-11-16T15:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 7,
"fields":
{
"child": 1,
"time": "2017-11-16T18:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 8,
"fields":
{
"child": 1,
"time": "2017-11-15T15:00:00Z",
"wet": false,
"solid": true,
"color": "brown"
}
},
{
"model": "core.diaperchange",
"pk": 9,
"fields":
{
"child": 1,
"time": "2017-11-15T13:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 10,
"fields":
{
"child": 1,
"time": "2017-11-15T16:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 11,
"fields":
{
"child": 1,
"time": "2017-11-15T21:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 12,
"fields":
{
"child": 1,
"time": "2017-11-14T15:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 13,
"fields":
{
"child": 1,
"time": "2017-11-13T14:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 14,
"fields":
{
"child": 1,
"time": "2017-11-13T18:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 15,
"fields":
{
"child": 1,
"time": "2017-11-12T15:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.diaperchange",
"pk": 16,
"fields":
{
"child": 1,
"time": "2017-11-11T15:00:00Z",
"wet": true,
"solid": false,
"color": ""
}
},
{
"model": "core.feeding",
"pk": 1,
"fields":
{
"child": 1,
"start": "2017-11-18T14:00:00Z",
"end": "2017-11-18T14:30:00Z",
"duration": "00:30:00",
"type": "breast milk",
"method": "left breast",
"amount": null
}
},
{
"model": "core.feeding",
"pk": 2,
"fields":
{
"child": 1,
"start": "2017-11-18T16:30:00Z",
"end": "2017-11-18T17:00:00Z",
"duration": "00:30:00",
"type": "breast milk",
"method": "right breast",
"amount": null
}
},
{
"model": "core.feeding",
"pk": 3,
"fields":
{
"child": 1,
"start": "2017-11-18T19:00:00Z",
"end": "2017-11-18T19:15:00Z",
"duration": "00:15:00",
"type": "formula",
"method": "bottle",
"amount": 2.5
}
},
{
"model": "core.sleep",
"pk": 1,
"fields":
{
"child": 1,
"start": "2017-11-18T04:30:00Z",
"end": "2017-11-18T05:30:00Z",
"duration": "01:00:00"
}
},
{
"model": "core.sleep",
"pk": 2,
"fields":
{
"child": 1,
"start": "2017-11-18T15:00:00Z",
"end": "2017-11-18T17:00:00Z",
"duration": "02:00:00"
}
},
{
"model": "core.sleep",
"pk": 3,
"fields":
{
"child": 1,
"start": "2017-11-18T19:00:00Z",
"end": "2017-11-18T19:30:00Z",
"duration": "00:30:00"
}
},
{
"model": "core.sleep",
"pk": 4,
"fields":
{
"child": 1,
"start": "2017-11-19T00:00:00Z",
"end": "2017-11-19T04:00:00Z",
"duration": "04:00:00"
}
},
{
"model": "core.tummytime",
"pk": 1,
"fields":
{
"child": 1,
"start": "2017-11-18T15:00:00Z",
"end": "2017-11-18T15:03:00Z",
"duration": "00:03:00",
"milestone": ""
}
},
{
"model": "core.tummytime",
"pk": 2,
"fields":
{
"child": 1,
"start": "2017-11-18T17:15:30Z",
"end": "2017-11-18T17:16:45Z",
"duration": "00:01:15",
"milestone": ""
}
},
{
"model": "core.tummytime",
"pk": 3,
"fields":
{
"child": 1,
"start": "2017-11-18T20:30:00Z",
"end": "2017-11-18T20:30:45Z",
"duration": "00:00:45",
"milestone": ""
}
},
{
"model": "core.weight",
"pk": 1,
"fields":
{
"child": 1,
"weight": 8.5,
"date": "2017-11-11"
}
},
{
"model": "core.weight",
"pk": 2,
"fields":
{
"child": 1,
"weight": 9.5,
"date": "2017-11-18"
}
}
]

View File

@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.test import TestCase
from django.utils import timezone
from core import models
from dashboard.templatetags import cards
class TemplateTagsTestCase(TestCase):
fixtures = ['tests_templatetags.json']
@classmethod
def setUpClass(cls):
super(TemplateTagsTestCase, cls).setUpClass()
cls.child = models.Child.objects.first()
# Test file data uses a basis date of 2017-11-18.
date = timezone.localtime().strptime('2017-11-18', '%Y-%m-%d')
cls.date = timezone.make_aware(date)
def test_card_diaperchange_last(self):
data = cards.card_diaperchange_last(self.child)
self.assertEqual(data['type'], 'diaperchange')
self.assertIsInstance(data['change'], models.DiaperChange)
self.assertEqual(data['change'], models.DiaperChange.objects.first())
def test_card_diaperchange_types(self):
data = cards.card_diaperchange_types(self.child, self.date)
self.assertEqual(data['type'], 'diaperchange')
stats = {
0: {'wet_pct': 66.66666666666666, 'solid_pct': 33.33333333333333,
'solid': 1, 'wet': 2},
1: {'wet_pct': 33.33333333333333, 'solid_pct': 66.66666666666666,
'solid': 2, 'wet': 1},
2: {'wet_pct': 100.0, 'solid_pct': 0.0, 'solid': 0, 'wet': 2},
3: {'wet_pct': 75.0, 'solid_pct': 25.0, 'solid': 1, 'wet': 3},
4: {'wet_pct': 100.0, 'solid_pct': 0.0, 'solid': 0, 'wet': 1},
5: {'wet_pct': 100.0, 'solid_pct': 0.0, 'solid': 0, 'wet': 2},
6: {'wet_pct': 100.0, 'solid_pct': 0.0, 'solid': 0, 'wet': 1}
}
self.assertEqual(data['stats'], stats)
def test_card_feeding_last(self):
data = cards.card_feeding_last(self.child)
self.assertEqual(data['type'], 'feeding')
self.assertIsInstance(data['feeding'], models.Feeding)
self.assertEqual(data['feeding'], models.Feeding.objects.first())
def test_card_feeding_last_method(self):
data = cards.card_feeding_last(self.child)
self.assertEqual(data['type'], 'feeding')
self.assertIsInstance(data['feeding'], models.Feeding)
self.assertEqual(
data['feeding'].method,
models.Feeding.objects.first().method)
def test_card_sleep_last(self):
data = cards.card_sleep_last(self.child)
self.assertEqual(data['type'], 'sleep')
self.assertIsInstance(data['sleep'], models.Sleep)
self.assertEqual(data['sleep'], models.Sleep.objects.first())
def test_card_sleep_day(self):
data = cards.card_sleep_day(self.child, self.date)
self.assertEqual(data['type'], 'sleep')
self.assertEqual(data['total'], timezone.timedelta(2, 7200))
self.assertEqual(data['count'], 4)
def test_card_sleep_naps_day(self):
data = cards.card_sleep_naps_day(self.child, self.date)
cards.card_sleep_naps_day(self.child)
self.assertEqual(data['type'], 'sleep')
self.assertEqual(data['total'], timezone.timedelta(0, 9000))
self.assertEqual(data['count'], 2)
def test_card_statistics(self):
data = cards.card_statistics(self.child)
stats = [
{
'title': 'Diaper change frequency',
'stat': timezone.timedelta(0, 44228, 571429),
'type': 'duration'
},
{
'title': 'Feeding frequency',
'stat': timezone.timedelta(0, 7200),
'type': 'duration'
},
{
'title': 'Average nap duration',
'stat': timezone.timedelta(0, 4500),
'type': 'duration'
},
{
'title': 'Average naps per day',
'stat': 2.0,
'type': 'float'
},
{
'title': 'Average sleep duration',
'stat': timezone.timedelta(0, 6750),
'type': 'duration'
},
{
'title': 'Average awake duration',
'stat': timezone.timedelta(0, 19200),
'type': 'duration'
},
{
'title': 'Weight change per week',
'stat': 1.0, 'type':
'float'
}
]
self.assertEqual(data['stats'], stats)
def test_card_timer_list(self):
models.Timer(user=User.objects.first()).save()
data = cards.card_timer_list()
self.assertIsInstance(data['instances'][0], models.Timer)
self.assertEqual(data['instances'][0], models.Timer.objects.first())
def test_card_tummytime_last(self):
data = cards.card_tummytime_last(self.child)
self.assertEqual(data['type'], 'tummytime')
self.assertIsInstance(data['tummytime'], models.TummyTime)
self.assertEqual(data['tummytime'], models.TummyTime.objects.first())
def test_card_tummytime_day(self):
data = cards.card_tummytime_day(self.child, self.date)
self.assertEqual(data['type'], 'tummytime')
self.assertIsInstance(data['instances'].first(), models.TummyTime)
self.assertIsInstance(data['last'], models.TummyTime)
stats = {'count': 3, 'total': timezone.timedelta(0, 300)}
self.assertEqual(data['stats'], stats)