def test_valid_format_invalid_value(self): validator = StdnumFormatValidator(formats=['hr.oib']) self.assertRaises( ValidationError, validator, '69435151531', )
def test_empty_format(self): validator = StdnumFormatValidator(formats=None) self.assertRaisesMessage( ValueError, "StdnumFormatValidator called without specified formats", validator, "69435151530", )
def test_invalid_format(self): validator = StdnumFormatValidator(formats=['damn']) self.assertRaisesMessage( ValueError, 'Unknown stdnum format: "damn"', validator, '123456', )
def __init__(self, formats, *args, **kwargs): if formats is None: raise ImproperlyConfigured('StdNumField defined without formats') if not is_iterable(formats) or isinstance(formats, string_types): formats = [formats] for format in formats: if format not in settings.DEFAULT_FORMATS: raise ValueError( 'Unknown format for StdNumField: "{}". Is it missing from ' 'settings.STDNUMFIELD["DEFAULT_FORMATS"]?'.format( format, )) if 'alphabets' in kwargs: alphabets = kwargs.pop('alphabets') alphabets = listify(alphabets) else: alphabets = None self.formats = formats self.alphabets = alphabets # TODO make dynamic when/if stdnum provides this data: kwargs["max_length"] = 254 super(StdNumField, self).__init__(*args, **kwargs) self.validators.append( StdnumFormatValidator(self.formats, self.alphabets))
def test_custom_multiple_alphabets(self): validator = StdnumFormatValidator( formats=["iso7064.mod_37_2", "iso7064.mod_37_36"], alphabets=["0123456789X", "0123456789"], ) validator("002006673085")
def test_custom_alphabet(self): validator = StdnumFormatValidator(formats="iso7064.mod_37_2", alphabets="0123456789X") validator("079X")
def test_default_format_valid_value(self): validator = StdnumFormatValidator(formats=["hr.oib"]) validator("69435151530")
def test_custom_multiple_alphabets(self): validator = StdnumFormatValidator( formats=['iso7064.mod_37_2', 'iso7064.mod_37_36'], alphabets=['0123456789X', '0123456789'], ) validator('002006673085')
def test_custom_alphabet(self): validator = StdnumFormatValidator( formats='iso7064.mod_37_2', alphabets='0123456789X', ) validator('079X')
def test_default_format_valid_value(self): validator = StdnumFormatValidator(formats=['hr.oib']) validator('69435151530')