def test_compression(self):
        """Test compression on large objects"""
        adr = ActivityDetailRecord(ActivityDetailType.INPUT_DATA, key="my_ke3", show=True, importance=125)
        adr.add_value("text/plain", "this is some data00000000000000000000000000000000000" * 1000)

        byte_array_no_compression = adr.to_bytes(compress=False)
        assert type(byte_array_no_compression) == bytes

        adr2 = ActivityDetailRecord(ActivityDetailType.INPUT_DATA, key="my_ke3", show=True, importance=125)
        adr2.add_value("text/plain", "this is some data00000000000000000000000000000000000" * 1000)
        byte_array_compression = adr2.to_bytes(compress=True)
        assert type(byte_array_compression) == bytes

        assert len(byte_array_compression) < len(byte_array_no_compression)

        adr3 = ActivityDetailRecord.from_bytes(byte_array_compression, decompress=True)

        assert type(adr3) == ActivityDetailRecord
        assert adr3.type == ActivityDetailType.INPUT_DATA
        assert adr3.action == ActivityAction.NOACTION
        assert adr3.key is None
        assert adr3.show is True
        assert adr3.importance == 125
        assert adr3.tags == []
        assert adr3.data == adr.data
    def test_to_bytes_from_bytes(self):
        """Test converting to a byte array"""
        adr = ActivityDetailRecord(ActivityDetailType.CODE_EXECUTED, key="my_key3", show=True, importance=225,
                                   action=ActivityAction.CREATE)
        adr.add_value("text/plain", "this is some data")

        byte_array_no_compression = adr.to_bytes(compress=False)
        assert type(byte_array_no_compression) == bytes

        adr2 = ActivityDetailRecord.from_bytes(byte_array_no_compression, decompress=False)

        assert type(adr2) == ActivityDetailRecord
        assert adr2.type == ActivityDetailType.CODE_EXECUTED
        assert adr2.action == ActivityAction.CREATE
        assert adr2.key is None
        assert adr2.show is True
        assert adr2.importance == 225
        assert adr2.tags == []
        assert adr2.data == {"text/plain": "this is some data"}
Ejemplo n.º 3
0
    def put_detail_record(
            self, detail_obj: ActivityDetailRecord) -> ActivityDetailRecord:
        """Method to write a detail record to the activity detail db

        Args:
            detail_obj(ActivityDetailRecord): The detail record to write

        Returns:
            ActivityDetailRecord: the detail record updated with the key
        """
        # Set compression option based on config and objects size
        compress = False
        if self.compress_details:
            if detail_obj.data_size >= self.compress_min_bytes:
                compress = True

        # Write record and store key
        detail_obj.key = self.detaildb.put(
            self._encode_write_options(compress=compress) +
            detail_obj.to_bytes(compress))

        logger.debug(
            f"Successfully wrote ActivityDetailRecord {detail_obj.key}")
        return detail_obj