def test_validate_invalid_custom_exc(self): data = {'name': 'abc123'} vmap = { 'name': ShouldContainsOnlyChars(exc=CustomException('Testing')) } validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)
def test_validate_absent_field_custom_exc(self): data = {'name_diff': 'abcabc'} vmap = { 'name': CanNotBeAEmptyStr(required_exc=CustomException('Test')) } validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)
def test_validate_absent_field_custom_exc(self): data = {'name_diff': 'abcabc'} vmap = { 'name': ShouldContainsOnlyChars(required_exc=CustomException('Test')) } validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)
def test_validate_invalid_default(self): data = {'name': 'abc123'} vmap = {'name': ShouldContainsOnlyChars()} validator = Validator(vmap) with pytest.raises(ShouldContainsOnlyCharsExc) as exc: validator.validate(data) assert isinstance(exc.value.validation_result, ValidationResult) assert exc.value.validation_result.field_name == 'name' assert exc.value.validation_result.valid is False
def test_validate_invalid_default(self): data = {'name': 'abcabc'} vmap = {'name': MaxLengthStr(4)} validator = Validator(vmap) with pytest.raises(MaxLengthStrExc) as exc: validator.validate(data) assert isinstance(exc.value.validation_result, ValidationResult) assert exc.value.validation_result.field_name == 'name' assert exc.value.validation_result.valid is False
def test_validate_invalid_default(self): data = {'name': ''} vmap = {'name': CanNotBeAEmptyStr()} validator = Validator(vmap) with pytest.raises(CanNotBeAEmptyStrExc) as exc: validator.validate(data) assert isinstance(exc.value.validation_result, ValidationResult) assert exc.value.validation_result.field_name == 'name' assert exc.value.validation_result.valid is False
def test_validate_inexisting_field(self): data = { 'name_diff': 'ab' } vmap = { 'name': MinLengthStr(3) } validator = Validator(vmap) with pytest.raises(FieldDoesNotExistsExc): validator.validate(data)
def test_validate_invalid_custom_exc(self): data = { 'name': 'abcabc' } vmap = { 'name': MinLengthStr(10, exc=CustomException('Testing')) } validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)
def test_validate_invalid_default(self): data = {'num': 'ab'} vmap = {'num': ShouldBeInt()} validator = Validator(vmap) with pytest.raises(ShouldBeIntExc) as exc: validator.validate(data) assert isinstance(exc.value.validation_result, ValidationResult) assert exc.value.validation_result.field_name == 'num' assert exc.value.validation_result.valid is False
def test_validate_inexisting_field(self): data = { 'name_diff': 'abcabc' } vmap = { 'name': StrShouldContains('-') } validator = Validator(vmap) with pytest.raises(FieldDoesNotExistsExc): validator.validate(data)
def test_validate_serialized(self): valid_msg1 = 'Is Valid 1' valid_msg2 = 'Is Valid 2' data = { 'name': { 'name1': 'abc-abc-abc-abc-abc-abc', 'name2': 'abc-abc', 'name3': 'abc-abc' } } vmap = { 'name': { 'name1': MinLengthStr(10, valid_msg=valid_msg1), 'name2': MinLengthStr(3, valid_msg=valid_msg2), 'name3': [MinLengthStr(3, valid_msg=valid_msg1)] } } validator = Validator(vmap) result_map = validator.validate(data) map_msgs = ValidationSerialized().map_msgs(result_map, root=True) assert 'name.name1' in map_msgs.keys() assert 'name.name2' in map_msgs.keys() assert 'name.name3' in map_msgs.keys() assert map_msgs['name.name1'] == valid_msg1 assert map_msgs['name.name2'] == valid_msg2 assert map_msgs['name.name3'][0] == valid_msg1
def test_validate_serialized(self): valid_msg1 = 'Is Valid 1' valid_msg2 = 'Is Valid 2' data = { 'name': { 'name1': 'abc-abc-abc-abc-abc-abc', 'name2': 'abc-abc', 'name3': 'abc-abc' } } vmap = { 'name': { 'name1': StrShouldContains('abc-abc-abc', valid_msg=valid_msg1), 'name2': StrShouldContains('-', valid_msg=valid_msg2), 'name3': [StrShouldContains('-', valid_msg=valid_msg1), StrShouldContains('-', valid_msg=valid_msg2)] } } validator = Validator(vmap) result_map = validator.validate(data) map_msgs = ValidationSerialized().map_msgs(result_map, root=True) assert 'name.name1' in map_msgs.keys() assert 'name.name2' in map_msgs.keys() assert 'name.name3' in map_msgs.keys() assert map_msgs['name.name1'] == valid_msg1 assert map_msgs['name.name2'] == valid_msg2 assert map_msgs['name.name3'][0] == valid_msg1 assert map_msgs['name.name3'][1] == valid_msg2
def test_validate_serialized(self): valid_msg1 = 'Is Valid 1' valid_msg2 = 'Is Valid 2' data = { 'num': { 'num1': 123, 'num2': 232, 'num3': 33, } } vmap = { 'num': { 'num1': ShouldBeInt(valid_msg=valid_msg1), 'num2': ShouldBeInt(valid_msg=valid_msg2), 'num3': [ShouldBeInt(valid_msg=valid_msg1)] } } validator = Validator(vmap) result_map = validator.validate(data) map_msgs = ValidationSerialized().map_msgs(result_map, root=True) assert 'num.num1' in map_msgs.keys() assert 'num.num2' in map_msgs.keys() assert 'num.num3' in map_msgs.keys() assert map_msgs['num.num1'] == valid_msg1 assert map_msgs['num.num2'] == valid_msg2 assert map_msgs['num.num3'][0] == valid_msg1
def test_validate_serialized(self): valid_msg1 = 'Is Valid 1' valid_msg2 = 'Is Valid 2' data = { 'name': { 'name1': 'abc-abc-abc-abc-abc-abc', 'name2': 'abc-abc', 'name3': 'abc-abc' } } vmap = { 'name': { 'name1': CanNotBeAEmptyStr(valid_msg=valid_msg1), 'name2': CanNotBeAEmptyStr(valid_msg=valid_msg2), 'name3': [ CanNotBeAEmptyStr(valid_msg=valid_msg1), CanNotBeAEmptyStr(valid_msg=valid_msg2) ] } } validator = Validator(vmap) result_map = validator.validate(data) vmp = ValidationSerialized().map_msgs(result_map, root=True) assert 'name.name1' in vmp.keys() assert 'name.name2' in vmp.keys() assert 'name.name3' in vmp.keys() assert vmp['name.name1'] == valid_msg1 assert vmp['name.name2'] == valid_msg2 assert vmp['name.name3'][0] == valid_msg1 assert vmp['name.name3'][1] == valid_msg2
def test_not_validate_if_absent_field(self): data = {'num': 123} vmap = {'num1': ShouldBeInt(required=False)} validator = Validator(vmap) result_map = validator.validate(data) assert 'num1' in result_map.keys() assert result_map['num1'].valid is None assert result_map['num1'].field_name == 'num1'
def test_not_validate_if_absent_field(self): data = {'name': 'abcabc'} vmap = {'name1': ShouldContainsOnlyChars(required=False)} validator = Validator(vmap) result_map = validator.validate(data) assert 'name1' in result_map.keys() assert result_map['name1'].valid is None assert result_map['name1'].field_name is 'name1'
def test_not_validate_if_absent_field(self): data = {'name': 'abcabc'} vmap = {'name1': CanNotBeAEmptyStr(required=False)} validator = Validator(vmap) result_map = validator.validate(data) assert 'name1' in result_map.keys() assert result_map['name1'].valid is None assert result_map['name1'].field_name is 'name1'
def test_validate_valid(self): data = {'name': 'abcd'} vmap = {'name': MaxLengthStr(4)} validator = Validator(vmap) result_map = validator.validate(data) assert 'name' in result_map.keys() assert isinstance(result_map['name'], ValidationResult) assert result_map['name'].field_name == 'name' assert result_map['name'].valid
def test_validate_valid(self): data = {'num': 1} vmap = {'num': ShouldBeInt()} validator = Validator(vmap) result_map = validator.validate(data) assert 'num' in result_map.keys() assert isinstance(result_map['num'], ValidationResult) assert result_map['num'].field_name == 'num' assert result_map['num'].valid
def test_validate_valid(self): data = {'name': 'abc'} vmap = {'name': CanNotBeAEmptyStr()} validator = Validator(vmap) result_map = validator.validate(data) assert 'name' in result_map.keys() assert isinstance(result_map['name'], ValidationResult) assert result_map['name'].field_name == 'name' assert result_map['name'].valid
def test_validate_valid(self): data = {'name': 'abc'} vmap = {'name': ShouldContainsOnlyChars()} validator = Validator(vmap) result_map = validator.validate(data) assert 'name' in result_map.keys() assert isinstance(result_map['name'], ValidationResult) assert result_map['name'].field_name == 'name' assert result_map['name'].valid
def test_validate_invalid_catch(self): exc_msg = 'Testing' data = {'name': 'abc'} vmap = {'name': Catch(ShouldBeInt(exc=CustomException(exc_msg)))} validator = Validator(vmap) result_map = validator.validate(data) assert 'name' in result_map.keys() assert result_map['name'].field_name == 'name' assert result_map['name'].valid is False assert result_map['name'].msg == exc_msg
def test_map_msgs_root_true_only_not_validated(self): data = {'name': 'abcabc'} vmap = {'name1': StrShouldContains('-', required=False)} validator = Validator(vmap) result_map = validator.validate(data) map_msg = ValidationSerialized().map_msgs(result_map, root=True, only=NOT_VALIDATED) assert 'name1' in map_msg.keys() assert map_msg[ 'name1'] in 'Not Validated. Field "name1" does not exists.'
def test_validate_absent_field_cacth(self): exc_msg = 'Testing' data = {'name_diff': ''} vmap = { 'name': Catch(CanNotBeNone(required_exc=CustomException(exc_msg))) } validator = Validator(vmap) result_map = validator.validate(data) assert 'name' in result_map.keys() assert result_map['name'].field_name == 'name' assert result_map['name'].valid is False assert result_map['name'].msg == exc_msg
def test_validate_invalid_custom_exc(self): data = {'num': 'abc'} vmap = {'num': ShouldBeInt(exc=CustomException('Testing'))} validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)
def test_validate_inexisting_field(self): data = {'name_diff': 'abcabc'} vmap = {'name': CanNotBeAEmptyStr()} validator = Validator(vmap) with pytest.raises(FieldDoesNotExistsExc): validator.validate(data)
def test_validate_invalid_custom_exc(self): data = {'name': ''} vmap = {'name': CanNotBeAEmptyStr(exc=CustomException('Testing'))} validator = Validator(vmap) with pytest.raises(CustomException): validator.validate(data)