Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15838 -- Promoted assertFieldOutput to a general test utility.…

… Thanks to Ramiro Morales for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16653 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2664fa189614a4dbbfed5cb683148707ab568f47 1 parent 46ef298
Russell Keith-Magee authored August 23, 2011

Showing 47 changed files with 167 additions and 161 deletions. Show diff stats Hide diff stats

  1. 58  django/test/testcases.py
  2. 21  docs/topics/testing.txt
  3. 12  tests/regressiontests/forms/localflavor/__init__.py
  4. 4  tests/regressiontests/forms/localflavor/ar.py
  5. 4  tests/regressiontests/forms/localflavor/at.py
  6. 4  tests/regressiontests/forms/localflavor/au.py
  7. 4  tests/regressiontests/forms/localflavor/be.py
  8. 4  tests/regressiontests/forms/localflavor/br.py
  9. 4  tests/regressiontests/forms/localflavor/ca.py
  10. 4  tests/regressiontests/forms/localflavor/ch.py
  11. 4  tests/regressiontests/forms/localflavor/cl.py
  12. 4  tests/regressiontests/forms/localflavor/cn.py
  13. 4  tests/regressiontests/forms/localflavor/co.py
  14. 4  tests/regressiontests/forms/localflavor/cz.py
  15. 4  tests/regressiontests/forms/localflavor/de.py
  16. 4  tests/regressiontests/forms/localflavor/ec.py
  17. 4  tests/regressiontests/forms/localflavor/es.py
  18. 4  tests/regressiontests/forms/localflavor/fi.py
  19. 4  tests/regressiontests/forms/localflavor/fr.py
  20. 4  tests/regressiontests/forms/localflavor/gb.py
  21. 4  tests/regressiontests/forms/localflavor/generic.py
  22. 4  tests/regressiontests/forms/localflavor/hr.py
  23. 4  tests/regressiontests/forms/localflavor/id.py
  24. 4  tests/regressiontests/forms/localflavor/ie.py
  25. 4  tests/regressiontests/forms/localflavor/il.py
  26. 4  tests/regressiontests/forms/localflavor/in_.py
  27. 4  tests/regressiontests/forms/localflavor/is_.py
  28. 4  tests/regressiontests/forms/localflavor/it.py
  29. 4  tests/regressiontests/forms/localflavor/jp.py
  30. 4  tests/regressiontests/forms/localflavor/kw.py
  31. 4  tests/regressiontests/forms/localflavor/mk.py
  32. 4  tests/regressiontests/forms/localflavor/mx.py
  33. 4  tests/regressiontests/forms/localflavor/nl.py
  34. 4  tests/regressiontests/forms/localflavor/pl.py
  35. 4  tests/regressiontests/forms/localflavor/pt.py
  36. 4  tests/regressiontests/forms/localflavor/py.py
  37. 4  tests/regressiontests/forms/localflavor/ro.py
  38. 4  tests/regressiontests/forms/localflavor/ru.py
  39. 4  tests/regressiontests/forms/localflavor/se.py
  40. 4  tests/regressiontests/forms/localflavor/sk.py
  41. 4  tests/regressiontests/forms/localflavor/us.py
  42. 64  tests/regressiontests/forms/localflavor/utils.py
  43. 4  tests/regressiontests/forms/localflavor/uy.py
  44. 4  tests/regressiontests/forms/localflavor/za.py
  45. 1  tests/regressiontests/forms/localflavortests.py
  46. 1  tests/regressiontests/forms/tests/__init__.py
  47. 11  tests/regressiontests/test_utils/tests.py
58  django/test/testcases.py
@@ -8,11 +8,14 @@
8 8
 
9 9
 from django.conf import settings
10 10
 from django.core import mail
  11
+from django.core.exceptions import ValidationError
11 12
 from django.core.management import call_command
12 13
 from django.core.signals import request_started
13 14
 from django.core.urlresolvers import clear_url_caches
  15
+from django.core.validators import EMPTY_VALUES
14 16
 from django.db import (transaction, connection, connections, DEFAULT_DB_ALIAS,
15 17
     reset_queries)
  18
+from django.forms.fields import CharField
16 19
 from django.http import QueryDict
17 20
 from django.test import _doctest as doctest
18 21
 from django.test.client import Client
@@ -271,6 +274,53 @@ def assertRaisesMessage(self, expected_exception, expected_message,
271 274
         return self.assertRaisesRegexp(expected_exception,
272 275
                 re.escape(expected_message), callable_obj, *args, **kwargs)
273 276
 
  277
+    def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None,
  278
+            field_kwargs=None, empty_value=u''):
  279
+        """
  280
+        Asserts that a form field behaves correctly with various inputs.
  281
+
  282
+        Args:
  283
+            fieldclass: the class of the field to be tested.
  284
+            valid: a dictionary mapping valid inputs to their expected
  285
+                    cleaned values.
  286
+            invalid: a dictionary mapping invalid inputs to one or more
  287
+                    raised error messages.
  288
+            field_args: the args passed to instantiate the field
  289
+            field_kwargs: the kwargs passed to instantiate the field
  290
+            empty_value: the expected clean output for inputs in EMPTY_VALUES
  291
+
  292
+        """
  293
+        if field_args is None:
  294
+            field_args = []
  295
+        if field_kwargs is None:
  296
+            field_kwargs = {}
  297
+        required = fieldclass(*field_args, **field_kwargs)
  298
+        optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
  299
+        # test valid inputs
  300
+        for input, output in valid.items():
  301
+            self.assertEqual(required.clean(input), output)
  302
+            self.assertEqual(optional.clean(input), output)
  303
+        # test invalid inputs
  304
+        for input, errors in invalid.items():
  305
+            with self.assertRaises(ValidationError) as context_manager:
  306
+                required.clean(input)
  307
+            self.assertEqual(context_manager.exception.messages, errors)
  308
+
  309
+            with self.assertRaises(ValidationError) as context_manager:
  310
+                optional.clean(input)
  311
+            self.assertEqual(context_manager.exception.messages, errors)
  312
+        # test required inputs
  313
+        error_required = [u'This field is required.']
  314
+        for e in EMPTY_VALUES:
  315
+            with self.assertRaises(ValidationError) as context_manager:
  316
+                required.clean(e)
  317
+            self.assertEqual(context_manager.exception.messages, error_required)
  318
+            self.assertEqual(optional.clean(e), empty_value)
  319
+        # test that max_length and min_length are always accepted
  320
+        if issubclass(fieldclass, CharField):
  321
+            field_kwargs.update({'min_length':2, 'max_length':20})
  322
+            self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs), fieldclass))
  323
+
274 324
 class TransactionTestCase(SimpleTestCase):
275 325
     # The class we'll use for the test client self.client.
276 326
     # Can be overridden in derived classes.
@@ -356,8 +406,8 @@ def _post_teardown(self):
356 406
         # be created with the wrong time).
357 407
         # To make sure this doesn't happen, get a clean connection at the
358 408
         # start of every test.
359  
-        for connection in connections.all():
360  
-            connection.close()
  409
+        for conn in connections.all():
  410
+            conn.close()
361 411
 
362 412
     def _fixture_teardown(self):
363 413
         pass
@@ -552,9 +602,9 @@ def assertQuerysetEqual(self, qs, values, transform=repr):
552 602
 
553 603
     def assertNumQueries(self, num, func=None, *args, **kwargs):
554 604
         using = kwargs.pop("using", DEFAULT_DB_ALIAS)
555  
-        connection = connections[using]
  605
+        conn = connections[using]
556 606
 
557  
-        context = _AssertNumQueriesContext(self, num, connection)
  607
+        context = _AssertNumQueriesContext(self, num, conn)
558 608
         if func is None:
559 609
             return context
560 610
 
21  docs/topics/testing.txt
@@ -1178,6 +1178,7 @@ basic functionality like:
1178 1178
 
1179 1179
  * Saving and restoring the Python warning machinery state.
1180 1180
  * Checking that a callable :meth:`raises a certain exeception <TestCase.assertRaisesMessage>`.
  1181
+ * :meth:`Testing form field rendering <assertFieldOutput>`.
1181 1182
 
1182 1183
 If you need any of the other more complex and heavyweight Django-specific
1183 1184
 features like:
@@ -1523,6 +1524,26 @@ your test suite.
1523 1524
     failure. Similar to unittest's ``assertRaisesRegexp`` with the difference
1524 1525
     that ``expected_message`` isn't a regular expression.
1525 1526
 
  1527
+.. method:: assertFieldOutput(self, fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value=u'')
  1528
+
  1529
+    Asserts that a form field behaves correctly with various inputs.
  1530
+
  1531
+    :param fieldclass: the class of the field to be tested.
  1532
+    :param valid: a dictionary mapping valid inputs to their expected cleaned
  1533
+        values.
  1534
+    :param invalid: a dictionary mapping invalid inputs to one or more raised
  1535
+        error messages.
  1536
+    :param field_args: the args passed to instantiate the field.
  1537
+    :param field_kwargs: the kwargs passed to instantiate the field.
  1538
+    :param empty_value: the expected clean output for inputs in ``EMPTY_VALUES``.
  1539
+
  1540
+    For example, the following code tests that an ``EmailField`` accepts
  1541
+    "a@a.com" as a valid email address, but rejects "aaa" with a reasonable
  1542
+    error message::
  1543
+
  1544
+        self.assertFieldOutput(EmailField, {'a@a.com': 'a@a.com'}, {'aaa': [u'Enter a valid e-mail address.']})
  1545
+
  1546
+
1526 1547
 .. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
1527 1548
 
1528 1549
     Asserts that a ``Response`` instance produced the given ``status_code`` and
12  tests/regressiontests/forms/localflavor/__init__.py
... ...
@@ -1,11 +1 @@
1  
-from django.forms import EmailField
2  
-from utils import LocalFlavorTestCase
3  
-
4  
-class AssertFieldOutputTests(LocalFlavorTestCase):
5  
-
6  
-    def test_assert_field_output(self):
7  
-        error_invalid = [u'Enter a valid e-mail address.']
8  
-        self.assertFieldOutput(EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid})
9  
-        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid + [u'Another error']})
10  
-        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'Wrong output'}, {'aaa': error_invalid})
11  
-        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': [u'Come on, gimme some well formatted data, dude.']})
  1
+#
4  tests/regressiontests/forms/localflavor/ar.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.ar.forms import (ARProvinceSelect,
2 2
     ARPostalCodeField, ARDNIField, ARCUITField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ARLocalFlavorTests(LocalFlavorTestCase):
  7
+class ARLocalFlavorTests(SimpleTestCase):
8 8
     def test_ARProvinceSelect(self):
9 9
         f = ARProvinceSelect()
10 10
         out = u'''<select name="provincias">
4  tests/regressiontests/forms/localflavor/at.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.at.forms import (ATZipCodeField, ATStateSelect,
2 2
     ATSocialSecurityNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ATLocalFlavorTests(LocalFlavorTestCase):
  7
+class ATLocalFlavorTests(SimpleTestCase):
8 8
     def test_ATStateSelect(self):
9 9
         f = ATStateSelect()
10 10
         out = u'''<select name="bundesland">
4  tests/regressiontests/forms/localflavor/au.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.au.forms import (AUPostCodeField,
2 2
         AUPhoneNumberField, AUStateSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class AULocalFlavorTests(LocalFlavorTestCase):
  7
+class AULocalFlavorTests(SimpleTestCase):
8 8
     def test_AUStateSelect(self):
9 9
         f = AUStateSelect()
10 10
         out = u'''<select name="state">
4  tests/regressiontests/forms/localflavor/be.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.be.forms import (BEPostalCodeField,
2 2
     BEPhoneNumberField, BERegionSelect, BEProvinceSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class BELocalFlavorTests(LocalFlavorTestCase):
  7
+class BELocalFlavorTests(SimpleTestCase):
8 8
     def test_BEPostalCodeField(self):
9 9
         error_format = [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
10 10
         valid = {
4  tests/regressiontests/forms/localflavor/br.py
@@ -2,10 +2,10 @@
2 2
     BRCNPJField, BRCPFField, BRPhoneNumberField, BRStateSelect,
3 3
     BRStateChoiceField)
4 4
 
5  
-from utils import LocalFlavorTestCase
  5
+from django.test import SimpleTestCase
6 6
 
7 7
 
8  
-class BRLocalFlavorTests(LocalFlavorTestCase):
  8
+class BRLocalFlavorTests(SimpleTestCase):
9 9
     def test_BRZipCodeField(self):
10 10
         error_format = [u'Enter a zip code in the format XXXXX-XXX.']
11 11
         valid = {
4  tests/regressiontests/forms/localflavor/ca.py
@@ -4,10 +4,10 @@
4 4
         CAPhoneNumberField, CAProvinceField, CAProvinceSelect,
5 5
         CASocialInsuranceNumberField)
6 6
 
7  
-from utils import LocalFlavorTestCase
  7
+from django.test import SimpleTestCase
8 8
 
9 9
 
10  
-class CALocalFlavorTests(LocalFlavorTestCase):
  10
+class CALocalFlavorTests(SimpleTestCase):
11 11
     def setUp(self):
12 12
         self.save_warnings_state()
13 13
         warnings.filterwarnings(
4  tests/regressiontests/forms/localflavor/ch.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.ch.forms import (CHZipCodeField,
2 2
     CHPhoneNumberField, CHIdentityCardNumberField, CHStateSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class CHLocalFlavorTests(LocalFlavorTestCase):
  7
+class CHLocalFlavorTests(SimpleTestCase):
8 8
     def test_CHStateSelect(self):
9 9
         f = CHStateSelect()
10 10
         out = u'''<select name="state">
4  tests/regressiontests/forms/localflavor/cl.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.cl.forms import CLRutField, CLRegionSelect
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class CLLocalFlavorTests(LocalFlavorTestCase):
  6
+class CLLocalFlavorTests(SimpleTestCase):
7 7
     def test_CLRegionSelect(self):
8 8
         f = CLRegionSelect()
9 9
         out = u'''<select name="foo">
4  tests/regressiontests/forms/localflavor/cn.py
@@ -2,9 +2,9 @@
2 2
 
3 3
 from django.contrib.localflavor.cn.forms import (CNProvinceSelect,
4 4
         CNPostCodeField, CNIDCardField, CNPhoneNumberField, CNCellNumberField)
5  
-from utils import LocalFlavorTestCase
  5
+from django.test import SimpleTestCase
6 6
 
7  
-class CNLocalFlavorTests(LocalFlavorTestCase):
  7
+class CNLocalFlavorTests(SimpleTestCase):
8 8
     def test_CNProvinceSelect(self):
9 9
         f = CNProvinceSelect()
10 10
         correct_output = u'''<select name="provinces">
4  tests/regressiontests/forms/localflavor/co.py
... ...
@@ -1,8 +1,8 @@
1 1
 from django.contrib.localflavor.co.forms import CODepartmentSelect
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5  
-class COLocalFlavorTests(LocalFlavorTestCase):
  5
+class COLocalFlavorTests(SimpleTestCase):
6 6
     def test_CODepartmentSelect(self):
7 7
         d = CODepartmentSelect()
8 8
         out = u"""<select name="department">
4  tests/regressiontests/forms/localflavor/cz.py
@@ -4,10 +4,10 @@
4 4
     CZRegionSelect, CZBirthNumberField, CZICNumberField)
5 5
 
6 6
 from django.core.exceptions import ValidationError
7  
-from utils import LocalFlavorTestCase
  7
+from django.test import SimpleTestCase
8 8
 
9 9
 
10  
-class CZLocalFlavorTests(LocalFlavorTestCase):
  10
+class CZLocalFlavorTests(SimpleTestCase):
11 11
     def setUp(self):
12 12
         self.save_warnings_state()
13 13
         warnings.filterwarnings(
4  tests/regressiontests/forms/localflavor/de.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.de.forms import (DEZipCodeField, DEStateSelect,
2 2
     DEIdentityCardNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class DELocalFlavorTests(LocalFlavorTestCase):
  7
+class DELocalFlavorTests(SimpleTestCase):
8 8
     def test_DEStateSelect(self):
9 9
         f = DEStateSelect()
10 10
         out = u'''<select name="states">
4  tests/regressiontests/forms/localflavor/ec.py
... ...
@@ -1,8 +1,8 @@
1 1
 from django.contrib.localflavor.ec.forms import ECProvinceSelect
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5  
-class ECLocalFlavorTests(LocalFlavorTestCase):
  5
+class ECLocalFlavorTests(SimpleTestCase):
6 6
     def test_ECProvinceSelect(self):
7 7
         p = ECProvinceSelect()
8 8
         out = u"""<select name="province">
4  tests/regressiontests/forms/localflavor/es.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.es.forms import (ESPostalCodeField, ESPhoneNumberField,
2 2
     ESIdentityCardNumberField, ESCCCField, ESRegionSelect, ESProvinceSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ESLocalFlavorTests(LocalFlavorTestCase):
  7
+class ESLocalFlavorTests(SimpleTestCase):
8 8
     def test_ESRegionSelect(self):
9 9
         f = ESRegionSelect()
10 10
         out = u'''<select name="regions">
4  tests/regressiontests/forms/localflavor/fi.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.fi.forms import (FIZipCodeField,
2 2
     FISocialSecurityNumber, FIMunicipalitySelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class FILocalFlavorTests(LocalFlavorTestCase):
  7
+class FILocalFlavorTests(SimpleTestCase):
8 8
     def test_FIMunicipalitySelect(self):
9 9
         f = FIMunicipalitySelect()
10 10
         out = u'''<select name="municipalities">
4  tests/regressiontests/forms/localflavor/fr.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.fr.forms import (FRZipCodeField,
2 2
         FRPhoneNumberField, FRDepartmentSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class FRLocalFlavorTests(LocalFlavorTestCase):
  7
+class FRLocalFlavorTests(SimpleTestCase):
8 8
     def test_FRZipCodeField(self):
9 9
         error_format = [u'Enter a zip code in the format XXXXX.']
10 10
         valid = {
4  tests/regressiontests/forms/localflavor/gb.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.gb.forms import GBPostcodeField
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class GBLocalFlavorTests(LocalFlavorTestCase):
  6
+class GBLocalFlavorTests(SimpleTestCase):
7 7
     def test_GBPostcodeField(self):
8 8
         error_invalid = [u'Enter a valid postcode.']
9 9
         valid = {
4  tests/regressiontests/forms/localflavor/generic.py
@@ -2,10 +2,10 @@
2 2
 
3 3
 from django.contrib.localflavor.generic.forms import DateField, DateTimeField
4 4
 
5  
-from utils import LocalFlavorTestCase
  5
+from django.test import SimpleTestCase
6 6
 
7 7
 
8  
-class GenericLocalFlavorTests(LocalFlavorTestCase):
  8
+class GenericLocalFlavorTests(SimpleTestCase):
9 9
     def test_GenericDateField(self):
10 10
         error_invalid = [u'Enter a valid date.']
11 11
         valid = {
4  tests/regressiontests/forms/localflavor/hr.py
@@ -4,9 +4,9 @@
4 4
     HRLicensePlateField, HRPostalCodeField, HROIBField, HRJMBGField,
5 5
     HRJMBAGField)
6 6
 
7  
-from utils import LocalFlavorTestCase
  7
+from django.test import SimpleTestCase
8 8
 
9  
-class HRLocalFlavorTests(LocalFlavorTestCase):
  9
+class HRLocalFlavorTests(SimpleTestCase):
10 10
     def test_HRCountySelect(self):
11 11
         f = HRCountySelect()
12 12
         out = u'''<select name="county">
4  tests/regressiontests/forms/localflavor/id.py
@@ -4,10 +4,10 @@
4 4
     IDPostCodeField, IDNationalIdentityNumberField, IDLicensePlateField,
5 5
     IDProvinceSelect, IDLicensePlatePrefixSelect)
6 6
 
7  
-from utils import LocalFlavorTestCase
  7
+from django.test import SimpleTestCase
8 8
 
9 9
 
10  
-class IDLocalFlavorTests(LocalFlavorTestCase):
  10
+class IDLocalFlavorTests(SimpleTestCase):
11 11
     def setUp(self):
12 12
         self.save_warnings_state()
13 13
         warnings.filterwarnings(
4  tests/regressiontests/forms/localflavor/ie.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.ie.forms import IECountySelect
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class IELocalFlavorTests(LocalFlavorTestCase):
  6
+class IELocalFlavorTests(SimpleTestCase):
7 7
     def test_IECountySelect(self):
8 8
         f = IECountySelect()
9 9
         out = u'''<select name="counties">
4  tests/regressiontests/forms/localflavor/il.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.il.forms import (ILPostalCodeField,
2 2
     ILIDNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ILLocalFlavorTests(LocalFlavorTestCase):
  7
+class ILLocalFlavorTests(SimpleTestCase):
8 8
     def test_ILPostalCodeField(self):
9 9
         error_format = [u'Enter a postal code in the format XXXXX']
10 10
         valid = {
4  tests/regressiontests/forms/localflavor/in_.py
... ...
@@ -1,11 +1,11 @@
1 1
 from django.contrib.localflavor.in_.forms import (INZipCodeField,
2 2
     INStateField, INStateSelect, INPhoneNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7 7
 
8  
-class INLocalFlavorTests(LocalFlavorTestCase):
  8
+class INLocalFlavorTests(SimpleTestCase):
9 9
     def test_INPhoneNumberField(self):
10 10
         error_format = [u'Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.']
11 11
         valid = {
4  tests/regressiontests/forms/localflavor/is_.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.is_.forms import (ISIdNumberField,
2 2
     ISPhoneNumberField, ISPostalCodeSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ISLocalFlavorTests(LocalFlavorTestCase):
  7
+class ISLocalFlavorTests(SimpleTestCase):
8 8
     def test_ISPostalCodeSelect(self):
9 9
         f = ISPostalCodeSelect()
10 10
         out = u'''<select name="foo">
4  tests/regressiontests/forms/localflavor/it.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.it.forms import (ITZipCodeField, ITRegionSelect,
2 2
     ITSocialSecurityNumberField, ITVatNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class ITLocalFlavorTests(LocalFlavorTestCase):
  7
+class ITLocalFlavorTests(SimpleTestCase):
8 8
     def test_ITRegionSelect(self):
9 9
         f = ITRegionSelect()
10 10
         out = u'''<select name="regions">
4  tests/regressiontests/forms/localflavor/jp.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.jp.forms import (JPPostalCodeField,
2 2
     JPPrefectureSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class JPLocalFlavorTests(LocalFlavorTestCase):
  7
+class JPLocalFlavorTests(SimpleTestCase):
8 8
     def test_JPPrefectureSelect(self):
9 9
         f = JPPrefectureSelect()
10 10
         out = u'''<select name="prefecture">
4  tests/regressiontests/forms/localflavor/kw.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.kw.forms import KWCivilIDNumberField
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class KWLocalFlavorTests(LocalFlavorTestCase):
  6
+class KWLocalFlavorTests(SimpleTestCase):
7 7
     def test_KWCivilIDNumberField(self):
8 8
         error_invalid = [u'Enter a valid Kuwaiti Civil ID number']
9 9
         valid = {
4  tests/regressiontests/forms/localflavor/mk.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.mk.forms import (
2 2
     MKIdentityCardNumberField, MKMunicipalitySelect, UMCNField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class MKLocalFlavorTests(LocalFlavorTestCase):
  7
+class MKLocalFlavorTests(SimpleTestCase):
8 8
 
9 9
     def test_MKIdentityCardNumberField(self):
10 10
         error_invalid  = [u'Identity card numbers must contain either 4 to 7 '
4  tests/regressiontests/forms/localflavor/mx.py
@@ -2,10 +2,10 @@
2 2
 from django.contrib.localflavor.mx.forms import (MXZipCodeField, MXRFCField,
3 3
     MXStateSelect, MXCURPField)
4 4
 
5  
-from utils import LocalFlavorTestCase
  5
+from django.test import SimpleTestCase
6 6
 
7 7
 
8  
-class MXLocalFlavorTests(LocalFlavorTestCase):
  8
+class MXLocalFlavorTests(SimpleTestCase):
9 9
     def test_MXStateSelect(self):
10 10
         f = MXStateSelect()
11 11
         out = u'''<select name="state">
4  tests/regressiontests/forms/localflavor/nl.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.nl.forms import (NLPhoneNumberField,
2 2
     NLZipCodeField, NLSoFiNumberField, NLProvinceSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class NLLocalFlavorTests(LocalFlavorTestCase):
  7
+class NLLocalFlavorTests(SimpleTestCase):
8 8
     def test_NLProvinceSelect(self):
9 9
         f = NLProvinceSelect()
10 10
         out = u'''<select name="provinces">
4  tests/regressiontests/forms/localflavor/pl.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.pl.forms import (PLProvinceSelect,
2 2
     PLCountySelect, PLPostalCodeField, PLNIPField, PLPESELField, PLNationalIDCardNumberField, PLREGONField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class PLLocalFlavorTests(LocalFlavorTestCase):
  7
+class PLLocalFlavorTests(SimpleTestCase):
8 8
     def test_PLProvinceSelect(self):
9 9
         f = PLProvinceSelect()
10 10
         out = u'''<select name="voivodeships">
4  tests/regressiontests/forms/localflavor/pt.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.pt.forms import PTZipCodeField, PTPhoneNumberField
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class PTLocalFlavorTests(LocalFlavorTestCase):
  6
+class PTLocalFlavorTests(SimpleTestCase):
7 7
     def test_PTZipCodeField(self):
8 8
         error_format = [u'Enter a zip code in the format XXXX-XXX.']
9 9
         valid = {
4  tests/regressiontests/forms/localflavor/py.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.py.forms import (PyDepartmentSelect,
2 2
     PyNumberedDepartmentSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6  
-class PYLocalFlavorTests(LocalFlavorTestCase):
  6
+class PYLocalFlavorTests(SimpleTestCase):
7 7
     def test_PyDepartmentSelect(self):
8 8
         py = PyDepartmentSelect()
9 9
         out = u'''<select name="department">
4  tests/regressiontests/forms/localflavor/ro.py
@@ -3,10 +3,10 @@
3 3
     ROCountyField, ROCountySelect, ROIBANField, ROPhoneNumberField,
4 4
     ROPostalCodeField)
5 5
 
6  
-from utils import LocalFlavorTestCase
  6
+from django.test import SimpleTestCase
7 7
 
8 8
 
9  
-class ROLocalFlavorTests(LocalFlavorTestCase):
  9
+class ROLocalFlavorTests(SimpleTestCase):
10 10
     def test_ROCountySelect(self):
11 11
         f = ROCountySelect()
12 12
         out = u'''<select name="county">
4  tests/regressiontests/forms/localflavor/ru.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.ru.forms import *
2 2
  
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class RULocalFlavorTests(LocalFlavorTestCase):
  6
+class RULocalFlavorTests(SimpleTestCase):
7 7
 
8 8
     def test_RUPassportNumberField(self):
9 9
         error = [u'Enter a passport number in the format XXXX XXXXXX.']
4  tests/regressiontests/forms/localflavor/se.py
@@ -4,10 +4,10 @@
4 4
     SEPostalCodeField)
5 5
 import datetime
6 6
 
7  
-from utils import LocalFlavorTestCase
  7
+from django.test import SimpleTestCase
8 8
 
9 9
 
10  
-class SELocalFlavorTests(LocalFlavorTestCase):
  10
+class SELocalFlavorTests(SimpleTestCase):
11 11
 
12 12
     def setUp(self):
13 13
         # Mocking datetime.date to make sure
4  tests/regressiontests/forms/localflavor/sk.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.sk.forms import (SKRegionSelect,
2 2
     SKPostalCodeField, SKDistrictSelect)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class SKLocalFlavorTests(LocalFlavorTestCase):
  7
+class SKLocalFlavorTests(SimpleTestCase):
8 8
     def test_SKRegionSelect(self):
9 9
         f = SKRegionSelect()
10 10
         out = u'''<select name="regions">
4  tests/regressiontests/forms/localflavor/us.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.us.forms import (USZipCodeField,
2 2
     USPhoneNumberField, USStateField, USStateSelect, USSocialSecurityNumberField)
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class USLocalFlavorTests(LocalFlavorTestCase):
  7
+class USLocalFlavorTests(SimpleTestCase):
8 8
     def test_USStateSelect(self):
9 9
         f = USStateSelect()
10 10
         out = u'''<select name="state">
64  tests/regressiontests/forms/localflavor/utils.py
... ...
@@ -1,64 +0,0 @@
1  
-from __future__ import with_statement
2  
-
3  
-from django.core.exceptions import ValidationError
4  
-from django.core.validators import EMPTY_VALUES
5  
-from django.forms.fields import CharField
6  
-from django.test.utils import get_warnings_state, restore_warnings_state
7  
-from django.utils.unittest import TestCase
8  
-
9  
-
10  
-class LocalFlavorTestCase(TestCase):
11  
-    # NOTE: These are copied from the TestCase Django uses for tests which
12  
-    # access the database
13  
-    def save_warnings_state(self):
14  
-        self._warnings_state = get_warnings_state()
15  
-
16  
-    def restore_warnings_state(self):
17  
-        restore_warnings_state(self._warnings_state)
18  
-
19  
-    def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None,
20  
-            field_kwargs=None, empty_value=u''):
21  
-        """
22  
-        Asserts that a field behaves correctly with various inputs.
23  
-
24  
-        Args:
25  
-            fieldclass: the class of the field to be tested.
26  
-            valid: a dictionary mapping valid inputs to their expected
27  
-                    cleaned values.
28  
-            invalid: a dictionary mapping invalid inputs to one or more
29  
-                    raised error messages.
30  
-            field_args: the args passed to instantiate the field
31  
-            field_kwargs: the kwargs passed to instantiate the field
32  
-            empty_value: the expected clean output for inputs in EMPTY_VALUES
33  
-
34  
-        """
35  
-        if field_args is None:
36  
-            field_args = []
37  
-        if field_kwargs is None:
38  
-            field_kwargs = {}
39  
-        required = fieldclass(*field_args, **field_kwargs)
40  
-        optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
41  
-        # test valid inputs
42  
-        for input, output in valid.items():
43  
-            self.assertEqual(required.clean(input), output)
44  
-            self.assertEqual(optional.clean(input), output)
45  
-        # test invalid inputs
46  
-        for input, errors in invalid.items():
47  
-            with self.assertRaises(ValidationError) as context_manager:
48  
-                required.clean(input)
49  
-            self.assertEqual(context_manager.exception.messages, errors)
50  
-
51  
-            with self.assertRaises(ValidationError) as context_manager:
52  
-                optional.clean(input)
53  
-            self.assertEqual(context_manager.exception.messages, errors)
54  
-        # test required inputs
55  
-        error_required = [u'This field is required.']
56  
-        for e in EMPTY_VALUES:
57  
-            with self.assertRaises(ValidationError) as context_manager:
58  
-                required.clean(e)
59  
-            self.assertEqual(context_manager.exception.messages, error_required)
60  
-            self.assertEqual(optional.clean(e), empty_value)
61  
-        # test that max_length and min_length are always accepted
62  
-        if issubclass(fieldclass, CharField):
63  
-            field_kwargs.update({'min_length':2, 'max_length':20})
64  
-            self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs), fieldclass))
4  tests/regressiontests/forms/localflavor/uy.py
... ...
@@ -1,10 +1,10 @@
1 1
 from django.contrib.localflavor.uy.forms import UYDepartamentSelect, UYCIField
2 2
 from django.contrib.localflavor.uy.util import get_validation_digit
3 3
 
4  
-from utils import LocalFlavorTestCase
  4
+from django.test import SimpleTestCase
5 5
 
6 6
 
7  
-class UYLocalFlavorTests(LocalFlavorTestCase):
  7
+class UYLocalFlavorTests(SimpleTestCase):
8 8
     def test_UYDepartmentSelect(self):
9 9
         f = UYDepartamentSelect()
10 10
         out = u'''<select name="departamentos">
4  tests/regressiontests/forms/localflavor/za.py
... ...
@@ -1,9 +1,9 @@
1 1
 from django.contrib.localflavor.za.forms import ZAIDField, ZAPostCodeField
2 2
 
3  
-from utils import LocalFlavorTestCase
  3
+from django.test import SimpleTestCase
4 4
 
5 5
 
6  
-class ZALocalFlavorTests(LocalFlavorTestCase):
  6
+class ZALocalFlavorTests(SimpleTestCase):
7 7
     def test_ZAIDField(self):
8 8
         error_invalid = [u'Enter a valid South African ID number']
9 9
         valid = {
1  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,4 +1,3 @@
1  
-from localflavor import AssertFieldOutputTests
2 1
 from localflavor.ar import ARLocalFlavorTests
3 2
 from localflavor.at import ATLocalFlavorTests
4 3
 from localflavor.au import AULocalFlavorTests
1  tests/regressiontests/forms/tests/__init__.py
@@ -53,5 +53,4 @@
53 53
     USLocalFlavorTests,
54 54
     UYLocalFlavorTests,
55 55
     ZALocalFlavorTests,
56  
-    AssertFieldOutputTests,
57 56
 )
11  tests/regressiontests/test_utils/tests.py
... ...
@@ -1,5 +1,6 @@
1 1
 from __future__ import with_statement
2 2
 
  3
+from django.forms import EmailField
3 4
 from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
4 5
 from django.utils.unittest import skip
5 6
 
@@ -139,6 +140,16 @@ def func1():
139 140
         self.assertRaisesMessage(ValueError, "[.*x+]y?", func1)
140 141
 
141 142
 
  143
+class AssertFieldOutputTests(SimpleTestCase):
  144
+
  145
+    def test_assert_field_output(self):
  146
+        error_invalid = [u'Enter a valid e-mail address.']
  147
+        self.assertFieldOutput(EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid})
  148
+        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid + [u'Another error']})
  149
+        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'Wrong output'}, {'aaa': error_invalid})
  150
+        self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': [u'Come on, gimme some well formatted data, dude.']})
  151
+
  152
+
142 153
 __test__ = {"API_TEST": r"""
143 154
 # Some checks of the doctest output normalizer.
144 155
 # Standard doctests do fairly

0 notes on commit 2664fa1

Please sign in to comment.
Something went wrong with that request. Please try again.