From 49a20c20b0290208c42edc50a9828ec4b0a30a05 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 16 Jan 2015 15:38:59 +0100 Subject: [PATCH] Add ducklevel.py to convert between level and minutes --- booking/ducklevel.py | 20 +++++++++++++++ booking/tests.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ duckbook/settings.py | 2 ++ 3 files changed, 80 insertions(+) create mode 100644 booking/ducklevel.py diff --git a/booking/ducklevel.py b/booking/ducklevel.py new file mode 100644 index 0000000..8485523 --- /dev/null +++ b/booking/ducklevel.py @@ -0,0 +1,20 @@ +from django.conf import settings +import math + +def level_to_up_minutes(level): + if level == 0: + return 0 + + return 2 * pow(10, level) + +def level_to_down_minutes(level): + if level == 0: + return 0 + + return 20 * pow(10, level) + +def minutes_to_level(up_minutes, down_minutes): + minutes = up_minutes + down_minutes / 10 + level = 0 if minutes <= 0 else min(settings.MAX_DUCK_LEVEL, math.floor(math.log10(minutes))) + + return level diff --git a/booking/tests.py b/booking/tests.py index 2301569..7051f57 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -1,8 +1,10 @@ from django.test import TestCase, Client from django.utils import timezone +from django.conf import settings import datetime +from .ducklevel import level_to_up_minutes, level_to_down_minutes, minutes_to_level from .templatetags import booking_tags from .models import Duck @@ -22,6 +24,62 @@ class FrontTest(TestCase): response = self.client.get('/disclaimer.html') self.assertEqual(response.status_code, 200) +class DuckCompLevelTest(TestCase): + def test_sane_max(self): + 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)""" + + max_up_minutes = level_to_up_minutes(settings.MAX_DUCK_LEVEL) + double_minutes = level_to_up_minutes(settings.MAX_DUCK_LEVEL * 2) + max_down_minutes = level_to_down_minutes(settings.MAX_DUCK_LEVEL) + + level = minutes_to_level(max_up_minutes, 0) + self.assertEqual(level, settings.MAX_DUCK_LEVEL) + + level = minutes_to_level(max_up_minutes + 1, 0) + self.assertEqual(level, settings.MAX_DUCK_LEVEL) + + level = minutes_to_level(double_minutes, 0) + self.assertEqual(level, settings.MAX_DUCK_LEVEL) + + level = minutes_to_level(0, max_down_minutes) + self.assertEqual(level, settings.MAX_DUCK_LEVEL) + + level = minutes_to_level(0, max_down_minutes + 1) + self.assertEqual(level, settings.MAX_DUCK_LEVEL) + + def test_conversions(self): + for i in range(1, settings.MAX_DUCK_LEVEL): + up_minutes = level_to_up_minutes(i) + down_minutes = level_to_down_minutes(i) + + up_level = minutes_to_level(up_minutes, 0) + down_level = minutes_to_level(0, down_minutes) + + self.assertEqual(up_level, i, msg = "Test failed for value %d" % i) + self.assertEqual( + down_level, i, + msg = "Test failed for value %d" % i) + + def test_level_to_minutes(self): + self.assertEqual(level_to_up_minutes(0), 0) + self.assertEqual(level_to_up_minutes(1), 20) + self.assertEqual(level_to_up_minutes(2), 200) + self.assertEqual(level_to_up_minutes(3), 2000) + self.assertEqual(level_to_up_minutes(4), 20000) + self.assertEqual(level_to_up_minutes(5), 200000) + + self.assertEqual(level_to_down_minutes(0), 0) + self.assertEqual(level_to_down_minutes(1), 200) + self.assertEqual(level_to_down_minutes(2), 2000) + self.assertEqual(level_to_down_minutes(3), 20000) + self.assertEqual(level_to_down_minutes(4), 200000) + self.assertEqual(level_to_down_minutes(5), 2000000) + class DuckAgeTest(TestCase): def test_duck_is_from_the_future(self): future_duck = Duck(donated_at = timezone.now() + datetime.timedelta(days = 2)) diff --git a/duckbook/settings.py b/duckbook/settings.py index b9f6da1..2bc2007 100644 --- a/duckbook/settings.py +++ b/duckbook/settings.py @@ -82,3 +82,5 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.7/howto/static-files/ STATIC_URL = '/static/' + +MAX_DUCK_LEVEL = 5