Skip to content

Commit

Permalink
Finish converting BRCNPJField to CharField.
Browse files Browse the repository at this point in the history
* Add Lefteris Nikoltsios to authors file.
* Fix problem with min_length / max_length comment.
* Add tests for min_length / max_length comment.
  • Loading branch information
benkonrath committed Nov 13, 2016
1 parent da5b9de commit 53f846c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/authors.rst
Expand Up @@ -52,6 +52,7 @@ Authors
* Justin Bronn
* Karen Tracey
* László Ratskó
* Lefteris Nikoltsios
* Łukasz Langa
* Luke Benstead
* luyikei
Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.rst
Expand Up @@ -20,7 +20,8 @@ New fields for existing flavors:
Modifications to existing flavors:

- Enhancements of localflavor.br.forms.BRCNPJField
(`gh-240 <https://github.com/django/django-localflavor/pull/240>`_).
(`gh-240 <https://github.com/django/django-localflavor/pull/240>`_)
(`gh-254 <https://github.com/django/django-localflavor/pull/254>`_).
- Fixed century bug with Kuwait Civil ID verification localflavor.kw.forms
(`gh-195 <https://github.com/django/django-localflavor/pull/195>`_).
- Allow passing field name as first positional argument of IBANField.
Expand Down
6 changes: 4 additions & 2 deletions localflavor/br/forms.py
Expand Up @@ -159,13 +159,15 @@ class BRCNPJField(CharField):
digits.
If you want to use the long format only, you can specify:
brcnpj_field = BRCNPJField(min_length=18)
brcnpj_field = BRCNPJField(min_length=16)
If you want to use the short format, you can specify:
brcnpj_field = BRCNPJField(max_length=14)
Otherwise both formats will be valid.
.. _Brazilian CNPJ: http://en.wikipedia.org/wiki/National_identification_number#Brazil
.. versionchanged:: 1.4
"""

default_error_messages = {
Expand Down
36 changes: 28 additions & 8 deletions tests/test_br.py
Expand Up @@ -23,22 +23,42 @@ def test_BRZipCodeField(self):
self.assertFieldOutput(BRZipCodeField, valid, invalid)

def test_BRCNPJField(self):
error_format = ['Invalid CNPJ number.']
valid = {
error_format = {
'invalid': ['Invalid CNPJ number.'],
'only_long_version': ['Ensure this value has at least 16 characters (it has 14).'],
# The long version can be 16 or 18 characters long so actual error message is set dynamically when the
# invalid_long dict is generated.
'only_short_version': ['Ensure this value has at most 14 characters (it has %s).'],
}

long_version_valid = {
'64.132.916/0001-88': '64.132.916/0001-88',
'64-132-916/0001-88': '64-132-916/0001-88',
'64132916/0001-88': '64132916/0001-88',
'64132916000188': '64132916000188'
}
short_version_valid = {
'64132916000188': '64132916000188',
}
valid = long_version_valid.copy()
valid.update(short_version_valid)

invalid = {
'../-12345678901210': error_format,
'12-345-678/9012-10': error_format,
'12.345.678/9012-10': error_format,
'12345678/9012-10': error_format,
'64.132.916/0001-XX': error_format,
'../-12345678901210': error_format['invalid'],
'12-345-678/9012-10': error_format['invalid'],
'12.345.678/9012-10': error_format['invalid'],
'12345678/9012-10': error_format['invalid'],
'64.132.916/0001-XX': error_format['invalid'],
}
self.assertFieldOutput(BRCNPJField, valid, invalid)

# The short versions should be invalid when 'min_length=16' passed to the field.
invalid_short = dict([(k, error_format['only_long_version']) for k in short_version_valid.keys()])
self.assertFieldOutput(BRCNPJField, long_version_valid, invalid_short, field_kwargs={'min_length': 16})

# The long versions should be invalid when 'max_length=14' passed to the field.
invalid_long = dict([(k, [error_format['only_short_version'][0] % len(k)]) for k in long_version_valid.keys()])
self.assertFieldOutput(BRCNPJField, short_version_valid, invalid_long, field_kwargs={'max_length': 14})

def test_BRCPFField(self):
error_format = ['Invalid CPF number.']
error_atmost_chars = ['Ensure this value has at most 14 characters (it has 15).']
Expand Down

0 comments on commit 53f846c

Please sign in to comment.