From 19e66de32c3df35a86596e8b946d531d145ad8a2 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 19 Jan 2015 15:36:50 +0100 Subject: [PATCH] Add duck competence level method (DuckCompetence.level) --- booking/models.py | 6 ++++++ booking/tests.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/booking/models.py b/booking/models.py index 5501e0f..a42ce45 100644 --- a/booking/models.py +++ b/booking/models.py @@ -1,6 +1,9 @@ from django.db import models from django.contrib.auth.models import User from django.utils import timezone +from django.conf import settings + +from .ducklevel import minutes_to_level class Species(models.Model): """Model to hold the Ducks’ species""" @@ -85,6 +88,9 @@ class DuckCompetence(models.Model): up_minutes = models.IntegerField(default = 0) down_minutes = models.IntegerField(default = 0) + def level(self): + return minutes_to_level(self.up_minutes, self.down_minutes) + class Meta: unique_together = ('duck', 'comp') diff --git a/booking/tests.py b/booking/tests.py index 7051f57..18119f2 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -6,7 +6,7 @@ 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 +from .models import Duck, Competence, DuckCompetence class FrontTest(TestCase): def setUp(self): @@ -25,6 +25,15 @@ class FrontTest(TestCase): self.assertEqual(response.status_code, 200) class DuckCompLevelTest(TestCase): + def setUp(self): + duck = Duck() + comp = Competence(name = 'testing') + self.duckcomp = DuckCompetence( + duck = duck, + comp = comp, + up_minutes = 0, + down_minutes =0) + def test_sane_max(self): self.assertGreater( settings.MAX_DUCK_LEVEL, 0, @@ -80,6 +89,24 @@ class DuckCompLevelTest(TestCase): self.assertEqual(level_to_down_minutes(4), 200000) self.assertEqual(level_to_down_minutes(5), 2000000) + def test_no_comp(self): + self.duckcomp.up_minutes = 0 + self.duckcomp.down_minutes = 0 + self.assertEquals(self.duckcomp.level(), 0) + + def test_comp_levels(self): + self.duckcomp.down_minutes = 0 + + for lvl in range(1, settings.MAX_DUCK_LEVEL): + minutes = level_to_up_minutes(lvl) + self.duckcomp.up_minutes = minutes + self.assertEqual(self.duckcomp.level(), lvl) + + def test_high_minutes(self): + self.duckcomp.up_minutes = level_to_up_minutes(settings.MAX_DUCK_LEVEL) + self.duckcomp.down_minutes = level_to_down_minutes(settings.MAX_DUCK_LEVEL) + self.assertEqual(self.duckcomp.level(), settings.MAX_DUCK_LEVEL) + class DuckAgeTest(TestCase): def test_duck_is_from_the_future(self): future_duck = Duck(donated_at = timezone.now() + datetime.timedelta(days = 2))