From b114fbfc075c40866fd4bd29636aeb67969769a6 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 17 Dec 2014 11:08:13 +0100 Subject: [PATCH] Add booking.models.Duck.age() with tests --- booking/models.py | 9 +++++++++ booking/tests.py | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/booking/models.py b/booking/models.py index 42f7e45..5501e0f 100644 --- a/booking/models.py +++ b/booking/models.py @@ -50,6 +50,15 @@ class Duck(models.Model): return self.name + def age(self): + seconds_d = timezone.now() - self.donated_at + seconds = seconds_d.total_seconds() + + if seconds < 0: + return -1 + + return seconds + class DuckName(models.Model): """Model to hold name suggestions for Ducks""" diff --git a/booking/tests.py b/booking/tests.py index cb08550..ca8c133 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -1,4 +1,9 @@ from django.test import TestCase, Client +from django.utils import timezone + +import datetime + +from .models import Duck class FrontTest(TestCase): def setUp(self): @@ -15,3 +20,8 @@ class FrontTest(TestCase): def test_disclaimer_page(self): response = self.client.get('/disclaimer.html') self.assertEqual(response.status_code, 200) + +class DuckAgeTest(TestCase): + def test_duck_is_from_the_future(self): + future_duck = Duck(donated_at = timezone.now() + datetime.timedelta(days = 2)) + self.assertEqual(future_duck.age(), -1)