def test_get_nested_values(): c = Context({"a": {"b": {"c": [18, 19], "a": "world"}, "a": "hello"}}) assert ["hello"] == c.get("a.a") assert ["world"] == c.get("a.b.a") assert ["{'c': [18, 19], 'a': 'world'}"] == c.get("a.b") assert ["18", "19"] == c.get("a.b.c")
def generate(self, ctx: Context = None): # type: ignore """ Substitutes the placeholder. Args: ctx (Optional[Context]): The generation context. Returns: A randomly selected value from the corresponding context key. """ if ctx is None: raise RuntimeError( f"could not get value for placeholder [{self.key}] - no context provided" ) try: val = ctx.get(self.key) except KeyError: raise RuntimeError( f"could not get value for placeholder [{self.key}] - key is missing" ) if not val: return "" return sub_punctuation(LiteralNode(random.choice(val))).generate()
def test_get_flat_values(): c = Context({ "hello": "world", "a": 42, "b": True, "c": 42.42, "rdm": ["a", "s", "d", "f"] }) assert ["world"] == c.get("hello") assert ["42"] == c.get("a") assert ["True"] == c.get("b") assert ["42.42"] == c.get("c") assert ["a", "s", "d", "f"] == c.get("rdm") with pytest.raises(KeyError): c.get("unknown")