Example #1
0
 def test_deserialize_buffers(self):
     """Test correct file handling when deserializing buffers."""
     with TransportSessionClient(SqliteSession, URI) as session:
         images = self.setup_buffers2(session)
         deserialize_buffers(
             session,
             buffer_context=BufferContext.USER,
             data=SERIALIZED_BUFFERS,
             temp_directory=None,
             target_directory=CLIENT_DIR,
         )
         added, updated, deleted = session._buffers[BufferContext.USER]
         self.assertEqual(len(added), 1)
         self.assertEqual(len(updated), 2)
         self.assertEqual(len(deleted), 1)
         images = images + [added[uuid.UUID(int=3)]]
         target = [
             "%s-%s" % (image.uid.hex, file)
             for image, file in zip(images, FILES)
         ]
         target_full_path = [os.path.join(CLIENT_DIR, t) for t in target]
         self.assertEqual(added[uuid.UUID(int=3)].path, target_full_path[2])
         self.assertEqual(
             updated[uuid.UUID(int=1)].path, target_full_path[0]
         )
         self.assertRaises(
             AttributeError, getattr, deleted[uuid.UUID(int=2)], "path"
         )
    def _receive(self, data, temp_directory):
        """Process the response of the server.

        Args:
            data (str): Receive changes made by the server
                (serialized buffers).

        Raises:
            RuntimeError: Error occurred on the server side
        """
        if data.startswith("ERROR: "):
            raise RuntimeError("Error on Server side: %s" % data[7:])
        remainder = deserialize_buffers(
            self,
            buffer_context=BufferContext.ENGINE,
            data=data,
            temp_directory=temp_directory,
            target_directory=self._file_destination
        )
        result = None
        if remainder and "expired" in remainder:
            self.expire(set(remainder["expired"]))
        if remainder and "result" in remainder:
            result = remainder["result"]
        return result
    def _load_from_session(self, data, connection_id, temp_directory=None):
        """Load cuds_objects from the session.

        Args:
            data (str): The uids to load as json encoded list.

        Returns:
            str: The resulting cuds_objects, serialized.
        """
        session = self.session_objs[connection_id]
        uids = deserialize_buffers(
            session,
            buffer_context=None,
            data=data,
            temp_directory=temp_directory,
            target_directory=self._file_destination)["uids"]
        cuds_objects = list(session.load(*uids))
        additional = {"result": cuds_objects}
        return serialize_buffers(session,
                                 buffer_context=BufferContext.ENGINE,
                                 additional_items=additional)
    def _run_command(self, data, command, connection_id, temp_directory=None):
        """Run a method of the session.

        Args:
            data (str): The data of the client.
            command (str): The method to execute.

        Returns:
            str: The buffers after the execution of the command, serialized.
        """
        session = self.session_objs[connection_id]
        arguments = deserialize_buffers(
            session,
            buffer_context=BufferContext.USER,
            data=data,
            temp_directory=temp_directory,
            target_directory=self._file_destination)
        result = getattr(session, command)(*arguments["args"],
                                           **arguments["kwargs"])
        additional = {"result": result} if result else dict()
        return serialize_buffers(session,
                                 buffer_context=BufferContext.ENGINE,
                                 additional_items=additional)
    def test_deserialize_buffers(self):
        """Test de-serialization of buffers."""
        # buffer context user
        with TestWrapperSession() as s1:
            ws1 = city.CityWrapper(session=s1, uid=123)
            c = city.City(name="Freiburg", uid=1)
            p1 = city.Citizen(uid=uuid.UUID(int=3))
            p2 = city.Citizen(uid=uuid.UUID(int=4))
            c.add(p1, p2, rel=city.hasInhabitant)
            ws1.add(c)
            s1._reset_buffers(BufferContext.USER)
            s1.expire(p2)

            additional = deserialize_buffers(
                s1,
                buffer_context=BufferContext.USER,
                data=json.dumps(SERIALIZED_BUFFERS_EXPIRED))
            self.assertEqual(additional, {
                "args": [42],
                "kwargs": {
                    "name": "London"
                }
            })
            self.assertEqual(
                set(s1._registry.keys()), {
                    uuid.UUID(int=123),
                    uuid.UUID(int=2),
                    uuid.UUID(int=3),
                    uuid.UUID(int=4)
                })
            cn = ws1.get(uuid.UUID(int=2))
            self.assertEqual(cn.name, "Paris")
            self.assertEqual(ws1._neighbors[city.hasPart],
                             {cn.uid: [city.City]})
            self.assertEqual(set(ws1._neighbors.keys()), {city.hasPart})
            self.assertEqual(cn._neighbors[city.isPartOf],
                             {ws1.uid: [city.CityWrapper]})
            self.assertEqual(set(cn._neighbors.keys()), {city.isPartOf})
            self.assertEqual(s1._expired, {uuid.UUID(int=3), uuid.UUID(int=4)})
            self.assertEqual(s1._buffers, [[{
                cn.uid: cn
            }, {
                ws1.uid: ws1
            }, {
                c.uid: c
            }], [dict(), dict(), dict()]])

        self.setUp()

        # buffer context engine
        with TestWrapperSession() as s1:
            ws1 = city.CityWrapper(session=s1, uid=123)
            c = city.City(name="Freiburg", uid=1)
            p1 = city.Citizen(uid=uuid.UUID(int=3))
            p2 = city.Citizen(uid=uuid.UUID(int=4))
            c.add(p1, p2, rel=city.hasInhabitant)
            ws1.add(c)
            s1._reset_buffers(BufferContext.USER)
            s1.expire(p2)

            additional = deserialize_buffers(
                s1,
                buffer_context=BufferContext.ENGINE,
                data=json.dumps(SERIALIZED_BUFFERS_EXPIRED))
            self.assertEqual(additional, {
                "args": [42],
                "kwargs": {
                    "name": "London"
                }
            })
            self.assertEqual(s1._buffers,
                             [[dict(), dict(), dict()],
                              [{
                                  cn.uid: cn
                              }, {
                                  ws1.uid: ws1
                              }, {
                                  c.uid: c
                              }]])
            self.assertEqual(
                set(s1._registry.keys()), {
                    uuid.UUID(int=123),
                    uuid.UUID(int=2),
                    uuid.UUID(int=3),
                    uuid.UUID(int=4)
                })
            cn = ws1.get(uuid.UUID(int=2))
            self.assertEqual(cn.name, "Paris")
            self.assertEqual(ws1._neighbors[city.hasPart],
                             {cn.uid: [city.City]})
            self.assertEqual(set(ws1._neighbors.keys()), {city.hasPart})
            self.assertEqual(cn._neighbors[city.isPartOf],
                             {ws1.uid: [city.CityWrapper]})
            self.assertEqual(set(cn._neighbors.keys()), {city.isPartOf})
            self.assertEqual(s1._expired, {uuid.UUID(int=3), uuid.UUID(int=4)})
            self.assertEqual(s1._buffers,
                             [[dict(), dict(), dict()],
                              [dict(), dict(), dict()]])