def test_create_placeholder_value(self, placeholder_type, argument, expected): class MyResolver(Resolver): def resolve(self): pass resolver = MyResolver(argument) with use_resolver_placeholders_on_error(): result = create_placeholder_value(resolver, placeholder_type) assert result == expected
def test_get__resolver_raises_error__placeholders_allowed__returns_placeholder( self): class ErroringResolver(Resolver): def resolve(self): raise ValueError() resolver = ErroringResolver() self.mock_object.resolvable_value_property = resolver with use_resolver_placeholders_on_error(): result = self.mock_object.resolvable_value_property assert result == create_placeholder_value(resolver, PlaceholderType.explicit)
def test_get__resolver_raises_error__placeholders_allowed__alternate_placeholder_type__uses_alternate_type( self): class ErroringResolver(Resolver): def resolve(self): raise ValueError() resolver = ErroringResolver() self.mock_object.value_with_none_placeholder = resolver with use_resolver_placeholders_on_error(): result = self.mock_object.value_with_none_placeholder assert result == create_placeholder_value(resolver, PlaceholderType.none)
def test_get__resolver_raises_error__placeholders_allowed__alternate_placeholder_type__uses_alternate( self): class ErroringResolver(Resolver): def resolve(self): raise ValueError() resolver = ErroringResolver() self.mock_object.container_with_alphanum_placeholder = { 'resolver': resolver } with use_resolver_placeholders_on_error(): result = self.mock_object.container_with_alphanum_placeholder assert result == { 'resolver': create_placeholder_value(resolver, PlaceholderType.alphanum) }
def resolve_resolver_value(self, resolver: 'Resolver') -> Any: """Returns the resolved parameter value. If the resolver happens to raise an error and placeholders are currently allowed for resolvers, a placeholder will be returned instead of reraising the error. :param resolver: The resolver to resolve. :return: The resolved value (or placeholder, in certain circumstances) """ try: return resolver.resolve() except RecursiveResolve: # Recursive resolve issues shouldn't be masked by a placeholder. raise except Exception: if are_placeholders_enabled(): placeholder_value = create_placeholder_value(resolver, self.placeholder_type) self.logger.debug( "Error encountered while resolving the resolver. This is allowed for the current " f"operation. Resolving it to a placeholder value instead: {placeholder_value}" ) return placeholder_value raise