def test_simplified_literal_str(self) -> None: """Test simplified literal str.""" assert (VariableValueConcatenation([VariableValueLiteral("foo") ]).simplified.value == "foo") assert (VariableValueConcatenation( [VariableValueLiteral("foo"), VariableValueLiteral("bar")]).simplified.value == "foobar")
def test_init_find_handler_unknown_lookup_type(self) -> None: """Test __init__ fund handler UnknownLookupType.""" with pytest.raises(UnknownLookupType): VariableValueLookup( VariableValueLiteral("invalid"), VariableValueLiteral("query"), )
def test_init_find_handler_value_error(self) -> None: """Test __init__ fund handler ValueError.""" with pytest.raises(ValueError): VariableValueLookup( VariableValueLiteral("test"), VariableValueLiteral("query"), variable_type="invalid", # type: ignore )
def test_simplified_concat(self) -> None: """Test simplified concatenation.""" assert (VariableValueConcatenation([ VariableValueLiteral("foo"), VariableValueConcatenation( [VariableValueLiteral("bar"), VariableValueLiteral("foo")]), ]).simplified.value == "foobarfoo")
def test_init_find_handler_runway(self, mocker: MockerFixture) -> None: """Test __init__ find handler runway.""" mocker.patch.dict(RUNWAY_LOOKUP_HANDLERS, {"test": "success"}) obj = VariableValueLookup( VariableValueLiteral("test"), VariableValueLiteral("query"), variable_type="runway", ) assert obj.handler == "success"
def test_init(self) -> None: """Test __init__.""" name = VariableValueLiteral("test") query = VariableValueLiteral("query") obj = VariableValueLookup(name, query, MockLookupHandler, "runway") assert obj.handler == MockLookupHandler assert obj.lookup_name == name assert obj.lookup_query == query assert obj.variable_type == "runway"
def test_resolve_exception(self, mocker: MockerFixture) -> None: """Test resolve raise Exception.""" mocker.patch.object(MockLookupHandler, "handle", side_effect=Exception) obj = VariableValueLookup(VariableValueLiteral("test"), "query") with pytest.raises(FailedLookup) as excinfo: obj.resolve(MagicMock(), MagicMock()) assert isinstance(excinfo.value.cause, Exception) assert excinfo.value.lookup == obj
def test_dependencies(self, mocker: MockerFixture) -> None: """Test dependencies.""" mocker.patch.object(MockLookupHandler, "dependencies", return_value={"test"}) obj = VariableValueLookup(VariableValueLiteral("test"), "query", MockLookupHandler) assert obj.dependencies == {"test"}
def test_value(self) -> None: """Test value.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query") assert obj.resolved is False with pytest.raises(UnresolvedVariableValue): obj.value # pylint: disable=pointless-statement obj._resolve("success") assert obj.resolved is True assert obj.value == "success"
def test_dependencies_no_attr(self) -> None: """Test dependencies class has no attr.""" class FakeLookup: """Fake lookup.""" obj = VariableValueLookup( VariableValueLiteral("test"), "query", FakeLookup # type: ignore ) assert obj.dependencies == set()
def test_resolve(self, mocker: MockerFixture) -> None: """Test resolve.""" kwargs = { "context": MagicMock(), "provider": MagicMock(), "variables": MagicMock(), "kwarg": "something", } mock_handle = mocker.patch.object(MockLookupHandler, "handle", return_value="resolved") mock_resolve = mocker.patch.object(VariableValueLookup, "_resolve", return_value=None) mock_resolve_query = mocker.patch.object(VariableValueLiteral, "resolve") obj = VariableValueLookup(VariableValueLiteral("test"), VariableValueLiteral("query")) assert not obj.resolve(**kwargs) # type: ignore mock_resolve_query.assert_called_once_with(**kwargs) mock_handle.assert_called_once_with("query", **kwargs) mock_resolve.assert_called_once_with("resolved")
def test_value_multiple(self) -> None: """Test multiple.""" assert (VariableValueConcatenation( [VariableValueLiteral("foo"), VariableValueLiteral("bar")]).value == "foobar") assert (VariableValueConcatenation( [VariableValueLiteral(13), VariableValueLiteral("/test")] # type: ignore ).value == "13/test") assert (VariableValueConcatenation( [VariableValueLiteral(5), VariableValueLiteral(13)]).value == "513") with pytest.raises(InvalidLookupConcatenation): VariableValueConcatenation( [VariableValueLiteral(True), VariableValueLiteral("test")] # type: ignore ).value
def test_resolve(self, cfngin_context: MockCFNginContext, mocker: MockerFixture) -> None: """Test resolve.""" mock_provider = MagicMock() mock_resolve = mocker.patch.object(VariableValueLiteral, "resolve", return_value=None) obj = VariableValueConcatenation([VariableValueLiteral("val0")]) assert not obj.resolve( cfngin_context, provider=mock_provider, variables={"var": "something"}, # type: ignore kwarg="test", ) mock_resolve.assert_called_once_with( cfngin_context, provider=mock_provider, variables={"var": "something"}, kwarg="test", )
def test_repr(self, value: Union[int, str]) -> None: """Test __repr__.""" obj = VariableValueLiteral(value) # type: ignore assert repr(obj) == f"Literal[{value}]" # type: ignore
def test_simplified(self) -> None: """Test simplified.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query") assert obj.simplified == obj
def test_resolved(self) -> None: """Test resolved.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query") assert obj.resolved is False obj._resolved = True assert obj.resolved is True
def test_simplified_literal_bool(self) -> None: """Test simplified literal bool.""" assert (VariableValueConcatenation([VariableValueLiteral(True) ]).simplified.value is True) assert (VariableValueConcatenation([VariableValueLiteral(False) ]).simplified.value is False)
def test_iter(self) -> None: """Test __iter__.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query") assert list(iter(obj)) == [obj]
def test_repr(self) -> None: """Test __repr__.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query") assert repr(obj) == "Lookup[Literal[test] Literal[query]]" obj._resolve("resolved") assert repr(obj) == "Lookup[resolved (Literal[test] Literal[query])]"
def test_simplified_literal_int(self) -> None: """Test simplified literal int.""" assert (VariableValueConcatenation([VariableValueLiteral(13) ]).simplified.value == 13)
def test_resolved(self, value: Union[int, str]) -> None: """Test resolved.""" obj = VariableValueLiteral(value) # type: ignore assert obj.resolved
def test_value_single(self) -> None: """Test value single.""" assert VariableValueConcatenation([VariableValueLiteral("foo") ]).value == "foo" assert VariableValueConcatenation([VariableValueLiteral(13) ]).value == 13
def test_iter(self, value: Union[int, str]) -> None: """Test __iter__.""" obj = VariableValueLiteral(value) # type: ignore assert list(iter(obj)) == [obj] # type: ignore
def test_init_find_handler_cfngin(self, mocker: MockerFixture) -> None: """Test __init__ find handler cfngin.""" mocker.patch.dict(CFNGIN_LOOKUP_HANDLERS, {"test": "success"}) obj = VariableValueLookup(VariableValueLiteral("test"), VariableValueLiteral("query")) assert obj.handler == "success"
def test_init_convert_query(self) -> None: """Test __init__ convert query.""" obj = VariableValueLookup(VariableValueLiteral("test"), "query", MockLookupHandler, "runway") assert isinstance(obj.lookup_query, VariableValueLiteral) assert obj.lookup_query.value == "query"
def test_insert(self) -> None: """Test insert.""" obj = VariableValueList(["val0", "val1"]) obj.insert(1, VariableValueLiteral("val2")) assert [i.value for i in obj] == ["val0", "val2", "val1"]
def test_str(self) -> None: """Test __str__.""" assert (str(VariableValueLookup(VariableValueLiteral("test"), "query")) == "${test query}")
def test_simplified_literal_empty(self) -> None: """Test simplified literal empty.""" assert (VariableValueConcatenation([VariableValueLiteral("") ]).simplified.value == "")
def test_init(self, value: Union[int, str]) -> None: """Test __init__.""" obj = VariableValueLiteral(value) # type: ignore assert obj._data == value
def test_value(self, value: Union[int, str]) -> None: """Test value.""" obj = VariableValueLiteral(value) # type: ignore assert obj.value == value