def test_validate_command_choices_dictionary_list_response( self, monkeypatch, validator ): process_mock = _process_mock(monkeypatch, return_value='[{"value": "value"}]') request = Request( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", instance_name="instance_name", ) command = Mock( parameters=[ Parameter( key="key1", type="String", choices=Choices(type="command", value="command_name", strict=True), optional=False, ) ] ) validator.get_and_validate_parameters(request, command) choices_request = process_mock.call_args[0][0] assert choices_request.command == "command_name" assert choices_request.system == "foo" assert choices_request.system_version == "0.0.1" assert choices_request.instance_name == "instance_name"
def test_validate_command_choices_bad_value_type(self, monkeypatch, validator): _process_mock(monkeypatch, return_value='["value"]') request = Request( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", instance_name="instance_name", ) command = Mock( parameters=[ Parameter( key="key1", type="String", optional=False, choices=Choices( type="command", value=1, strict=True, ), ) ] ) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(request, command)
def test_repr(self): choices = Choices(type="static", display="select", value=[1], strict=True) assert "static" in repr(choices) assert "select" in repr(choices) assert "[1]" in repr(choices)
def test_repr(self): choices = Choices(type='static', display='select', value=[1], strict=True) assert 'static' in repr(choices) assert 'select' in repr(choices) assert '[1]' in repr(choices)
def choices_parameter(parameter_dict, choices_dict): """Parameter with a different choices""" # Change the choices value choices_copy = copy.deepcopy(choices_dict) choices_copy["value"] = ["choiceA", "choiceB", "choiceC"] dict_copy = copy.deepcopy(parameter_dict) dict_copy["parameters"] = [Parameter(**dict_copy["parameters"][0])] dict_copy["choices"] = Choices(**choices_copy) return Parameter(**dict_copy)
def test_validate_command_choices_dict_value(self, monkeypatch, validator): process_mock = _process_mock(monkeypatch, return_value='["value"]') request = Request( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", namespace="default", instance_name="instance_name", ) choices_value = { "command": "command_name", "system": "foo", "namespace": "default", "version": "0.0.1", "instance_name": "default", } command = Mock( parameters=[ Parameter( key="key1", type="String", optional=False, choices=Choices( type="command", value=choices_value, strict=True, ), ) ] ) validator.get_and_validate_parameters(request, command) choices_request = process_mock.call_args[0][0] assert choices_request.command == "command_name" assert choices_request.system == "foo" assert choices_request.namespace == "default" assert choices_request.system_version == "0.0.1" assert choices_request.instance_name == "default"
def process_choices(choices): # type: (Union[dict, str, Iterable, Callable]) -> Optional[Choices] """Process a choices definition into a usable Choices object Args: choices: Raw choices definition, usually from a decorator Returns: Choices: Dictionary that fully describes a choices specification """ if choices is None or isinstance(choices, Choices): return choices # If choices is a Callable, call it if callable(choices): choices = choices() if isinstance(choices, dict): if not choices.get("value"): raise PluginParamError( "No 'value' provided for choices. You must at least " "provide valid values.") # Again, if value is a Callable, call it value = choices.get("value") if callable(value): value = value() display = choices.get("display", _determine_display(value)) choice_type = choices.get("type") strict = choices.get("strict", True) if choice_type is None: choice_type = _determine_type(value) elif choice_type not in Choices.TYPES: raise PluginParamError( "Invalid choices type '%s' - Valid type options are %s" % (choice_type, Choices.TYPES)) else: if ((choice_type == "command" and not isinstance(value, (six.string_types, dict))) or (choice_type == "url" and not isinstance(value, six.string_types)) or (choice_type == "static" and not isinstance(value, (list, dict)))): allowed_types = { "command": "('string', 'dictionary')", "url": "('string')", "static": "('list', 'dictionary)", } raise PluginParamError( "Invalid choices value type '%s' - Valid value types for " "choice type '%s' are %s" % (type(value), choice_type, allowed_types[choice_type])) if display not in Choices.DISPLAYS: raise PluginParamError( "Invalid choices display '%s' - Valid display options are %s" % (display, Choices.DISPLAYS)) elif isinstance(choices, str): value = choices display = _determine_display(value) choice_type = _determine_type(value) strict = True else: try: # Assume some sort of iterable value = list(choices) except TypeError: raise PluginParamError( "Invalid 'choices': must be a string, dictionary, or iterable." ) display = _determine_display(value) choice_type = _determine_type(value) strict = True # Now parse out type-specific aspects unparsed_value = "" try: if choice_type == "command": if isinstance(value, six.string_types): unparsed_value = value else: unparsed_value = value["command"] details = parse(unparsed_value, parse_as="func") elif choice_type == "url": unparsed_value = value details = parse(unparsed_value, parse_as="url") else: if isinstance(value, dict): unparsed_value = choices.get("key_reference") if unparsed_value is None: raise PluginParamError( "Specifying a static choices dictionary requires a " '"key_reference" field with a reference to another ' 'parameter ("key_reference": "${param_key}")') details = { "key_reference": parse(unparsed_value, parse_as="reference") } else: details = {} except ParseError: raise PluginParamError( "Invalid choices definition - Unable to parse '%s'" % unparsed_value) return Choices(type=choice_type, display=display, value=value, strict=strict, details=details)
def bg_choices(choices_dict): return Choices(**choices_dict)
def test_str(self): assert "value" == str(Choices(value="value"))
def _format_choices(choices): def determine_display(display_value): if isinstance(display_value, six.string_types): return 'typeahead' return 'select' if len(display_value) <= 50 else 'typeahead' def determine_type(type_value): if isinstance(type_value, (list, dict)): return 'static' elif type_value.startswith('http'): return 'url' else: return 'command' if not choices: return None if not isinstance(choices, (list, six.string_types, dict)): raise PluginParamError( "Invalid 'choices' provided. Must be a list, dictionary or string." ) elif isinstance(choices, dict): if not choices.get('value'): raise PluginParamError( "No 'value' provided for choices. You must at least " "provide valid values.") value = choices.get('value') display = choices.get('display', determine_display(value)) choice_type = choices.get('type') strict = choices.get('strict', True) if choice_type is None: choice_type = determine_type(value) elif choice_type not in Choices.TYPES: raise PluginParamError( "Invalid choices type '%s' - Valid type options are %s" % (choice_type, Choices.TYPES)) else: if (choice_type == 'command' and not isinstance(value, (six.string_types, dict))) \ or (choice_type == 'url' and not isinstance(value, six.string_types)) \ or (choice_type == 'static' and not isinstance(value, (list, dict))): allowed_types = { 'command': "('string', 'dictionary')", 'url': "('string')", 'static': "('list', 'dictionary)" } raise PluginParamError( "Invalid choices value type '%s' - Valid value types for " "choice type '%s' are %s" % (type(value), choice_type, allowed_types[choice_type])) if display not in Choices.DISPLAYS: raise PluginParamError( "Invalid choices display '%s' - Valid display options are %s" % (display, Choices.DISPLAYS)) else: value = choices display = determine_display(value) choice_type = determine_type(value) strict = True # Now parse out type-specific aspects unparsed_value = '' try: if choice_type == 'command': if isinstance(value, six.string_types): unparsed_value = value else: unparsed_value = value['command'] details = parse(unparsed_value, parse_as='func') elif choice_type == 'url': unparsed_value = value details = parse(unparsed_value, parse_as='url') else: if isinstance(value, dict): unparsed_value = choices.get('key_reference') if unparsed_value is None: raise PluginParamError( 'Specifying a static choices dictionary requires a ' '"key_reference" field with a reference to another ' 'parameter ("key_reference": "${param_key}")') details = { 'key_reference': parse(unparsed_value, parse_as='reference') } else: details = {} except ParseError: raise PluginParamError( "Invalid choices definition - Unable to parse '%s'" % unparsed_value) return Choices(type=choice_type, display=display, value=value, strict=strict, details=details)
def __repr__(self): return BrewtilsChoices.__repr__(self)
def __str__(self): return BrewtilsChoices.__str__(self)
def _format_choices(choices): def determine_display(display_value): if isinstance(display_value, six.string_types): return "typeahead" return "select" if len(display_value) <= 50 else "typeahead" def determine_type(type_value): if isinstance(type_value, (list, dict)): return "static" elif type_value.startswith("http"): return "url" else: return "command" if not choices: return None if not isinstance(choices, (list, six.string_types, dict)): raise PluginParamError( "Invalid 'choices' provided. Must be a list, dictionary or string." ) elif isinstance(choices, dict): if not choices.get("value"): raise PluginParamError( "No 'value' provided for choices. You must at least " "provide valid values.") value = choices.get("value") display = choices.get("display", determine_display(value)) choice_type = choices.get("type") strict = choices.get("strict", True) if choice_type is None: choice_type = determine_type(value) elif choice_type not in Choices.TYPES: raise PluginParamError( "Invalid choices type '%s' - Valid type options are %s" % (choice_type, Choices.TYPES)) else: if ((choice_type == "command" and not isinstance(value, (six.string_types, dict))) or (choice_type == "url" and not isinstance(value, six.string_types)) or (choice_type == "static" and not isinstance(value, (list, dict)))): allowed_types = { "command": "('string', 'dictionary')", "url": "('string')", "static": "('list', 'dictionary)", } raise PluginParamError( "Invalid choices value type '%s' - Valid value types for " "choice type '%s' are %s" % (type(value), choice_type, allowed_types[choice_type])) if display not in Choices.DISPLAYS: raise PluginParamError( "Invalid choices display '%s' - Valid display options are %s" % (display, Choices.DISPLAYS)) else: value = choices display = determine_display(value) choice_type = determine_type(value) strict = True # Now parse out type-specific aspects unparsed_value = "" try: if choice_type == "command": if isinstance(value, six.string_types): unparsed_value = value else: unparsed_value = value["command"] details = parse(unparsed_value, parse_as="func") elif choice_type == "url": unparsed_value = value details = parse(unparsed_value, parse_as="url") else: if isinstance(value, dict): unparsed_value = choices.get("key_reference") if unparsed_value is None: raise PluginParamError( "Specifying a static choices dictionary requires a " '"key_reference" field with a reference to another ' 'parameter ("key_reference": "${param_key}")') details = { "key_reference": parse(unparsed_value, parse_as="reference") } else: details = {} except ParseError: raise PluginParamError( "Invalid choices definition - Unable to parse '%s'" % unparsed_value) return Choices(type=choice_type, display=display, value=value, strict=strict, details=details)
def test_str(self): assert 'value' == str(Choices(value='value'))