def test_resolve_variable_provided_not_resolved(mocker: MockerFixture) -> None: """Test resolve_variable.""" mocker.patch("runway.variables.CFNGIN_LOOKUP_HANDLERS", {"mock": Mock()}) with pytest.raises(UnresolvedBlueprintVariable): resolve_variable( "name", {"type": str}, Variable("name", "${mock abc}", "cfngin"), "test" )
def test_resolve_variable_no_provided_without_default(self): """Test resolve variable no provided without default.""" with self.assertRaises(MissingVariable): var_name = "testVar" var_def = {"type": str} provided_variable = None blueprint_name = "testBlueprint" resolve_variable(var_name, var_def, provided_variable, blueprint_name)
def test_resolve_variable_provided_not_resolved(self): """Test resolve variable provided not resolved.""" var_name = "testVar" provided_variable = Variable(var_name, "${mock abc}", "cfngin") with self.assertRaises(UnresolvedVariable): var_def = {"type": str} blueprint_name = "testBlueprint" resolve_variable(var_name, var_def, provided_variable, blueprint_name)
def test_resolve_variable_no_type_on_variable_definition(self): """Test resolve variable no type on variable definition.""" with self.assertRaises(VariableTypeRequired): var_name = "testVar" var_def = {} provided_variable = None blueprint_name = "testBlueprint" resolve_variable(var_name, var_def, provided_variable, blueprint_name)
def test_variable_exists_but_value_is_none(self): """Test variable exists but value is none.""" var_name = "testVar" var_value = None provided_variable = Variable(var_name, var_value, "cfngin") with self.assertRaises(ValueError): var_def = {"type": str} blueprint_name = "testBlueprint" resolve_variable(var_name, var_def, provided_variable, blueprint_name)
def test_resolve_variable_allowed_values(self): """Test resolve variable allowed values.""" var_name = "testVar" var_def = {"type": str, "allowed_values": ["allowed"]} provided_variable = Variable(var_name, "not_allowed", "cfngin") blueprint_name = "testBlueprint" with self.assertRaises(ValueError): resolve_variable(var_name, var_def, provided_variable, blueprint_name) provided_variable = Variable(var_name, "allowed", "cfngin") value = resolve_variable(var_name, var_def, provided_variable, blueprint_name) self.assertEqual(value, "allowed")
def test_resolve_variable_allowed_values() -> None: """Test resolve_variable.""" var_name = "testVar" var_def: BlueprintVariableTypeDef = {"type": str, "allowed_values": ["allowed"]} with pytest.raises(ValueError): resolve_variable( var_name, var_def, Variable(var_name, "not_allowed", "cfngin"), "test" ) assert ( resolve_variable( var_name, var_def, Variable(var_name, "allowed", "cfngin"), "test" ) == "allowed" )
def test_resolve_variable_default() -> None: """Test resolve_variable.""" default_value = "foo" assert ( resolve_variable("name", {"default": default_value, "type": str}, None, "test") == default_value )
def _resolve_troposphere_var(self, tpe, value, **kwargs): """Resolve troposphere var.""" var_name = "testVar" var_def = {"type": TroposphereType(tpe, **kwargs)} provided_variable = Variable(var_name, value, "cfngin") blueprint_name = "testBlueprint" return resolve_variable(var_name, var_def, provided_variable, blueprint_name)
def resolve_troposphere_var(tpe: Any, value: Any, **kwargs: Any) -> Any: """Resolve troposphere var.""" return resolve_variable( "name", {"type": TroposphereType(tpe, **kwargs)}, Variable("name", value, "cfngin"), "test", )
def test_resolve_variable_validator_invalid_value() -> None: """Test resolve_variable.""" def triple_validator(value: Any) -> Any: if len(value) != 3: raise ValueError("Must be a triple.") return value var_name = "testVar" var_value = [1, 2] with pytest.raises(ValidatorError) as exc: resolve_variable( var_name, {"type": list, "validator": triple_validator}, Variable(var_name, var_value, "cfngin"), "", ) assert isinstance(exc.value.exception, ValueError)
def test_resolve_variable_validator_invalid_value(self): """Test resolve variable validator invalid value.""" def triple_validator(value): if len(value) != 3: raise ValueError("Must be a triple.") return value var_name = "testVar" var_def = {"type": list, "validator": triple_validator} var_value = [1, 2] provided_variable = Variable(var_name, var_value, "cfngin") blueprint_name = "testBlueprint" with self.assertRaises(ValidatorError) as result: resolve_variable(var_name, var_def, provided_variable, blueprint_name) exc = result.exception.exception # The wrapped exception self.assertIsInstance(exc, ValueError)
def test_resolve_variable_provided_resolved(self): """Test resolve variable provided resolved.""" var_name = "testVar" var_def = {"type": str} provided_variable = Variable(var_name, "${mock 1}", "cfngin") provided_variable.resolve(context=MagicMock(), provider=MagicMock()) blueprint_name = "testBlueprint" value = resolve_variable(var_name, var_def, provided_variable, blueprint_name) self.assertEqual(value, "1")
def test_resolve_variable_no_provided_with_default(self): """Test resolve variable no provided with default.""" var_name = "testVar" default_value = "foo" var_def = {"default": default_value, "type": str} provided_variable = None blueprint_name = "testBlueprint" value = resolve_variable(var_name, var_def, provided_variable, blueprint_name) self.assertEqual(default_value, value)
def test_resolve_variable_validator_valid_value(self): """Test resolve variable validator valid value.""" def triple_validator(value): if len(value) != 3: raise ValueError return value var_name = "testVar" var_def = {"type": list, "validator": triple_validator} var_value = [1, 2, 3] provided_variable = Variable(var_name, var_value, "cfngin") blueprint_name = "testBlueprint" value = resolve_variable(var_name, var_def, provided_variable, blueprint_name) self.assertEqual(value, var_value)
def test_resolve_variable_validator_valid_value() -> None: """Test resolve_variable.""" def triple_validator(value: Any) -> Any: if len(value) != 3: raise ValueError return value var_name = "testVar" var_value = [1, 2, 3] assert ( resolve_variable( var_name, {"type": list, "validator": triple_validator}, Variable(var_name, var_value, "cfngin"), "test", ) == var_value )
def test_resolve_variable_missing_variable() -> None: """Test resolve_variable raise MissingVariable.""" with pytest.raises(MissingVariable): resolve_variable("name", {"type": str}, None, "test")
def test_resolve_variable_no_type() -> None: """Test resolve_variable.""" with pytest.raises(VariableTypeRequired): resolve_variable("name", {}, None, "test")