def test_serialize():
    print(sys.version_info, sys.version)
    assert serialize(data, Serialization.JSON_UTF8, Compression.NONE) == \
        ser[Serialization.JSON_UTF8] == data.to_json().encode()
    assert serialize(data, Serialization.JSON_BASE64, Compression.NONE) == \
        ser[Serialization.JSON_BASE64] == base64.b64encode(data.to_json().encode())
    assert serialize(data, Serialization.PICKLE3, Compression.NONE) == \
        ser[Serialization.PICKLE3] == pickle.dumps(data, 3)
    assert serialize(data, Serialization.PICKLE4, Compression.NONE) == \
        ser[Serialization.PICKLE4] == pickle.dumps(data, 4)
    if pickle5_available:
        assert serialize(data, Serialization.PICKLE5, Compression.NONE) == \
            ser[Serialization.PICKLE5] == pickle.dumps(data, 5)
Ejemplo n.º 2
0
 def _encode_message(self, payload: EventPayload, queue: str,
                     track_ids: Dict[str, str], auth_info: Dict[str, Any],
                     compression: Compression,
                     serialization: Serialization) -> dict:
     """
     Extract dictionary of fields to be sent to Redis from a DataEvent
     :param payload, DataEvent
     :return: dict of str containing:
         :id: extracted from payload.event_id() method
         :type: datatype name
         :submit_ts: datetime at the moment of this call, in UTC ISO format
         :event_ts: extracted from payload.event_ts() if defined, if not empty string
         :payload: json serialized payload
     """
     datatype = type(payload)
     event_fields = {
         'id': payload.event_id(),  # type: ignore
         'type': f"{datatype.__module__}.{datatype.__qualname__}",
         'submit_ts': datetime.now(tz=timezone.utc).isoformat(),
         'event_ts': '',
         **{k: v or ''
            for k, v in track_ids.items()},
         'auth_info': base64.b64encode(json.dumps(auth_info).encode()),
         'ser': serialization.value,
         'comp': compression.value,
         'payload': serialize(payload, serialization, compression),
         'queue': queue.encode()
     }
     event_ts = payload.event_ts()  # type: ignore
     if isinstance(event_ts, datetime):
         event_fields['event_ts'] = \
             event_ts.astimezone(tz=timezone.utc).isoformat()
     elif isinstance(event_ts, str):
         event_fields['event_ts'] = event_ts
     return event_fields
def test_serialize_collections():
    assert serialize({"test": "value"}, Serialization.JSON_UTF8,
                     Compression.NONE) == b'{"test": "value"}'
    assert deserialize(b'{"test": "value"}', Serialization.JSON_UTF8,
                       Compression.NONE, dict) == {
                           "test": "value"
                       }

    assert serialize(["test", "value"], Serialization.JSON_UTF8,
                     Compression.NONE) == b'["test", "value"]'
    assert deserialize(b'["test", "value"]', Serialization.JSON_UTF8,
                       Compression.NONE, list) == ["test", "value"]

    assert serialize({"test", "value"}, Serialization.JSON_UTF8, Compression.NONE) == b'["test", "value"]' \
        or serialize({"test", "value"}, Serialization.JSON_UTF8, Compression.NONE) == b'["value", "test"]'
    assert deserialize(b'["test", "value"]', Serialization.JSON_UTF8,
                       Compression.NONE, set) == {"test", "value"}
def test_serialize_primitives():
    assert serialize("test", Serialization.JSON_UTF8,
                     Compression.NONE) == b'{"value": "test"}'
    assert deserialize(b'{"value": "test"}', Serialization.JSON_UTF8,
                       Compression.NONE, str) == "test"

    assert serialize("test", Serialization.JSON_BASE64,
                     Compression.NONE) == b'eyJ2YWx1ZSI6ICJ0ZXN0In0='
    assert deserialize(b'eyJ2YWx1ZSI6ICJ0ZXN0In0=', Serialization.JSON_BASE64,
                       Compression.NONE, str) == "test"

    assert serialize(
        "test", Serialization.PICKLE3,
        Compression.NONE) == b'\x80\x03X\x04\x00\x00\x00testq\x00.'
    assert deserialize(b'\x80\x03X\x04\x00\x00\x00testq\x00.',
                       Serialization.PICKLE3, Compression.NONE, str) == "test"

    assert serialize("test", Serialization.PICKLE4, Compression.NONE) == \
        b'\x80\x04\x95\x08\x00\x00\x00\x00\x00\x00\x00\x8c\x04test\x94.'
    assert deserialize(
        b'\x80\x04\x95\x08\x00\x00\x00\x00\x00\x00\x00\x8c\x04test\x94.',
        Serialization.PICKLE4, Compression.NONE, str) == "test"
    if pickle5_available:
        assert serialize("test", Serialization.PICKLE5, Compression.NONE) == \
            b'\x80\x05\x95\x08\x00\x00\x00\x00\x00\x00\x00\x8c\x04test\x94.'
        assert deserialize(
            b'\x80\x05\x95\x08\x00\x00\x00\x00\x00\x00\x00\x8c\x04test\x94.',
            Serialization.PICKLE5, Compression.NONE, str) == "test"

    assert serialize(42, Serialization.JSON_UTF8,
                     Compression.NONE) == b'{"value": 42}'
    assert deserialize(b'{"value": 42}', Serialization.JSON_UTF8,
                       Compression.NONE, int) == 42

    assert serialize(42.5, Serialization.JSON_UTF8,
                     Compression.NONE) == b'{"value": 42.5}'
    assert deserialize(b'{"value": 42.5}', Serialization.JSON_UTF8,
                       Compression.NONE, float) == 42.5

    assert serialize(True, Serialization.JSON_UTF8,
                     Compression.NONE) == b'{"value": true}'
    assert deserialize(b'{"value": true}', Serialization.JSON_UTF8,
                       Compression.NONE, bool) is True