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 test_message(self) -> None: error = base_validation_errors.ModelRelationshipError( model_property.ModelProperty(FooModel, FooModel.bar_id), '123', 'BarModel', '123') self.assertEqual( error.stderr, 'ModelRelationshipError in FooModel(id="123"): ' 'FooModel.bar_id="123" should correspond to the ID of an ' 'existing BarModel, but no such model exists')
def test_reports_error_when_id_property_target_does_not_exist(self): self.put_multi([ # UserEmailPreferencesModel.id -> UserSettingsModel.id. self.create_model(user_models.UserEmailPreferencesModel, id=self.VALID_USER_ID), # UserSettingsModel missing. ]) self.assert_job_output_is([ base_validation_errors.ModelRelationshipError( model_property.ModelProperty( user_models.UserEmailPreferencesModel, user_models.UserEmailPreferencesModel.id), self.VALID_USER_ID, 'UserSettingsModel', self.VALID_USER_ID), ])
def test_reports_missing_id_property_target_even_if_sibling_property_is_valid(self): # pylint: disable=line-too-long self.put_multi([ self.create_model( auth_models.UserAuthDetailsModel, id=self.VALID_USER_ID, # Value is not None, so UserIdentifiersModel must exist. gae_id='abc', # Value is None, so missing UserIdByFirebaseAuthIdModel is OK. firebase_auth_id=None), self.create_model( auth_models.UserIdentifiersModel, user_id=self.VALID_USER_ID, # Should be gae_id='abc', so error will occur. id='123'), ]) self.assert_job_output_is([ base_validation_errors.ModelRelationshipError( model_property.ModelProperty( auth_models.UserAuthDetailsModel, auth_models.UserAuthDetailsModel.gae_id), self.VALID_USER_ID, 'UserIdentifiersModel', 'abc'), ])