From ba163fe943b5456883dff90b60ce3c1446230e8b Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 21 Oct 2015 15:56:12 +0200 Subject: [PATCH] Create system check for MAX_DUCK_LEVEL setting --- booking/__init__.py | 5 +++++ booking/apps.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ booking/tests.py | 40 +++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 booking/apps.py diff --git a/booking/__init__.py b/booking/__init__.py index e69de29..41279d5 100644 --- a/booking/__init__.py +++ b/booking/__init__.py @@ -0,0 +1,5 @@ +""" +Booking module of the Duck Booking Tool +""" + +default_app_config = 'booking.apps.BookingConfig' diff --git a/booking/apps.py b/booking/apps.py new file mode 100644 index 0000000..9b4e553 --- /dev/null +++ b/booking/apps.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 +""" +App config for the booking module of the Duck Booking Tool. This module +is currently needed for the MAX_DUCK_LEVEL system check. +""" + +from django.apps import AppConfig +from django.conf import settings +from django.core.checks import Error, register +from django.core.exceptions import ImproperlyConfigured + +@register() +def max_level_check(app_configs, **kwargs): + """ + System check to see if MAX_DUCK_LEVEL has a sane value (non-zero, + positive integer) + """ + + errors = [] + + if not hasattr(settings, 'MAX_DUCK_LEVEL'): + errors.append( + Error( + 'MAX_DUCK_LEVEL is not set!', + id='booking.E001' + ) + ) + else: + if settings.MAX_DUCK_LEVEL <= 0: + errors.append( + Error( + 'MAX_DUCK_LEVEL should be greater than zero!', + id='booking.E002' + ) + ) + + if not isinstance(settings.MAX_DUCK_LEVEL, int): + errors.append( + Error( + 'MAX_DUCK_LEVEL must be an integer!', + id='booking.E003' + ) + ) + + return errors + +class BookingConfig(AppConfig): + name = 'booking' diff --git a/booking/tests.py b/booking/tests.py index 8d6f4b0..e9a7f47 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -7,13 +7,14 @@ import datetime from django.conf import settings from django.contrib.auth.models import User -from django.test import TestCase, Client +from django.test import TestCase, Client, override_settings from django.utils import timezone from .ducklevel import level_to_up_minutes, level_to_down_minutes, minutes_to_level from .templatetags import booking_tags from .models import Duck, Competence, DuckCompetence, Species, \ Location, Booking, DuckName, DuckNameVote +from .apps import max_level_check class FrontTest(TestCase): """ @@ -79,15 +80,6 @@ class DuckCompLevelTest(TestCase): up_minutes=0, down_minutes=0) - def test_sane_max(self): - """ - Test if the MAX_DUCK_LEVEL setting has a sane value - """ - - self.assertGreater( - settings.MAX_DUCK_LEVEL, 0, - msg="MAX_DUCK_LEVEL must be greater than zero!") - def test_max_minutes(self): """ Test if level can not go above settings.MAX_DUCK_LEVEL) @@ -491,3 +483,31 @@ class StrTest(TestCase): self.assertEquals("First Duck booked by test for Testing since {0}".format(start), booking.__str__()) + +class SystemCheckTest(TestCase): + @override_settings() + def test_max_duck_level_missing(self): + del settings.MAX_DUCK_LEVEL + + errors = max_level_check(None) + + self.assertGreater( + len([e for e in errors if e.id == 'booking.E001']), + 0) + + def test_max_duck_level_illegal(self): + with self.settings(MAX_DUCK_LEVEL=0): + errors = max_level_check(None) + + self.assertGreater( + len([e for e in errors if e.id == 'booking.E002']), + 0 + ) + + with self.settings(MAX_DUCK_LEVEL=1.1): + errors = max_level_check(None) + + self.assertGreater( + len([e for e in errors if e.id == 'booking.E003']), + 0 + )