def test_invalid_name(): data = {'name': 'Gram', 'mail': 'mail.ru', 'count': 10} v = Scheme(data) assert v.is_valid() is False assert v.cleaned_data is None error = vaa.Error(field='mail', message='Not a valid email address.') assert v.errors == [error]
def test_scheme_errors_rewrite_message(scheme): @deal.pre(scheme, message='old message') def func(name): return name * 2 try: func(2) except deal.PreContractError as exc: assert exc.args[0] == [ vaa.Error(field='name', message='Not a valid string.') ]
def test_scheme_errors_rewrite_message(scheme): @deal.pre(scheme, message='old message') def func(name): return name * 2 try: func(2) except deal.PreContractError as exc: assert exc.errors == [ vaa.Error(field='name', message='Not a valid string.') ] else: raise AssertionError('exception was not raised')
def test_scheme_string_validation_args_correct(scheme): @deal.pre(scheme) def func(name): return name * 2 assert func('Chris') == 'ChrisChris' with pytest.raises(deal.PreContractError): func(123) try: func(123) except deal.PreContractError as e: assert e.args[0] == [ vaa.Error(field='name', message='Not a valid string.') ]
def test_scheme_contract_is_satisfied_when_setting_arg(scheme): @deal.inv(scheme) class User: name = '' user = User() user.name = 'Chris' with pytest.raises(deal.InvContractError): user.name = 123 try: user.name = 123 except deal.InvContractError as e: assert e.args[0] == [ vaa.Error(field='name', message='Not a valid string.') ]
def validator_raise_container(_): if _.a > 0 and _.b > 0: return True raise vaa.ValidationError('should be positive') def validator_return_error_container(_): if _.a > 0 and _.b > 0: return True return vaa.ValidationError('should be positive') @pytest.mark.parametrize('validator, errors', [ (lambda a, b: a > 0 and b > 0, [vaa.Error(message='PH')]), (validator_bool_kwargs, [vaa.Error(message='PH')]), (validator_string_kwargs, [vaa.Error(message='should be positive')]), (validator_list_kwargs, [vaa.Error(message='should be'), vaa.Error(message='positive')]), (validator_dict_kwargs, [vaa.Error(message='should be positive', field='ab')]), (validator_raise_kwargs, [vaa.Error(message='should be positive')]), (validator_bool_container, [vaa.Error(message='PH')]), (validator_string_container, [vaa.Error(message='should be positive')]), (validator_dict_container, [vaa.Error(message='should be positive', field='ab')]), (validator_raise_container, [vaa.Error(message='should be positive')]), (validator_return_error_container, [vaa.Error(message='should be positive')]),