From 0d7382407cf3af03ff7b82aee1dddfecb73e393d Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Mon, 30 Oct 2017 20:45:46 -0400 Subject: [PATCH] Refactor some utils functions sligthly and add tests. --- api/tests/__init__.py | 0 api/tests/tests_utils.py | 35 +++++++++++++++++++++++++++++++++++ api/utils.py | 15 +++++++++++++++ api/views.py | 2 +- core/tests/tests_utils.py | 23 +++++++++++++++++++++++ core/utils.py | 11 ----------- 6 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 api/tests/__init__.py create mode 100644 api/tests/tests_utils.py create mode 100644 api/utils.py create mode 100644 core/tests/tests_utils.py diff --git a/api/tests/__init__.py b/api/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/api/tests/tests_utils.py b/api/tests/tests_utils.py new file mode 100644 index 00000000..7a30aadd --- /dev/null +++ b/api/tests/tests_utils.py @@ -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')) diff --git a/api/utils.py b/api/utils.py new file mode 100644 index 00000000..775b35cb --- /dev/null +++ b/api/utils.py @@ -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 diff --git a/api/views.py b/api/views.py index e437086f..10d87fda 100644 --- a/api/views.py +++ b/api/views.py @@ -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, diff --git a/core/tests/tests_utils.py b/core/tests/tests_utils.py new file mode 100644 index 00000000..0b3fa98c --- /dev/null +++ b/core/tests/tests_utils.py @@ -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')) diff --git a/core/utils.py b/core/utils.py index 2efd7abf..3239e8db 100644 --- a/core/utils.py +++ b/core/utils.py @@ -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