def __init__(self, message, model_or_kind, model_id=None): """Initializes a new audit error. Args: message: str. The message describing the error. model_or_kind: Model|bytes. If model_id is not provided, then this is a model (type: BaseModel). Otherwise, this is a model's kind (type: bytes). model_id: bytes|None. The model's ID, or None when model_or_kind is a model. Raises: TypeError. When the input message is not a string. ValueError. When the input message is empty. """ if not isinstance(message, str): raise TypeError('message must be a string') if not message: raise ValueError('message must be a non-empty string') if model_id is None: model_id = job_utils.get_model_id(model_or_kind) model_kind = job_utils.get_model_kind(model_or_kind) else: model_kind = model_or_kind error_message = '%s in %s(id=%s): %s' % ( self.__class__.__name__, model_kind, utils.quoted(model_id), message) super(BaseAuditError, self).__init__(stderr=error_message)
def _generate_missing_key_errors(self, model, property_of_model, referenced_kinds): """Yields all model keys referenced by the given model's properties. Args: model: Model. The input model. property_of_model: ModelProperty. The property that holds the ID(s) of referenced model(s). referenced_kinds: tuple(str). The kinds of models that the property refers to. Yields: tuple(ModelKey, ModelRelationshipError). The key for a referenced model and the error to report when the key doesn't exist. """ # NOTE: This loop yields 1 or many values, depending on whether the # property is a repeated property (i.e. a list). for property_value in property_of_model.yield_value_from_model(model): if property_value is None: continue model_id = job_utils.get_model_id(model) referenced_id = property_value for referenced_kind in referenced_kinds: error = base_validation_errors.ModelRelationshipError( property_of_model, model_id, referenced_kind, referenced_id) yield (ModelKey(referenced_kind, referenced_id), error)
def from_model(cls, model): """Creates a model key from the given model. Args: model: Model. The model to create a key for. Returns: ModelKey. The corresponding model key. """ return cls(model_kind=job_utils.get_model_kind(model), model_id=job_utils.get_model_id(model))
def test_get_id_from_bad_value(self) -> None: with self.assertRaisesRegexp(TypeError, 'not a model instance'): # type: ignore[no-untyped-call] job_utils.get_model_id(123) # type: ignore[arg-type]
def test_get_id_from_datastore_model(self) -> None: model = FooModel(id='123') self.assertEqual(job_utils.get_model_id(model), '123')