Repository: dotkom/onlineweb4

Issue:
OnlineUser.year tests
We really should write some tests to verify that the year attribute actually produces a correct value.

Make sure to include:
- Any date in August should produce a correct value for year. Especially important if "now" is august
- Masters degrees are registered differently in the user object now, many field_of_study entries will be eligible for 4th and 5th year.
- What happens to a user that started in january?



Discussion:

Resolving patch:
diff --git a/apps/authentication/models.py b/apps/authentication/models.py
--- a/apps/authentication/models.py
+++ b/apps/authentication/models.py
@@ -99,7 +99,7 @@ def year(self):
             if year > 3:
                 return 3
             return year
-        elif 9 < self.field_of_study < 30:  # 10-29 is considered master
+        elif 10 <= self.field_of_study <= 30:  # 10-29 is considered master
             if year >= 2:
                 return 5
             return 4
@@ -109,6 +109,9 @@ def year(self):
             if year == 1:
                 return 1
             return 4
+        # If user's field of study is not matched by any of these tests, return -1
+        else:
+            return -1
 
     def __unicode__(self):
         return self.get_full_name()


Test patch:
diff --git a/apps/authentication/tests.py b/apps/authentication/tests.py
--- a/apps/authentication/tests.py
+++ b/apps/authentication/tests.py
@@ -1,24 +1,91 @@
 import logging
 
-from datetime import datetime, timedelta
+from datetime import timedelta
 
 from django_dynamic_fixture import G
-from django.utils import timezone
 from django.conf import settings
 from django.test import TestCase
+from django.utils import timezone
 
-from apps.authentication.models import RegisterToken
+from apps.authentication.models import RegisterToken, OnlineUser
 
 class AuthenticationTest(TestCase):
     def setUp(self):
         self.logger = logging.getLogger(__name__)
+        self.user = G(OnlineUser, username='testuser')
+        self.now = timezone.now()
 
     def testTokenActive(self):
         self.logger.debug("Testing that the token is active, with dynamic fixtures")
-        self.registertoken=G(RegisterToken, created=timezone.now())
+        self.registertoken=G(RegisterToken, created=self.now)
         self.assertTrue(self.registertoken.is_valid)
 
     def testTokenNotActive(self):
         self.logger.debug("Testing that the token is not active, with dynamic fixtures")
-        self.registertoken=G(RegisterToken, created=timezone.now() - timedelta(days=1))
+        self.registertoken=G(RegisterToken, created=self.now - timedelta(days=1))
         self.assertFalse(self.registertoken.is_valid)
+
+    def testYearZeroIfNoFieldOfStudy(self):
+        self.user.started_date = self.now.date()
+        self.assertEqual(0, self.user.year)
+
+    def testYearOneBachelor(self):
+        self.user.started_date = self.now.date()
+        self.user.field_of_study = 1
+        self.assertEqual(1, self.user.year)
+
+    def testYearTwoBachelor(self):
+        self.user.started_date = self.now.date() - timedelta(days=365)
+        self.user.field_of_study = 1
+        self.assertEqual(2, self.user.year)
+
+    def testYearThreeBachelor(self):
+        self.user.started_date = self.now.date() - timedelta(days=365*2)
+        self.user.field_of_study = 1
+        self.assertEqual(3, self.user.year)
+
+    def testYearFourBachelorShouldNotBePossible(self):
+        self.user.started_date = self.now.date() - timedelta(days=365*3)
+        self.user.field_of_study = 1
+        self.assertEqual(3, self.user.year)
+
+    def testYearFourMaster(self):
+        self.user.started_date = self.now.date()
+        self.user.field_of_study = 10
+        self.assertEqual(4, self.user.year)
+
+    def testYearFiveMaster(self):
+        self.user.started_date = self.now.date() - timedelta(days=365)
+        self.user.field_of_study = 10
+        self.assertEqual(5, self.user.year)
+
+    def testYearSixMasterShouldNotBePossible(self):
+        self.user.started_date = self.now.date() - timedelta(days=365*2)
+        self.user.field_of_study = 10
+        self.assertEqual(5, self.user.year)
+
+    def testFieldOfStudy30IsAlsoMaster(self):
+        self.user.started_date = self.now.date()
+        self.user.field_of_study = 30
+        self.assertEqual(4, self.user.year)
+
+    def testPhD(self):
+        self.user.started_date = self.now.date()
+        self.user.field_of_study = 80
+        self.assertEqual(6, self.user.year)
+
+    def testPhdYearCouldBeInfinite(self):
+        self.user.started_date = self.now.date() - timedelta(days=365*5)
+        self.user.field_of_study = 80
+        self.assertEqual(11, self.user.year)
+
+    def testInternational(self):
+        self.user.started_date = self.now.date()
+        self.user.field_of_study = 90
+        self.assertEqual(1, self.user.year)
+
+    def testInternational(self):
+        self.user.started_date = self.now.date() - timedelta(days=365)
+        self.user.field_of_study = 90
+        self.assertEqual(4, self.user.year)
+