Refactor some utils functions sligthly and add tests.

This commit is contained in:
Christopher Charbonneau Wells 2017-10-30 20:45:46 -04:00
parent 003e112d27
commit 0d7382407c
6 changed files with 74 additions and 12 deletions

0
api/tests/__init__.py Normal file
View File

35
api/tests/tests_utils.py Normal file
View File

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from django.utils import timezone
from rest_framework.views import APIView
from rest_framework.test import APIRequestFactory
from api.utils import filter_by_params
from core.models import Child
class UtilsTestCase(TestCase):
def test_filter_by_params(self):
factory = APIRequestFactory()
Child.objects.create(
first_name='First',
last_name='Child',
birth_date=timezone.localdate())
Child.objects.create(
first_name='Second',
last_name='Child',
birth_date=timezone.localdate())
request = factory.get('/children/')
request = APIView().initialize_request(request)
response = filter_by_params(request, Child, [])
self.assertTrue(response, Child.objects.all())
request = factory.get('/children/', {'first_name': 'First'})
request = APIView().initialize_request(request)
response = filter_by_params(request, Child, ['first_name'])
self.assertTrue(response, Child.objects.filter(first_name='First'))

15
api/utils.py Normal file
View File

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
def filter_by_params(request, model, available_params):
"""Filters all instances of a model based on request parameters.
"""
queryset = model.objects.all()
for param in available_params:
value = request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset

View File

@ -3,9 +3,9 @@ from __future__ import unicode_literals
from rest_framework import viewsets
from api.utils import filter_by_params
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
from core.utils import filter_by_params
from .serializers import (ChildSerializer, DiaperChangeSerializer,
FeedingSerializer, NoteSerializer, SleepSerializer,

23
core/tests/tests_utils.py Normal file
View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from django.utils import timezone
from core.utils import duration_string, duration_parts
class UtilsTestCase(TestCase):
def test_duration_string(self):
duration = timezone.timedelta(hours=1, minutes=30, seconds=45)
self.assertEqual(
duration_string(duration),
'1 hour, 30 minutes, 45 seconds')
self.assertEqual(duration_string(duration, 'm'), '1 hour, 30 minutes')
self.assertEqual(duration_string(duration, 'h'), '1 hour')
self.assertRaises(TypeError, lambda: duration_string('1 hour'))
def test_duration_parts(self):
duration = timezone.timedelta(hours=1, minutes=30, seconds=45)
self.assertEqual(duration_parts(duration), (1, 30, 45))
self.assertRaises(TypeError, lambda: duration_parts('1 hour'))

View File

@ -4,17 +4,6 @@ from __future__ import unicode_literals
from django.utils import timezone
def filter_by_params(request, model, available_params):
queryset = model.objects.all()
for param in available_params:
value = request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
def duration_string(duration, precision='s'):
"""Format hours, minutes and seconds as a human-friendly string (e.g. "2
hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or