Create system check for MAX_DUCK_LEVEL setting

This commit is contained in:
Gergely Polonkai 2015-10-21 15:56:12 +02:00
parent 7eb89d2f4c
commit ba163fe943
3 changed files with 83 additions and 10 deletions

View File

@ -0,0 +1,5 @@
"""
Booking module of the Duck Booking Tool
"""
default_app_config = 'booking.apps.BookingConfig'

48
booking/apps.py Normal file
View File

@ -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'

View File

@ -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
)