ARIA-158 Restore support for relationship ordering
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/1f3e7ff4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/1f3e7ff4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/1f3e7ff4 Branch: refs/heads/ARIA-148-extra-cli-commands Commit: 1f3e7ff42fefcd5dc8191e3b0eeac0460db059b0 Parents: 5bc28b6 Author: max-orlov <ma...@gigaspaces.com> Authored: Thu Apr 27 17:09:14 2017 +0300 Committer: max-orlov <ma...@gigaspaces.com> Committed: Thu Apr 27 17:20:48 2017 +0300 ---------------------------------------------------------------------- aria/modeling/relationship.py | 8 +++++--- aria/modeling/service_instance.py | 27 ++++++++++++++++++++------- tests/modeling/test_mixins.py | 7 +++++-- 3 files changed, 30 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1f3e7ff4/aria/modeling/relationship.py ---------------------------------------------------------------------- diff --git a/aria/modeling/relationship.py b/aria/modeling/relationship.py index e6830b8..291d08c 100644 --- a/aria/modeling/relationship.py +++ b/aria/modeling/relationship.py @@ -14,7 +14,6 @@ # limitations under the License. # pylint: disable=invalid-name, redefined-outer-name - from sqlalchemy.orm import relationship, backref from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy import ( @@ -161,7 +160,8 @@ def one_to_many(model_class, child_table, child_fk=None, dict_key=None, - back_populates=None): + back_populates=None, + rel_kwargs=None): """ Declare a one-to-many relationship property. The property value would be a list or dict of instances of the child table's model. @@ -186,6 +186,8 @@ def one_to_many(model_class, false to disable :type back_populates: basestring|bool """ + rel_kwargs = rel_kwargs or {} + rel_kwargs.setdefault('cascade', 'all') if back_populates is None: back_populates = model_class.__tablename__ return _relationship( @@ -194,7 +196,7 @@ def one_to_many(model_class, back_populates=back_populates, other_fk=child_fk, dict_key=dict_key, - relationship_kwargs=dict(cascade='all')) + relationship_kwargs=rel_kwargs) def many_to_one(model_class, http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1f3e7ff4/aria/modeling/service_instance.py ---------------------------------------------------------------------- diff --git a/aria/modeling/service_instance.py b/aria/modeling/service_instance.py index 6d8f3fe..d5391ef 100644 --- a/aria/modeling/service_instance.py +++ b/aria/modeling/service_instance.py @@ -25,6 +25,7 @@ from sqlalchemy import ( from sqlalchemy import DateTime from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.declarative import declared_attr +from sqlalchemy.ext.orderinglist import ordering_list from .mixins import InstanceModelMixin from ..orchestrator import execution_plugin @@ -485,12 +486,22 @@ class NodeBase(InstanceModelMixin): @declared_attr def outbound_relationships(cls): return relationship.one_to_many( - cls, 'relationship', child_fk='source_node_fk', back_populates='source_node') + cls, 'relationship', child_fk='source_node_fk', back_populates='source_node', + rel_kwargs=dict( + order_by='Relationship.source_position', + collection_class=ordering_list('source_position', count_from=0) + ) + ) @declared_attr def inbound_relationships(cls): return relationship.one_to_many( - cls, 'relationship', child_fk='target_node_fk', back_populates='target_node') + cls, 'relationship', child_fk='target_node_fk', back_populates='target_node', + rel_kwargs=dict( + order_by='Relationship.target_position', + collection_class=ordering_list('target_position', count_from=0) + ) + ) # endregion @@ -1166,9 +1177,9 @@ class RelationshipBase(InstanceModelMixin): :vartype properties: {basestring: :class:`Parameter`} :ivar interfaces: Bundles of operations :vartype interfaces: {basestring: :class:`Interfaces`} - :ivar source_position: ?? + :ivar source_position: The position of the relationship in the outbound relationships. :vartype source_position: int - :ivar target_position: ?? + :ivar target_position: The position of the relationship in the inbound relationships. :vartype target_position: int :ivar source_node: Source node :vartype source_node: :class:`Node` @@ -1185,7 +1196,9 @@ class RelationshipBase(InstanceModelMixin): 'target_node_fk', 'target_capability_fk', 'requirement_template_fk', - 'relationship_template_fk'] + 'relationship_template_fk', + 'target_position', + 'source_position'] # region foreign keys @@ -1289,8 +1302,8 @@ class RelationshipBase(InstanceModelMixin): # endregion - source_position = Column(Integer) # ??? - target_position = Column(Integer) # ??? + source_position = Column(Integer) + target_position = Column(Integer) def configure_operations(self): for interface in self.interfaces.itervalues(): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1f3e7ff4/tests/modeling/test_mixins.py ---------------------------------------------------------------------- diff --git a/tests/modeling/test_mixins.py b/tests/modeling/test_mixins.py index 651f53f..cd7cadf 100644 --- a/tests/modeling/test_mixins.py +++ b/tests/modeling/test_mixins.py @@ -155,7 +155,10 @@ def test_relationship_model_ordering(context): """ assert direction in ('inbound', 'outbound') - relationships = getattr(node, direction + '_relationships') + def get_relationships(): + return getattr(node, direction + '_relationships') + + relationships = get_relationships() assert len(relationships) == 2 reversed_relationship = list(reversed(relationships)) @@ -163,7 +166,7 @@ def test_relationship_model_ordering(context): relationships[:] = reversed_relationship context.model.node.update(node) - assert relationships == reversed_relationship + assert get_relationships() == reversed_relationship flip_and_assert(source_node, 'outbound') flip_and_assert(target_node, 'inbound')