mybuddy/api/tests.py

407 lines
14 KiB
Python
Raw Normal View History

2017-12-07 19:09:56 +00:00
# -*- coding: utf-8 -*-
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from core import models
2020-03-14 02:24:35 +00:00
class TestBase:
class BabyBuddyAPITestCaseBase(APITestCase):
fixtures = ['tests.json']
model = None
endpoint = None
delete_id = 1
def setUp(self):
self.client.login(username='admin', password='admin')
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], '{} List'.format(
self.model._meta.verbose_name))
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, self.delete_id)
response = self.client.get(endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response = self.client.delete(endpoint)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
2017-12-07 19:09:56 +00:00
2020-03-14 02:24:35 +00:00
class ChildAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
endpoint = reverse('api:child-list')
model = models.Child
delete_id = 'fake-child'
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 1,
'first_name': 'Fake',
'last_name': 'Child',
'birth_date': '2017-11-11',
'slug': 'fake-child',
'picture': None
2017-12-07 19:09:56 +00:00
})
def test_post(self):
data = {
'first_name': 'Test',
'last_name': 'Child',
'birth_date': '2017-11-12'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Child.objects.get(pk=response.data['id'])
self.assertEqual(obj.first_name, data['first_name'])
2020-03-13 13:29:14 +00:00
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 'fake-child')
response = self.client.get(endpoint)
entry = response.data
entry['first_name'] = 'New'
entry['last_name'] = 'Name'
response = self.client.patch(endpoint, {
'first_name': entry['first_name'],
'last_name': entry['last_name'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
# The slug we be updated by the name change.
entry['slug'] = 'new-name'
self.assertEqual(response.data, entry)
2020-03-14 02:24:35 +00:00
class DiaperChangeAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:diaperchange-list')
2020-03-14 02:24:35 +00:00
model = models.DiaperChange
delete_id = 3
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 3,
'child': 1,
'time': '2017-11-18T14:00:00-05:00',
'wet': True,
'solid': False,
'color': '',
'amount': 2.25
2017-12-07 19:09:56 +00:00
})
def test_post(self):
data = {
'child': 1,
'time': '2017-11-18T12:00:00-05:00',
'wet': True,
'solid': True,
'color': 'brown',
'amount': 1.25
2017-12-07 19:09:56 +00:00
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.DiaperChange.objects.get(pk=response.data['id'])
self.assertTrue(obj.wet)
self.assertTrue(obj.solid)
2017-12-07 19:09:56 +00:00
self.assertEqual(obj.color, data['color'])
self.assertEqual(obj.amount, data['amount'])
2017-12-07 19:09:56 +00:00
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
response = self.client.get(endpoint)
entry = response.data
entry['wet'] = False
entry['solid'] = True
response = self.client.patch(endpoint, {
'wet': entry['wet'],
'solid': entry['solid'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
2017-12-07 19:09:56 +00:00
2020-03-14 02:24:35 +00:00
class FeedingAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:feeding-list')
2020-03-14 02:24:35 +00:00
model = models.Feeding
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 3,
'child': 1,
'start': '2017-11-18T14:00:00-05:00',
'end': '2017-11-18T14:15:00-05:00',
'duration': '00:15:00',
'type': 'formula',
'method': 'bottle',
'amount': 2.5
})
def test_post(self):
data = {
'child': 1,
'start': '2017-11-19T14:00:00-05:00',
'end': '2017-11-19T14:15:00-05:00',
'type': 'breast milk',
'method': 'left breast'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Feeding.objects.get(pk=response.data['id'])
self.assertEqual(obj.type, data['type'])
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
response = self.client.get(endpoint)
entry = response.data
entry['type'] = 'breast milk'
entry['method'] = 'left breast'
entry['amount'] = 0
response = self.client.patch(endpoint, {
'type': entry['type'],
'method': entry['method'],
'amount': entry['amount'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
2017-12-07 19:09:56 +00:00
2020-03-14 02:24:35 +00:00
class NoteAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:note-list')
2020-03-14 02:24:35 +00:00
model = models.Note
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 1,
'child': 1,
'note': 'Fake note.',
'time': '2017-11-17T22:45:00-05:00'
})
def test_post(self):
data = {
'child': 1,
'note': 'New fake note.',
'time': '2017-11-18T22:45:00-05:00'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Note.objects.get(pk=response.data['id'])
self.assertEqual(obj.note, data['note'])
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
response = self.client.get(endpoint)
entry = response.data
entry['note'] = 'Updated note text.'
response = self.client.patch(endpoint, {
'note': entry['note'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
# The time of entry will always update automatically, so only check the
# new value.
self.assertEqual(response.data['note'], entry['note'])
2017-12-07 19:09:56 +00:00
2020-03-14 02:24:35 +00:00
class SleepAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:sleep-list')
2020-03-14 02:24:35 +00:00
model = models.Sleep
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 4,
'child': 1,
'start': '2017-11-18T19:00:00-05:00',
'end': '2017-11-18T23:00:00-05:00',
'duration': '04:00:00',
'nap': False
2017-12-07 19:09:56 +00:00
})
def test_post(self):
data = {
'child': 1,
'start': '2017-11-21T19:30:00-05:00',
'end': '2017-11-21T23:00:00-05:00',
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Sleep.objects.get(pk=response.data['id'])
self.assertEqual(str(obj.duration), '3:30:00')
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 4)
response = self.client.get(endpoint)
entry = response.data
entry['end'] = '2017-11-18T23:30:00-05:00'
response = self.client.patch(endpoint, {
'end': entry['end'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
# The duration of entry will always update automatically, so only check
# the new value.
self.assertEqual(response.data['end'], entry['end'])
2020-03-14 02:24:35 +00:00
class TemperatureAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2019-05-17 05:13:14 +00:00
endpoint = reverse('api:temperature-list')
2020-03-14 02:24:35 +00:00
model = models.Temperature
2019-05-17 05:13:14 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 1,
'child': 1,
'temperature': 98.6,
'time': '2017-11-17T12:52:00-05:00'
})
def test_post(self):
data = {
'child': 1,
'temperature': '100.1',
'time': '2017-11-20T22:52:00-05:00'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Temperature.objects.get(pk=response.data['id'])
self.assertEqual(str(obj.temperature), data['temperature'])
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
response = self.client.get(endpoint)
entry = response.data
entry['temperature'] = 99
response = self.client.patch(endpoint, {
'temperature': entry['temperature'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
2019-05-17 05:13:14 +00:00
2020-03-14 02:24:35 +00:00
class TimerAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:timer-list')
2020-03-14 02:24:35 +00:00
model = models.Timer
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 1,
'child': None,
2017-12-07 19:09:56 +00:00
'name': 'Fake timer',
'start': '2017-11-17T23:30:00-05:00',
'end': '2017-11-18T00:30:00-05:00',
'duration': '01:00:00',
'active': False,
'user': 1
})
def test_post(self):
data = {
'name': 'New fake timer',
'user': 1
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Timer.objects.get(pk=response.data['id'])
self.assertEqual(obj.name, data['name'])
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
response = self.client.get(endpoint)
entry = response.data
entry['name'] = 'New Timer Name'
response = self.client.patch(endpoint, {
'name': entry['name'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
2017-12-07 19:09:56 +00:00
2020-03-14 02:24:35 +00:00
class TummyTimeAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:tummytime-list')
2020-03-14 02:24:35 +00:00
model = models.TummyTime
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 3,
'child': 1,
'start': '2017-11-18T15:30:00-05:00',
'end': '2017-11-18T15:30:45-05:00',
'duration': '00:00:45',
'milestone': ''
})
def test_post(self):
data = {
'child': 1,
'start': '2017-11-18T12:30:00-05:00',
'end': '2017-11-18T12:35:30-05:00',
'milestone': 'Rolled over.'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.TummyTime.objects.get(pk=response.data['id'])
self.assertEqual(str(obj.duration), '0:05:30')
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
response = self.client.get(endpoint)
entry = response.data
entry['milestone'] = 'Switched sides!'
response = self.client.patch(endpoint, {
'milestone': entry['milestone'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
2020-03-14 02:24:35 +00:00
class WeightAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
2017-12-07 19:09:56 +00:00
endpoint = reverse('api:weight-list')
2020-03-14 02:24:35 +00:00
model = models.Weight
2017-12-07 19:09:56 +00:00
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 2,
'child': 1,
'weight': 9.5,
'date': '2017-11-18'
})
def test_post(self):
data = {
'child': 1,
'weight': '9.75',
'date': '2017-11-20'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Weight.objects.get(pk=response.data['id'])
self.assertEqual(str(obj.weight), data['weight'])
def test_patch(self):
endpoint = '{}{}/'.format(self.endpoint, 2)
response = self.client.get(endpoint)
entry = response.data
entry['weight'] = 8.25
response = self.client.patch(endpoint, {
'weight': entry['weight'],
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)