Exemple #1
0
    async def test_remove_path_value(self):
        test = {}
        ObjectPath.set_path_value(test, "x.y.z", 15)
        ObjectPath.set_path_value(test, "x.p", "hello")
        ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"})
        ObjectPath.set_path_value(test, "x.a[1]", "yabba")
        ObjectPath.set_path_value(test, "x.a[0]", "dabba")

        ObjectPath.remove_path_value(test, "x.y.z")
        with self.assertRaises(KeyError):
            ObjectPath.get_path_value(test, "x.y.z")

        assert ObjectPath.get_path_value(test, "x.y.z", 99) == 99

        ObjectPath.remove_path_value(test, "x.a[1]")
        assert not ObjectPath.try_get_path_value(test, "x.a[1]")

        assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba"
Exemple #2
0
    async def test_set_value(self):
        test = {}
        ObjectPath.set_path_value(test, "x.y.z", 15)
        ObjectPath.set_path_value(test, "x.p", "hello")
        ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"})
        ObjectPath.set_path_value(test, "x.a[1]", "yabba")
        ObjectPath.set_path_value(test, "x.a[0]", "dabba")
        ObjectPath.set_path_value(test, "null", None)

        assert ObjectPath.get_path_value(test, "x.y.z") == 15
        assert ObjectPath.get_path_value(test, "x.p") == "hello"
        assert ObjectPath.get_path_value(test, "foo.bar") == 15

        assert not ObjectPath.try_get_path_value(test, "foo.Blatxxx")
        assert ObjectPath.try_get_path_value(test, "x.a[1]") == "yabba"
        assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba"

        assert not ObjectPath.try_get_path_value(test, "null")
    def _try_get_first_nested_value(remaining_path: str,
                                    memory: object) -> Tuple[bool, object]:
        # These modules are imported at static level to avoid circular dependency problems
        # pylint: disable=import-outside-toplevel

        from botbuilder.dialogs import ObjectPath

        array = ObjectPath.try_get_path_value(memory, remaining_path)
        if array:
            if isinstance(array[0], list):
                first = array[0]
                if first:
                    second = first[0]
                    return True, second

                return False, None

            return True, array[0]

        return False, None