Exemplo n.º 1
0
async def test_event_tree_parent_node_event():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    await asyncio.gather(*[
        async_event_tree_set_node(event_tree, 2, None, created_node),
        event_tree.root.node_event.wait()
    ])
Exemplo n.º 2
0
async def test_event_tree_get_new_relative_node():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    relative_created_node = event_tree.get_or_create_node(
        ["test-relative"], starting_node=created_node)
    get_node_result = event_tree.get_or_create_node(["test", "test-relative"])
    assert relative_created_node is get_node_result
Exemplo n.º 3
0
async def test_event_tree_set_node():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    event_tree.set_node(1, None, created_node)
    assert created_node.node_value == 1
    assert created_node.node_type is None
    event_tree.set_node(5, None, created_node, timestamp=10)
    assert created_node.node_value == 5
    assert created_node.node_type is None
    assert created_node.node_value_time == 10
Exemplo n.º 4
0
 def __init__(self):
     """
     Initialize the matrix as an EventTree instance
     """
     self.matrix = EventTree()
Exemplo n.º 5
0
class Matrix(Singleton):
    """
    Matrix dataclass store tentacles data in a EventTree
    """
    __slots__ = ['matrix']

    def __init__(self):
        """
        Initialize the matrix as an EventTree instance
        """
        self.matrix = EventTree()

    def set_tentacle_value(self, value, value_type, value_path):
        self.matrix.set_node_at_path(value, value_type, value_path)

    def get_node_children_at_path(self, node_path, starting_node=None):
        try:
            return list(
                self.matrix.get_node(
                    node_path, starting_node=starting_node).children.values())
        except NodeExistsError:
            return []

    def get_node_at_path(self, node_path, starting_node=None):
        try:
            return self.matrix.get_node(node_path, starting_node=starting_node)
        except NodeExistsError:
            return None

    def get_tentacle_nodes(self,
                           exchange_name=None,
                           tentacle_type=None,
                           tentacle_name=None):
        """
        Returns the list of nodes related to the exchange_name, tentacle_type and tentacle_name, ignored if None
        :param exchange_name: the exchange name to search for in the matrix
        :param tentacle_type: the tentacle type to search for in the matrix
        :param tentacle_name: the tentacle name to search for in the matrix
        :return: nodes linked to the given params
        """
        return self.get_node_children_at_path(
            get_tentacle_path(exchange_name=exchange_name,
                              tentacle_type=tentacle_type,
                              tentacle_name=tentacle_name))

    def get_tentacles_value_nodes(self,
                                  tentacle_nodes,
                                  symbol=None,
                                  time_frame=None):
        """
        Returns the list of nodes related to the symbol and / or time_frame from the given tentacle_nodes list
        :param tentacle_nodes: the exchange name to search for in the matrix
        :param symbol: the symbol to search for in the given node list
        :param time_frame: the time frame to search for in the given nodes list
        :return: nodes linked to the given params
        """
        return [
            node_at_path for node_at_path in [
                self.get_node_at_path(get_tentacle_value_path(
                    symbol=symbol, time_frame=time_frame),
                                      starting_node=n) for n in tentacle_nodes
            ] if node_at_path is not None
        ]
Exemplo n.º 6
0
async def test_event_tree_set_node_at_path():
    event_tree = EventTree()
    event_tree.set_node_at_path("test-string", "test-type",
                                ["test", "test2", "test3"])
    assert event_tree.get_or_create_node(["test"])
    assert event_tree.get_or_create_node(["test", "test2"])
    assert event_tree.get_or_create_node(["test", "test2", "test3"])
    assert event_tree.get_or_create_node(["test"]).children
    assert event_tree.get_or_create_node(["test", "test2"]).children
    assert not event_tree.get_or_create_node(["test", "test2", "test3"
                                              ]).children
    assert event_tree.get_or_create_node(["test", "test2",
                                          "test3"]).node_value == "test-string"
    assert event_tree.get_or_create_node(["test", "test2",
                                          "test3"]).node_type == "test-type"
Exemplo n.º 7
0
async def test_event_tree_get_not_existing_node():
    event_tree = EventTree()
    with pytest.raises(NodeExistsError):
        assert event_tree.get_node(["test"]) is None
Exemplo n.º 8
0
async def test_event_tree_get_existing_node():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    get_node_result = event_tree.get_or_create_node(["test"])
    assert created_node is get_node_result
Exemplo n.º 9
0
async def test_event_tree_get_new_node():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    assert event_tree.root.children == {"test": created_node}
Exemplo n.º 10
0
def test_event_tree_init():
    assert EventTree()
Exemplo n.º 11
0
async def test_event_tree_clear_node_event():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    with patch('asyncio.Event.clear') as event_clear:
        event_tree.set_node(2, None, created_node)
        asyncio.get_event_loop().call_soon(event_clear.assert_called_once)
Exemplo n.º 12
0
async def test_event_tree_set_node_event():
    event_tree = EventTree()
    created_node = event_tree.get_or_create_node(["test"])
    with patch('asyncio.Event.set') as event_set:
        event_tree.set_node(2, None, created_node)
        event_set.assert_called_once()