コード例 #1
0
    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 python_utils.is_string(message):
            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)
コード例 #2
0
    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 = python_utils.convert_to_bytes(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)
コード例 #3
0
    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))
コード例 #4
0
ファイル: audit_errors.py プロジェクト: 15Shubh07/oppia
    def __init__(self, model_or_kind, model_id=None):
        """Initializes a new audit error.

        Args:
            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.
        """
        if model_id is not None:
            model_kind = model_or_kind
        else:
            model_id = job_utils.get_model_id(model_or_kind)
            model_kind = job_utils.get_model_kind(model_or_kind)
        # At first, self._message is a tuple of model identifiers that will be
        # used to annotate the _actual_ message provided by subclasses.
        self._message = (model_kind, model_id)
コード例 #5
0
ファイル: job_utils_test.py プロジェクト: sajalasati/oppia
 def test_get_id_from_bad_value(self):
     with self.assertRaisesRegexp(TypeError, 'not a model instance'):
         job_utils.get_model_id(123)
コード例 #6
0
ファイル: job_utils_test.py プロジェクト: sajalasati/oppia
    def test_get_id_from_datastore_model(self):
        model = FooModel(id='123')

        self.assertEqual(job_utils.get_model_id(model), '123')
コード例 #7
0
ファイル: job_utils_test.py プロジェクト: nlok5923/oppia
 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]
コード例 #8
0
 def test_get_id_from_cloud_datastore_entity(self):
     entity = cloud_datastore_types.Entity(
         key=cloud_datastore_types.Key('FooModel', '123', project='foo'))
     self.assertEqual(job_utils.get_model_id(entity), '123')