-
diff --git a/django/test/testcases.py b/django/test/testcases.py
a
|
b
|
|
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 | 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 | 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 | 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 | |
-
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
a
|
b
|
|
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: |
… |
… |
|
1522 | 1523 | failure. Similar to unittest's ``assertRaisesRegexp`` with the difference |
1523 | 1524 | that ``expected_message`` isn't a regular expression. |
1524 | 1525 | |
| 1526 | .. method:: assertFieldOutput(self, fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value=u'') |
| 1527 | |
| 1528 | Asserts that a form field behaves correctly with various inputs. |
| 1529 | |
| 1530 | :param fieldclass: the class of the field to be tested. |
| 1531 | :param valid: a dictionary mapping valid inputs to their expected cleaned |
| 1532 | values. |
| 1533 | :param invalid: a dictionary mapping invalid inputs to one or more raised |
| 1534 | error messages. |
| 1535 | :param field_args: the args passed to instantiate the field. |
| 1536 | :param field_kwargs: the kwargs passed to instantiate the field. |
| 1537 | :param empty_value: the expected clean output for inputs in ``EMPTY_VALUES``. |
| 1538 | |
1525 | 1539 | .. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='') |
1526 | 1540 | |
1527 | 1541 | Asserts that a ``Response`` instance produced the given ``status_code`` and |
-
diff --git a/tests/regressiontests/forms/localflavor/__init__.py b/tests/regressiontests/forms/localflavor/__init__.py
a
|
b
|
|
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 | # |
-
diff --git a/tests/regressiontests/forms/localflavor/ar.py b/tests/regressiontests/forms/localflavor/ar.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/au.py b/tests/regressiontests/forms/localflavor/au.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/be.py b/tests/regressiontests/forms/localflavor/be.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/br.py b/tests/regressiontests/forms/localflavor/br.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/ca.py b/tests/regressiontests/forms/localflavor/ca.py
a
|
b
|
|
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( |
-
diff --git a/tests/regressiontests/forms/localflavor/ch.py b/tests/regressiontests/forms/localflavor/ch.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/cl.py b/tests/regressiontests/forms/localflavor/cl.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/cn.py b/tests/regressiontests/forms/localflavor/cn.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/co.py b/tests/regressiontests/forms/localflavor/co.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/cz.py b/tests/regressiontests/forms/localflavor/cz.py
a
|
b
|
|
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( |
-
diff --git a/tests/regressiontests/forms/localflavor/de.py b/tests/regressiontests/forms/localflavor/de.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/ec.py b/tests/regressiontests/forms/localflavor/ec.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/es.py b/tests/regressiontests/forms/localflavor/es.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/fi.py b/tests/regressiontests/forms/localflavor/fi.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/fr.py b/tests/regressiontests/forms/localflavor/fr.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/gb.py b/tests/regressiontests/forms/localflavor/gb.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/generic.py b/tests/regressiontests/forms/localflavor/generic.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/hr.py b/tests/regressiontests/forms/localflavor/hr.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/id.py b/tests/regressiontests/forms/localflavor/id.py
a
|
b
|
|
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( |
-
diff --git a/tests/regressiontests/forms/localflavor/ie.py b/tests/regressiontests/forms/localflavor/ie.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/il.py b/tests/regressiontests/forms/localflavor/il.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/in_.py b/tests/regressiontests/forms/localflavor/in_.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/is_.py b/tests/regressiontests/forms/localflavor/is_.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/it.py b/tests/regressiontests/forms/localflavor/it.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/jp.py b/tests/regressiontests/forms/localflavor/jp.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/kw.py b/tests/regressiontests/forms/localflavor/kw.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/mk.py b/tests/regressiontests/forms/localflavor/mk.py
a
|
b
|
|
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 ' |
-
diff --git a/tests/regressiontests/forms/localflavor/mx.py b/tests/regressiontests/forms/localflavor/mx.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/nl.py b/tests/regressiontests/forms/localflavor/nl.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/pl.py b/tests/regressiontests/forms/localflavor/pl.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/pt.py b/tests/regressiontests/forms/localflavor/pt.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavor/py.py b/tests/regressiontests/forms/localflavor/py.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/ro.py b/tests/regressiontests/forms/localflavor/ro.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/ru.py b/tests/regressiontests/forms/localflavor/ru.py
a
|
b
|
|
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.'] |
-
diff --git a/tests/regressiontests/forms/localflavor/se.py b/tests/regressiontests/forms/localflavor/se.py
a
|
b
|
|
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 |
-
diff --git a/tests/regressiontests/forms/localflavor/sk.py b/tests/regressiontests/forms/localflavor/sk.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/us.py b/tests/regressiontests/forms/localflavor/us.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/utils.py b/tests/regressiontests/forms/localflavor/utils.py
deleted file mode 100644
+
|
-
|
|
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)) |
-
diff --git a/tests/regressiontests/forms/localflavor/uy.py b/tests/regressiontests/forms/localflavor/uy.py
a
|
b
|
|
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"> |
-
diff --git a/tests/regressiontests/forms/localflavor/za.py b/tests/regressiontests/forms/localflavor/za.py
a
|
b
|
|
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 = { |
-
diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py
a
|
b
|
|
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 |
-
diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py
a
|
b
|
|
53 | 53 | USLocalFlavorTests, |
54 | 54 | UYLocalFlavorTests, |
55 | 55 | ZALocalFlavorTests, |
56 | | AssertFieldOutputTests, |
57 | 56 | ) |
-
diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
a
|
b
|
|
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 | 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 |