Ejemplo n.º 1
0
    def test_ordering_limits_offsets_sqla(self):
        """Test ordering limits offsets of SQLA query results."""
        # Creating 10 nodes with an attribute that can be ordered
        for i in range(10):
            node = Data()
            node.set_attribute('foo', i)
            node.store()
        q_b = QueryBuilder().append(Node, project='attributes.foo').order_by(
            {Node: {
                'attributes.foo': {
                    'cast': 'i'
                }
            }})
        res = next(zip(*q_b.all()))
        self.assertEqual(res, tuple(range(10)))

        # Now applying an offset:
        q_b.offset(5)
        res = next(zip(*q_b.all()))
        self.assertEqual(res, tuple(range(5, 10)))

        # Now also applying a limit:
        q_b.limit(3)
        res = next(zip(*q_b.all()))
        self.assertEqual(res, tuple(range(5, 8)))
Ejemplo n.º 2
0
def link_triple_exists(source, target, link_type, link_label):
    """Return whether a link with the given type and label exists between the given source and target node.

    :param source: node from which the link is outgoing
    :param target: node to which the link is incoming
    :param link_type: the link type
    :param link_label: the link label
    :return: boolean, True if the link triple exists, False otherwise
    """
    from aiida.orm import Node, QueryBuilder

    # First check if the triple exist in the cache, in the case of an unstored target node
    if target._incoming_cache and LinkTriple(source, link_type, link_label) in target._incoming_cache:  # pylint: disable=protected-access
        return True

    # If either node is unstored (i.e. does not have a pk), the link cannot exist in the database, so no need to check
    if source.pk is None or target.pk is None:
        return False

    # Here we have two stored nodes, so we need to check if the same link already exists in the database.
    # Finding just a single match is sufficient so we can use the `limit` clause for efficiency
    builder = QueryBuilder()
    builder.append(Node, filters={'id': source.id}, project=['id'])
    builder.append(Node, filters={'id': target.id}, edge_filters={'type': link_type.value, 'label': link_label})
    builder.limit(1)

    return builder.count() != 0