Use a base class for API tests

This commit is contained in:
Christopher C. Wells 2020-03-13 19:24:35 -07:00 committed by Christopher Charbonneau Wells
parent 0fbb4d92fc
commit 0c086e6469
1 changed files with 45 additions and 153 deletions

View File

@ -7,13 +7,36 @@ from rest_framework.test import APITestCase
from core import models from core import models
class ChildAPITestCase(APITestCase): class TestBase:
class BabyBuddyAPITestCaseBase(APITestCase):
fixtures = ['tests.json'] fixtures = ['tests.json']
endpoint = reverse('api:child-list') model = None
endpoint = None
delete_id = 1
def setUp(self): def setUp(self):
self.client.login(username='admin', password='admin') 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)
class ChildAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
endpoint = reverse('api:child-list')
model = models.Child
delete_id = 'fake-child'
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -26,11 +49,6 @@ class ChildAPITestCase(APITestCase):
'picture': None 'picture': None
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Child List')
def test_post(self): def test_post(self):
data = { data = {
'first_name': 'Test', 'first_name': 'Test',
@ -57,20 +75,11 @@ class ChildAPITestCase(APITestCase):
entry['slug'] = 'new-name' entry['slug'] = 'new-name'
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 'fake-child')
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)
class DiaperChangeAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class DiaperChangeAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:diaperchange-list') endpoint = reverse('api:diaperchange-list')
model = models.DiaperChange
def setUp(self): delete_id = 3
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -85,11 +94,6 @@ class DiaperChangeAPITestCase(APITestCase):
'amount': 2.25 'amount': 2.25
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Diaper Change List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -120,20 +124,10 @@ class DiaperChangeAPITestCase(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
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)
class FeedingAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class FeedingAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:feeding-list') endpoint = reverse('api:feeding-list')
model = models.Feeding
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -149,11 +143,6 @@ class FeedingAPITestCase(APITestCase):
'amount': 2.5 'amount': 2.5
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Feeding List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -182,20 +171,10 @@ class FeedingAPITestCase(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
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)
class NoteAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class NoteAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:note-list') endpoint = reverse('api:note-list')
model = models.Note
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -207,11 +186,6 @@ class NoteAPITestCase(APITestCase):
'time': '2017-11-17T22:45:00-05:00' 'time': '2017-11-17T22:45:00-05:00'
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Note List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -236,20 +210,10 @@ class NoteAPITestCase(APITestCase):
# new value. # new value.
self.assertEqual(response.data['note'], entry['note']) self.assertEqual(response.data['note'], entry['note'])
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
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)
class SleepAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class SleepAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:sleep-list') endpoint = reverse('api:sleep-list')
model = models.Sleep
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -263,11 +227,6 @@ class SleepAPITestCase(APITestCase):
'nap': False 'nap': False
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Sleep List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -292,20 +251,10 @@ class SleepAPITestCase(APITestCase):
# the new value. # the new value.
self.assertEqual(response.data['end'], entry['end']) self.assertEqual(response.data['end'], entry['end'])
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 4)
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)
class TemperatureAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class TemperatureAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:temperature-list') endpoint = reverse('api:temperature-list')
model = models.Temperature
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -317,11 +266,6 @@ class TemperatureAPITestCase(APITestCase):
'time': '2017-11-17T12:52:00-05:00' 'time': '2017-11-17T12:52:00-05:00'
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Temperature List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -344,20 +288,10 @@ class TemperatureAPITestCase(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
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)
class TimerAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class TimerAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:timer-list') endpoint = reverse('api:timer-list')
model = models.Timer
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -373,11 +307,6 @@ class TimerAPITestCase(APITestCase):
'user': 1 'user': 1
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Timer List')
def test_post(self): def test_post(self):
data = { data = {
'name': 'New fake timer', 'name': 'New fake timer',
@ -399,20 +328,10 @@ class TimerAPITestCase(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 1)
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)
class TummyTimeAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class TummyTimeAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:tummytime-list') endpoint = reverse('api:tummytime-list')
model = models.TummyTime
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -426,11 +345,6 @@ class TummyTimeAPITestCase(APITestCase):
'milestone': '' 'milestone': ''
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Tummy Time List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -454,20 +368,10 @@ class TummyTimeAPITestCase(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 3)
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)
class WeightAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
class WeightAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:weight-list') endpoint = reverse('api:weight-list')
model = models.Weight
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self): def test_get(self):
response = self.client.get(self.endpoint) response = self.client.get(self.endpoint)
@ -479,11 +383,6 @@ class WeightAPITestCase(APITestCase):
'date': '2017-11-18' 'date': '2017-11-18'
}) })
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Weight List')
def test_post(self): def test_post(self):
data = { data = {
'child': 1, 'child': 1,
@ -505,10 +404,3 @@ class WeightAPITestCase(APITestCase):
}) })
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry) self.assertEqual(response.data, entry)
def test_delete(self):
endpoint = '{}{}/'.format(self.endpoint, 2)
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)