Ejemplo n.º 1
0
    def _store(self, cuds_object):
        initialize = self.root is None
        super()._store(cuds_object)

        if initialize:
            with EngineContext(self):
                self._initialize()
                self._load_first_level()
Ejemplo n.º 2
0
    def test_create_recycle(self):
        """Test creation of cuds_objects for different session."""
        default_session = CoreSession()
        osp.core.cuds.Cuds._session = default_session
        a = city.City(name="Freiburg")
        self.assertIs(a.session, default_session)
        with TestWrapperSession() as session:
            w = city.CityWrapper(session=session)
            with EngineContext(session):
                b = create_recycle(oclass=city.City,
                                   kwargs={"name": "Offenburg"},
                                   uid=a.uid,
                                   session=session,
                                   fix_neighbors=False)
            self.assertEqual(b.name, "Offenburg")
            self.assertEqual(b.uid, a.uid)
            self.assertEqual(set(default_session._registry.keys()), {a.uid})
            self.assertIs(default_session._registry.get(a.uid), a)
            self.assertEqual(set(session._registry.keys()), {b.uid, w.uid})
            self.assertIs(session._registry.get(b.uid), b)
            self.assertEqual(session._buffers, [[{
                w.uid: w
            }, dict(), dict()], [{
                b.uid: b
            }, dict(), dict()]])

            x = city.Citizen()
            x = b.add(x, rel=city.hasInhabitant)

            c = create_recycle(oclass=city.City,
                               kwargs={"name": "Emmendingen"},
                               session=session,
                               uid=a.uid,
                               fix_neighbors=False)
            self.assertIs(b, c)
            self.assertEqual(c.name, "Emmendingen")
            self.assertEqual(c.get(rel=cuba.relationship), [])
            self.assertNotEqual(x.get(rel=cuba.relationship), [])
            self.assertEqual(set(default_session._registry.keys()),
                             {a.uid, x.uid})
            self.assertIs(default_session._registry.get(a.uid), a)
            self.assertEqual(session._buffers, [[{
                w.uid: w,
                x.uid: x
            }, {
                c.uid: c
            }, dict()], [dict(), dict(), dict()]])

            x = city.Citizen()
            x = c.add(x, rel=city.hasInhabitant)

            c = create_recycle(oclass=city.City,
                               kwargs={"name": "Karlsruhe"},
                               session=session,
                               uid=a.uid,
                               fix_neighbors=True)
            self.assertEqual(x.get(rel=cuba.relationship), [])
Ejemplo n.º 3
0
    def test_create_from_cuds_object(self):
        """Test copying cuds_objects to different session."""
        default_session = CoreSession()
        Cuds._session = default_session
        a = city.City(name="Freiburg")
        self.assertIs(a.session, default_session)
        with TestWrapperSession() as session:
            w = city.CityWrapper(session=session)
            with EngineContext(session):
                b = create_from_cuds_object(a, session)
            self.assertEqual(b.name, "Freiburg")
            self.assertEqual(b.uid, a.uid)
            self.assertEqual(set(default_session._registry.keys()), {a.uid})
            self.assertIs(default_session._registry.get(a.uid), a)
            self.assertEqual(set(session._registry.keys()), {b.uid, w.uid})
            self.assertIs(session._registry.get(b.uid), b)
            self.assertEqual(
                session._buffers,
                [[dict(), dict(), dict()], [{
                    b.uid: b
                }, dict(), dict()]],
            )

            b.name = "Emmendingen"
            x = city.Citizen(age=54, name="Franz", session=default_session)
            b.add(x, rel=city.hasInhabitant)
            y = city.Citizen(age=21, name="Rolf", session=default_session)
            a.add(y, rel=city.hasInhabitant)

            c = create_from_cuds_object(a, session)
            self.assertIs(b, c)
            self.assertEqual(c.name, "Freiburg")
            self.assertEqual(len(c.get(rel=cuba.relationship)), 1)
            self.assertEqual(c._neighbors[city.hasInhabitant],
                             {y.uid: [city.Citizen]})
            self.assertEqual(set(default_session._registry.keys()),
                             {a.uid, x.uid, y.uid})
            self.assertIs(default_session._registry.get(a.uid), a)
            self.assertEqual(
                session._buffers,
                [[{
                    x.uid: x
                }, {
                    c.uid: c
                }, dict()], [dict(), dict(), dict()]],
            )
Ejemplo n.º 4
0
    def test_load_from_session(self):
        """Test loading from the remote side."""
        with TestWrapperSession() as s1:
            c = city.City(name="Freiburg", uid=1)
            w = city.CityWrapper(session=s1, uid=3)
            cw = w.add(c)

            with EngineContext(s1):
                p = city.Citizen(name="Peter", age=12, uid=2)
                cw.add(p, rel=city.hasInhabitant)
                server = TransportSessionServer(TestWrapperSession, None, None)
                server.session_objs["user"] = s1
                s1._expired |= {c.uid, w.uid}
                result = server._load_from_session(
                    '{"uids": [{"UUID": 1}, {"UUID": 3}]}', "user")
            self.maxDiff = None
            assertJsonLdEqual(self, json.loads(result[0]), SERIALIZED_BUFFERS3)
            self.assertEqual(result[1], [])
Ejemplo n.º 5
0
    def _store(self, cuds_object):
        """Store and object in the database.

        Args:
            cuds_object (Cuds): The Cuds object to add.
        """
        initialize = self.root is None
        super()._store(cuds_object)

        if initialize:
            with EngineContext(self):
                self._init_transaction()
                try:
                    self._initialize()
                    self._load_first_level()
                    self._commit()
                except Exception as e:
                    self._rollback_transaction()
                    raise type(e)(str(e)) from e
Ejemplo n.º 6
0
 def f(session, *args, **kwargs):
     with EngineContext(session):
         func(session, *args, **kwargs)
Ejemplo n.º 7
0
 def on_send(command, data):
     with EngineContext(client):
         create_from_cuds_object(c2, client)
         return [c2, None]