Commits

Kevin Wetzels committed d9733ac

Add tests and fix some issues

Comments (0)

Files changed (3)

 from __future__ import division
 import re
 
-from django.forms import IntegerField
+from django import forms
 from django.utils.encoding import smart_str
 
 from widgets import MinutesWidget
 
-class MinutesField(IntegerField):
+class MinutesField(forms.IntegerField):
     
     widget = MinutesWidget
     
                 try:
                     hours = int(parts[0])
                     fraction = int(parts[1])
+                    if not fraction in (5, 25, 50, 75):
+                        raise forms.ValidationError(self.error_messages['invalid'])
+                    if fraction == 5:
+                        fraction = 50
                     value = int((hours * 60) + (60 / 100 * fraction))
                 except (ValueError, TypeError):
                     raise forms.ValidationError(self.error_messages['invalid'])
+# -*- coding: utf-8 -*-
 """
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
+Test cases for MinutesField and MinutesWidget.
+"""
+from django.test import TestCase
+from django.forms import ValidationError
 
-Replace this with more appropriate tests for your application.
-"""
+from stdfields.forms import MinutesField
+from stdfields.widgets import MinutesWidget
 
-from django.test import TestCase
+class MinutesFieldTest(TestCase):
+    
+    def test_minutes_field(self):
+        f = MinutesField()
+        self.assertEqual(121, f.clean(121))
+        self.assertEqual(121, f.clean('2:01'))
+        self.assertEqual(121, f.clean('2:1'))
+        self.assertEqual(150, f.clean('2.5'))
+        self.assertEqual(150, f.clean('2,5'))
+        self.assertEqual(150, f.clean('2.50'))
+        self.assertEqual(150, f.clean('2.50.120'))
+        self.assertEqual(135, f.clean('2.25'))
+        self.assertEqual(165, f.clean('2.75'))
+        minutes = 480
+        for i in range(60):
+            self.assertEqual(minutes + i, f.clean('8:%d' % (i)))
+        
+    def test_minutes_field_invalid(self):
+        f = MinutesField()
+        self._should_raise_validation_error(f, '2:60')
+        self._should_raise_validation_error(f, 'x:60')
+        self._should_raise_validation_error(f, 'x:y')
+        self._should_raise_validation_error(f, '2;30')
+        self._should_raise_validation_error(f, '2;30')
+        
+    def _should_raise_validation_error(self, f, value):
+        try:
+            f.clean(value)
+            self.fail('%s should raise a ValidationError' % (value))
+        except ValidationError:
+            pass
 
 
-class SimpleTest(TestCase):
-    def test_basic_addition(self):
-        """
-        Tests that 1 + 1 always equals 2.
-        """
-        self.assertEqual(1 + 1, 2)
+class MinutesWidgetTest(TestCase):
+    
+    def test_minutes_widget(self):
+        w = MinutesWidget()
+        self.assertEqual(w.render('hi', '121'), '<input type="text" name="hi" value="2:01" />')
+        self.assertEqual(w.render('hi', '2:1'), '<input type="text" name="hi" value="2:1" />')
+        self.assertEqual(w.render('hi', '2:60'), '<input type="text" name="hi" value="2:60" />')
+        self.assertEqual(w.render('hi', '493'), '<input type="text" name="hi" value="8:13" />')
 def format_minutes(value):
     if value is None or value == '':
         return u''
+    try:
+        value = int(value)
+    except ValueError:
+        return value
     divided = MinutesWidget.divide(value)
     return u'%d:%02d' % divided
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.