def test_validate_dict_without_constraints(self): msg = attributes._validate_dict({}) self.assertIsNone(msg) # Validate a dictionary without constraints. msg = attributes._validate_dict({'key': 'value'}) self.assertIsNone(msg)
def test_validate_dict(self): for value in (None, True, '1', []): self.assertEquals(attributes._validate_dict(value), "'%s' is not a dictionary" % value) msg = attributes._validate_dict({}) self.assertIsNone(msg) msg = attributes._validate_dict({'key': 'value'}) self.assertIsNone(msg)
def _validate_device_list(data, valid_values=None): """Validate the list of service definitions.""" if not data: # Devices must be provided msg = _("Cannot create a gateway with an empty device list") return msg try: for device in data: key_specs = {DEVICE_ID_ATTR: {'type:regex': attributes.UUID_PATTERN, 'required': True}, IFACE_NAME_ATTR: {'type:string': None, 'required': False}} err_msg = attributes._validate_dict( device, key_specs=key_specs) if err_msg: return err_msg unexpected_keys = [key for key in device if key not in key_specs] if unexpected_keys: err_msg = ("Unexpected keys found in device description:%s", ",".join(unexpected_keys)) return err_msg except TypeError: return (_("%s: provided data are not iterable") % _validate_device_list.__name__)
def _validate_device_list(data, valid_values=None): """Validate the list of service definitions.""" if not data: # Devices must be provided msg = _("Cannot create a gateway with an empty device list") return msg try: for device in data: key_specs = { DEVICE_ID_ATTR: { 'type:regex': attributes.UUID_PATTERN, 'required': True }, IFACE_NAME_ATTR: { 'type:string': None, 'required': False } } err_msg = attributes._validate_dict(device, key_specs=key_specs) if err_msg: return err_msg unexpected_keys = [key for key in device if key not in key_specs] if unexpected_keys: err_msg = ("Unexpected keys found in device description:%s", ",".join(unexpected_keys)) return err_msg except TypeError: return (_("%s: provided data are not iterable") % _validate_device_list.__name__)
def test_validate_dict_not_required_keys(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary['key2'] msg = attributes._validate_dict(dictionary, constraints) self.assertIsNone(msg, 'Field that was not required by the specs was' 'required by the validator.')
def test_subdictionary(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary['key3']['k4'] dictionary['key3']['k5'] = 'a string value' msg = attributes._validate_dict(dictionary, constraints) self.assertIn('Expected keys:', msg)
def _validate_device_list(data, valid_values=None): """ Validate the list of service definitions. """ if not data: # Devices must be provided msg = _("Cannot create a gateway with an empty device list") return msg try: for device in data: err_msg = attributes._validate_dict( device, key_specs={ DEVICE_ID_ATTR: {"type:regex": attributes.UUID_PATTERN, "required": True}, IFACE_NAME_ATTR: {"type:string": None, "required": False}, }, ) if err_msg: return err_msg except TypeError: return _("%s: provided data are not iterable") % _validate_device_list.__name__
def _validate_device_list(data, valid_values=None): """ Validate the list of service definitions. """ if not data: # Devices must be provided msg = _("Cannot create a gateway with an empty device list") return msg try: for device in data: err_msg = attributes._validate_dict( device, key_specs={DEVICE_ID_ATTR: {'type:regex': attributes.UUID_PATTERN, 'required': True}, IFACE_NAME_ATTR: {'type:string': None, 'required': False}}) if err_msg: return err_msg except TypeError: return (_("%s: provided data are not iterable") % _validate_device_list.__name__)
def test_validate_dict_wrong_values(self): dictionary, constraints = self._construct_dict_and_constraints() dictionary['key1'] = 'UNSUPPORTED' msg = attributes._validate_dict(dictionary, constraints) self.assertIsNotNone(msg)
def test_validate_dict_required_keys(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary['key1'] msg = attributes._validate_dict(dictionary, constraints) self.assertIn('Expected keys:', msg)
def test_validate_dict_with_invalid_validator(self): dictionary, constraints = self._construct_dict_and_constraints() constraints['key1'] = {'type:unsupported': None, 'required': True} msg = attributes._validate_dict(dictionary, constraints) self.assertEqual(msg, "Validator 'type:unsupported' does not exist.")
def test_validate_a_valid_dict_with_constraints(self): dictionary, constraints = self._construct_dict_and_constraints() msg = attributes._validate_dict(dictionary, constraints) self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_dict_type(self): for value in (None, True, '1', []): self.assertEqual(attributes._validate_dict(value), "'%s' is not a dictionary" % value)