Exemple #1
0
    def test_fix_old_neighbors(self):
        """Check if _fix_old_neighbors.

        - Deletes old children.
        - Adds connection to old parents.
        """
        c = city.City(name="Freiburg")

        with CoreSession() as session:
            wrapper = city.CityWrapper(session=session)
            cw = wrapper.add(c)
            n = city.Neighborhood(name="Zähringen")
            nw = cw.add(n)

            c = clone_cuds_object(c)
            c._session = session
            old_neighbor_diff = get_neighbor_diff(cw, c)
            old_neighbors = session.load(*[x[0] for x in old_neighbor_diff])
            Cuds._fix_old_neighbors(
                new_cuds_object=c,
                old_cuds_object=cw,
                old_neighbors=old_neighbors,
                old_neighbor_diff=old_neighbor_diff,
            )
        self.assertEqual(c.get(rel=city.isPartOf), [wrapper])
        self.assertEqual(c.get(rel=city.hasPart), [])
        self.assertEqual(nw.get(rel=city.isPartOf), [])
        self.assertEqual(wrapper.get(rel=city.hasPart), [c])
Exemple #2
0
    def _recursive_store(self, new_cuds_object, old_cuds_object=None):
        """Recursively store cuds_object and all its children.

        One-way relationships and dangling references are fixed.

        Args:
            new_cuds_object (Cuds): The Cuds object to store recursively.
            old_cuds_object (Cuds, optional): The old version of the
                CUDS object. Defaults to None.

        Returns:
            Cuds: The added CUDS object.
        """
        # add new_cuds_object to self and replace old_cuds_object
        queue = [(self, new_cuds_object, old_cuds_object)]
        uids_stored = {new_cuds_object.uid, self.uid}
        missing = dict()
        result = None
        while queue:

            # Store copy in registry
            add_to, new_cuds_object, old_cuds_object = queue.pop(0)
            if new_cuds_object.uid in missing:
                del missing[new_cuds_object.uid]
            old_cuds_object = clone_cuds_object(old_cuds_object)
            new_child_getter = new_cuds_object
            new_cuds_object = create_from_cuds_object(new_cuds_object,
                                                      add_to.session)
            # fix the connections to the neighbors
            add_to._fix_neighbors(new_cuds_object, old_cuds_object,
                                  add_to.session, missing)
            result = result or new_cuds_object

            for outgoing_rel in new_cuds_object._neighbors:

                # do not recursively add parents
                if not outgoing_rel.is_subclass_of(cuba.activeRelationship):
                    continue

                # add children not already added
                for child_uid in new_cuds_object._neighbors[outgoing_rel]:
                    if child_uid not in uids_stored:
                        new_child = new_child_getter.get(child_uid,
                                                         rel=outgoing_rel)
                        old_child = self.session.load(child_uid).first()
                        queue.append((new_cuds_object, new_child, old_child))
                        uids_stored.add(new_child.uid)

        # perform the deletion
        for uid in missing:
            for cuds_object, rel in missing[uid]:
                del cuds_object._neighbors[rel][uid]
                if not cuds_object._neighbors[rel]:
                    del cuds_object._neighbors[rel]
        return result
Exemple #3
0
 def test_clone_cuds_object(self):
     """Test cloning of cuds."""
     a = city.City(name="Freiburg")
     with CoreSession() as session:
         w = city.CityWrapper(session=session)
         aw = w.add(a)
         clone = clone_cuds_object(aw)
         self.assertIsNot(aw, None)
         self.assertIs(clone.session, aw.session)
         self.assertEqual(clone.uid, aw.uid)
         self.assertIs(aw, session._registry.get(aw.uid))
         self.assertEqual(clone.name, "Freiburg")
Exemple #4
0
    def _get_old_cuds_object_clone(self, uid):
        """Get old version of expired cuds object from registry.

        Args:
            uid (Union[UUID, URIRef]): The uid to get the old
            cuds object.

        Returns:
            Cuds, optional: A clone of the old cuds object
        """
        clone = None
        if uid in self._registry:
            clone = clone_cuds_object(self._registry.get(uid))
        return clone
Exemple #5
0
    def test_fix_new_parents(self):
        """Check _fix_new_parent.

        Make sure the method:
        - Deletes connection to new parents not available in new session
        - Adds connection to new parents available in new session
        """
        n = city.Neighborhood(name="Zähringen")
        # parent in both sessions
        c1 = city.City(name="Berlin")
        # only parent in default session (available in both)
        c2 = city.City(name="Paris")
        n.add(c1, c2, rel=city.isPartOf)

        c3 = city.City(name="London")

        with CoreSession() as session:
            wrapper = city.CityWrapper(session=session)
            c1w, c2w = wrapper.add(c1, c2)
            nw = c2w.get(n.uid)
            nw.remove(c2.uid, rel=cuba.relationship)

            # only parent + available in default session
            n.add(c3, rel=city.isPartOf)

            n = clone_cuds_object(n)
            n._session = session
            new_parent_diff = get_neighbor_diff(n, nw, mode="non-active")
            new_parents = session.load(*[x[0] for x in new_parent_diff])

            missing = dict()
            Cuds._fix_new_parents(
                new_cuds_object=n,
                new_parents=new_parents,
                new_parent_diff=new_parent_diff,
                missing=missing,
            )

        self.assertEqual(
            set(n.get(rel=city.isPartOf)),
            {c1w, c2w, None},  # missing parent, should be in missing dict
        )
        self.assertEqual(missing, {c3.uid: [(n, city.isPartOf)]})
        self.assertEqual(c2w.get(rel=city.hasPart), [n])