def test_string_type_simple_true(self): _type = types.StringType(self.field) value = 'a string' self.assertEquals(_type.cast(value), value) value = u'a string' self.assertEquals(_type.cast(value), value)
def test_constraints_empty_with_no_value(self): '''Empty constraints object, with no value (empty string)''' value = '' field = self._make_default_field(type='string') _type = types.StringType(field) self.assertEqual(_type.cast(value), '')
def test_constraints_required_false_with_no_value(self): '''Required False with no value (empty string)''' value = '' field = self._make_default_field(type='string', constraints={'required': False}) _type = types.StringType(field) self.assertEqual(_type.cast(value), '')
def test_constraints_required_true_with_no_value(self): '''Required True with no value (empty string) returns empty string.''' value = '' field = self._make_default_field(type='string', constraints={'required': True}) _type = types.StringType(field) assert _type.cast(value) == value
def test_constraints_required_true_with_value(self): '''Required True with a value''' value = 'string' field = self._make_default_field(type='string', constraints={'required': True}) _type = types.StringType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_minlength_valid_value_equals(self): '''minLength with valid value equal to each other.''' value = 'string' field = self._make_default_field(type='string', constraints={'minLength': 6}) _type = types.StringType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_maxlength_valid_value(self): '''maxLength with valid value''' value = 'string' field = self._make_default_field(type='string', constraints={'maxLength': 7}) _type = types.StringType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_enum_valid_value(self): '''value is in enum array''' value = "bob" field = self._make_default_field( type='string', constraints={'enum': ['alice', 'bob', 'chuck']}) _type = types.StringType(field) self.assertEqual(_type.cast(value), value)
def test_invalid_email_fails(self): self.field['format'] = 'email' _type = types.StringType(self.field) value = 1 self.assertRaises(exceptions.InvalidCastError, _type.cast, value) value = 'notanemail' self.assertRaises(exceptions.InvalidEmail, _type.cast, value)
def test_constraints_pattern_valid_value(self): '''value is valid for pattern''' value = "078-05-1120" field = self._make_default_field( type='string', constraints={"pattern": "[0-9]{3}-[0-9]{2}-[0-9]{4}"}) _type = types.StringType(field) self.assertEqual(_type.cast(value), value)
def test_uuid_failure(self): self.field['format'] = 'uuid' _type = types.StringType(self.field) value = '1234567812345678123456781234567?' self.assertRaises(exceptions.InvalidUUID, _type.cast, value) value = '1234567812345678123456781234567' self.assertRaises(exceptions.InvalidUUID, _type.cast, value) value = 'X23e4567-e89b-12d3-a456-426655440000' self.assertRaises(exceptions.InvalidUUID, _type.cast, value)
def test_uuid(self): self.field['format'] = 'uuid' _type = types.StringType(self.field) value = '12345678123456781234567812345678' self.assertEqual(_type.cast(value), value) value = 'urn:uuid:12345678-1234-5678-1234-567812345678' self.assertEqual(_type.cast(value), value) value = '123e4567-e89b-12d3-a456-426655440000' self.assertEqual(_type.cast(value), value)
def test_constraints_maxlength_invalid_value(self): '''maxLength with invalid value''' value = 'string' field = self._make_default_field(type='string', constraints={'maxLength': 5}) _type = types.StringType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual(e.value.msg, "The field 'Name' must have a maximum length of 5")
def test_constraints_minlength_valid_value(self): '''maximum with unsupported type''' value = 'string' field = self._make_default_field(type='string', constraints={'maximum': 2}) _type = types.StringType(field) with pytest.raises(exceptions.ConstraintNotSupported) as e: _type.cast(value) self.assertEqual( e.value.msg, "Field type 'string' does not support " "the maximum constraint")
def test_constraints_enum_invalid_value_case_sensitive(self): '''value comparison is case sensitive''' value = "Bob" field = self._make_default_field( type='string', constraints={'enum': ['alice', 'bob', 'chuck']}) _type = types.StringType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must be in the enum array")
def test_constraints_pattern_invalid_value(self): '''value is invalid for pattern''' value = "078-05-112A" field = self._make_default_field( type='string', constraints={"pattern": "[0-9]{3}-[0-9]{2}-[0-9]{4}"}) _type = types.StringType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must match the pattern")
def test_valid_email(self): self.field['format'] = 'email' _type = types.StringType(self.field) value = '*****@*****.**' self.assertEquals(_type.cast(value), value) value = 'customer/[email protected]' self.assertEquals(_type.cast(value), value) value = '\[email protected]' self.assertEquals(_type.cast(value), value) value = '!def!xyz%[email protected]' self.assertEquals(_type.cast(value), value)
def test_uri_failure(self): self.field['format'] = 'uri' _type = types.StringType(self.field) value = 'notauri' self.assertRaises(exceptions.InvalidURI, _type.cast, value)
def test_uri(self): self.field['format'] = 'uri' _type = types.StringType(self.field) value = 'http://test.com' self.assertEqual(_type.cast(value), value)
def test_string_type_simple_false(self): _type = types.StringType(self.field) value = 1 self.assertRaises(exceptions.InvalidCastError, _type.cast, value)
def test_uri_empty_skip_constraints(self): self.field['format'] = 'uri' _type = types.StringType(self.field) assert _type.cast('', skip_constraints=True) == ''