Skip to content

Commit

Permalink
Change ancestor_of/descendant_of to `with_descendants/with_ancestor…
Browse files Browse the repository at this point in the history
…s` (#2278)

* Deprecate `ancestor_of` and `descendant_of`
* Replace all occurrences of `ancestor_of` with `with_descendants`
* Replace all occurrences of `descendant_of` with `with_ancestors`
* Remove `_beta` version of recursive `ancestor_of` and `descendant_of` joins
* Update docs with this convention
  • Loading branch information
ConradJohnston authored and sphuber committed Dec 4, 2018
1 parent 1adafc4 commit af61105
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 75 deletions.
20 changes: 10 additions & 10 deletions aiida/backends/djsite/db/subtests/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 0
)

Expand All @@ -155,7 +155,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 2
)

Expand All @@ -165,7 +165,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 2
)

Expand All @@ -175,7 +175,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 4
)

Expand All @@ -187,7 +187,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 2
)

Expand All @@ -196,7 +196,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 1
)
#~ self.assertEquals(
Expand All @@ -207,7 +207,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 0
)
#~ self.assertEquals(
Expand All @@ -218,13 +218,13 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n4.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 1
)
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n5.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n7.pk}
).append(Node, with_ancestors='anc', filters={'id':n7.pk}
).count(), 1
)
#~ self.assertEquals(
Expand All @@ -239,7 +239,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id':n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id':n8.pk}
).append(Node, with_ancestors='anc', filters={'id':n8.pk}
).count(), 1
)
#~ self.assertEquals(
Expand Down
4 changes: 2 additions & 2 deletions aiida/backends/general/abstractqueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_bands_and_parents_structure(self, args):
qb.append(orm.Group, tag="group", filters=group_filters,
group_of="bdata")

qb.append(StructureData, tag="sdata", ancestor_of="bdata",
qb.append(StructureData, tag="sdata", with_descendants="bdata",
# We don't care about the creator of StructureData
project=["id", "attributes.kinds", "attributes.sites"])

Expand Down Expand Up @@ -245,5 +245,5 @@ def get_all_parents(self, node_pks, return_values=('id',)):
qb = QueryBuilder()
qb.append(Node, tag='low_node',
filters={'id': {'in': node_pks}})
qb.append(Node, ancestor_of='low_node', project=return_values)
qb.append(Node, with_descendants='low_node', project=return_values)
return qb.all()
20 changes: 10 additions & 10 deletions aiida/backends/sqlalchemy/tests/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 0
)

Expand All @@ -66,7 +66,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 2
)

Expand All @@ -81,7 +81,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 2
)
# ~ self.assertEquals(
Expand All @@ -94,7 +94,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 4
)

Expand All @@ -111,7 +111,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 2
)

Expand All @@ -127,7 +127,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 1
)

Expand All @@ -143,7 +143,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 0
)

Expand All @@ -158,7 +158,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n4.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 1
)
# ~ self.assertEquals(
Expand All @@ -169,7 +169,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n5.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n7.pk}
).append(Node, with_ancestors='anc', filters={'id': n7.pk}
).count(), 1
)
# ~ self.assertEquals(
Expand All @@ -184,7 +184,7 @@ def test_creation_and_deletion(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 1
)
# ~ self.assertEquals(
Expand Down
28 changes: 14 additions & 14 deletions aiida/backends/tests/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,50 +878,50 @@ def test_query_path(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 0)

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}
).append(Node, with_descendants='desc', filters={'id': n1.pk}
).count(), 0)

n6.add_incoming(n5, link_type=LinkType.INPUT_CALC, link_label='link1')
# Yet, now 2 links from 1 to 8
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 2
)

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}
).append(Node, with_descendants='desc', filters={'id': n1.pk}
).count(), 2)

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}, edge_filters={'depth': {'<': 6}},
).append(Node, with_descendants='desc', filters={'id': n1.pk}, edge_filters={'depth': {'<': 6}},
).count(), 2)
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}, edge_filters={'depth': 5},
).append(Node, with_descendants='desc', filters={'id': n1.pk}, edge_filters={'depth': 5},
).count(), 2)
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}, edge_filters={'depth': {'<': 5}},
).append(Node, with_descendants='desc', filters={'id': n1.pk}, edge_filters={'depth': {'<': 5}},
).count(), 0)

# TODO write a query that can filter certain paths by traversed ID
qb = QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc',
).append(Node, ancestor_of='desc', edge_project='path', filters={'id': n1.pk})
).append(Node, with_descendants='desc', edge_project='path', filters={'id': n1.pk})
queried_path_set = set([frozenset(p) for p, in qb.all()])

paths_there_should_be = set([
Expand All @@ -934,7 +934,7 @@ def test_query_path(self):
qb = QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(
Node, descendant_of='anc', filters={'id': n8.pk}, edge_project='path'
Node, with_ancestors='anc', filters={'id': n8.pk}, edge_project='path'
)

self.assertTrue(set(
Expand All @@ -950,34 +950,34 @@ def test_query_path(self):
self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 2
)

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}
).append(Node, with_descendants='desc', filters={'id': n1.pk}
).count(), 2)
n9.add_incoming(n6, link_type=LinkType.INPUT_CALC, link_label='link6')
# And now there should be 4 nodes

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(Node, descendant_of='anc', filters={'id': n8.pk}
).append(Node, with_ancestors='anc', filters={'id': n8.pk}
).count(), 4)

self.assertEquals(
QueryBuilder().append(
Node, filters={'id': n8.pk}, tag='desc'
).append(Node, ancestor_of='desc', filters={'id': n1.pk}
).append(Node, with_descendants='desc', filters={'id': n1.pk}
).count(), 4)

qb = QueryBuilder().append(
Node, filters={'id': n1.pk}, tag='anc'
).append(
Node, descendant_of='anc', filters={'id': n8.pk}, edge_tag='edge'
Node, with_ancestors='anc', filters={'id': n8.pk}, edge_tag='edge'
)
qb.add_projection('edge', 'depth')
self.assertTrue(set(next(zip(*qb.all()))), set([5, 6]))
Expand Down
2 changes: 1 addition & 1 deletion aiida/orm/implementation/django/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _add_dblink_from(self, src, link_type, label):
# in the TC table from self to src
if QueryBuilder().append(
Node, filters={'id': self.pk}, tag='parent').append(
Node, filters={'id': src.pk}, tag='child', descendant_of='parent').count() > 0:
Node, filters={'id': src.pk}, tag='child', with_ancestors='parent').count() > 0:
raise ValueError(
"The link you are attempting to create would generate a loop")

Expand Down
4 changes: 2 additions & 2 deletions aiida/orm/implementation/general/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ def has_children(self):
from aiida.orm import Node
first_desc = QueryBuilder().append(
Node, filters={'id': self.pk}, tag='self').append(
Node, descendant_of='self', project='id').first()
Node, with_ancestors='self', project='id').first()
return bool(first_desc)

@property
Expand All @@ -2051,7 +2051,7 @@ def has_parents(self):
from aiida.orm import Node
first_ancestor = QueryBuilder().append(
Node, filters={'id': self.pk}, tag='self').append(
Node, ancestor_of='self', project='id').first()
Node, with_descendants='self', project='id').first()
return bool(first_ancestor)

# pylint: disable=no-self-argument
Expand Down
2 changes: 1 addition & 1 deletion aiida/orm/implementation/sqlalchemy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _add_dblink_from(self, src, link_type, label):
}, tag='parent').append(
Node, filters={
'id': src.pk
}, tag='child', descendant_of='parent').count() > 0:
}, tag='child', with_ancestors='parent').count() > 0:
raise ValueError("The link you are attempting to create would generate a loop")

self._do_create_link(src, label, link_type)
Expand Down
Loading

0 comments on commit af61105

Please sign in to comment.