Ejemplo n.º 1
0
def test_conditional_connect_zero_or_more():
    """Test EstuaryStructuredNode.conditional_connect on a ZeroOrMore relationship."""
    adv = Advisory(id_='12345', advisory_name='RHBA-2017:27760-01').save()
    bug = BugzillaBug(id_='2345').save()
    bug_two = BugzillaBug(id_='3456').save()

    assert len(adv.attached_bugs) == 0
    EstuaryStructuredNode.conditional_connect(adv.attached_bugs, bug)
    assert bug in adv.attached_bugs
    assert len(adv.attached_bugs) == 1

    EstuaryStructuredNode.conditional_connect(adv.attached_bugs, bug_two)
    assert bug in adv.attached_bugs
    assert bug_two in adv.attached_bugs
    assert len(adv.attached_bugs) == 2
Ejemplo n.º 2
0
def test_conditional_connect_zero_or_one():
    """Test EstuaryStructuredNode.conditional_connect on a ZerorOrOne relationship."""
    adv = Advisory(id_='12345', advisory_name='RHBA-2017:27760-01').save()
    tbrady = User(username='******').save()
    thanks = User(username='******').save()

    assert len(adv.assigned_to) == 0
    EstuaryStructuredNode.conditional_connect(adv.assigned_to, tbrady)
    assert tbrady in adv.assigned_to
    assert len(adv.assigned_to) == 1

    EstuaryStructuredNode.conditional_connect(adv.assigned_to, thanks)
    assert tbrady not in adv.assigned_to
    assert thanks in adv.assigned_to
    assert len(adv.assigned_to) == 1
Ejemplo n.º 3
0
def test_conditional_connect_one():
    """Test EstuaryStructuredNode.conditional_connect on a One relationship."""
    class TestModel(EstuaryStructuredNode):
        id_ = UniqueIdProperty(db_property='id')
        owner = RelationshipTo('estuary.models.user.User',
                               'OWNS',
                               cardinality=One)

    tbrady = User(username='******').save()
    thanks = User(username='******').save()
    test = TestModel(id_='12345').save()
    test.owner.connect(tbrady)
    with pytest.raises(NotImplementedError) as exc_info:
        EstuaryStructuredNode.conditional_connect(test.owner, thanks)
    assert 'conditional_connect doesn\'t support cardinality of one' == str(
        exc_info.value)
Ejemplo n.º 4
0
    def _get_partial_story(query, resources_to_expand):
        results, _ = db.cypher_query(query)

        if not results:
            return {}

        # Assuming that if Path is the first result, then that's all we want to process.
        results = [list(results[0][0].nodes)]

        return EstuaryStructuredNode.inflate_results(results, resources_to_expand)[0]
Ejemplo n.º 5
0
    def _get_partial_story(results, reverse=False):

        if not results:
            return []

        # Assuming that if Path is the first result, then that's all we want to process
        results = [list(results[0][0].nodes)]
        # Reverse will be true when it is a backward query to preserve the story order
        if reverse:
            results = [results[0][::-1]]

        return EstuaryStructuredNode.inflate_results(results)[0]
Ejemplo n.º 6
0
    def _get_partial_stories(results, reverse=False):

        results_list = []

        if not results:
            return results_list

        # Creating a list of lists where each list is a collection of node IDs
        # of the nodes present in that particular story path.
        # Paths are re-sorted in ascending order to simplify the logic below
        path_nodes_id = []
        for path in reversed(results):
            path_nodes_id.append([node.id for node in path[0].nodes])

        unique_paths = []
        for index, node_set in enumerate(path_nodes_id[:-1]):
            unique = True
            for alternate_set in path_nodes_id[index + 1:]:
                # If the node_set is a subset of alternate_set,
                # we know they are the same path except the alternate_set is longer.
                # If alternate_set and node_set only have one node ID of difference,
                # we know it's the same path but from the perspective of different siblings.
                if set(node_set).issubset(set(alternate_set)) or len(
                        set(alternate_set).difference(set(node_set))) == 1:
                    unique = False
                    break
            if unique:
                # Since results is from longest to shortest, we need to get the opposite index.
                unique_paths.append(results[(len(path_nodes_id) - index) -
                                            1][0])
        # While traversing, the outer for loop only goes until the second to last element
        # because the inner for loop always starts one element ahead of the outer for loop.
        # Hence, all the subsets of the last element will not be added to the unique_paths
        # list as the for loops will eliminate them. So we add the last element
        # since we are sure it is unique.
        unique_paths.append(results[0][0])
        if reverse:
            unique_paths_nodes = [path.nodes[::-1] for path in unique_paths]
        else:
            unique_paths_nodes = [path.nodes for path in unique_paths]

        return EstuaryStructuredNode.inflate_results(unique_paths_nodes)