Source

araldo / test / marshalling_test.py

Full commit
import pytest
import json
from datetime import datetime
from araldo.marshalling.default_marshalling import DefaultMarshalling
from araldo.message import Message

TIMESTAMP_FORMAT = "%Y%m%d_%H%M%S"


@pytest.fixture
def default_marshalling():
    return DefaultMarshalling()


@pytest.fixture
def message_str():
    message_obj = {
        "origin": "test_origin",
        "destination": "test_destination",
        "payload": "test_payload",
        "msg_id": "test_msg_id",
        "creation_timestamp": datetime(2012, 10, 1).strftime(TIMESTAMP_FORMAT)}
    return json.dumps(message_obj)


@pytest.fixture
def message():
    return Message(
        origin="test_origin",
        destination="test_destination",
        payload="test_payload",
        msg_id="test_msg_id",
        creation_timestamp=datetime(2012, 10, 1).strftime(TIMESTAMP_FORMAT)
    )


def test_instantiation(default_marshalling):
    pass


def test_to_internal_format(default_marshalling, message_str):
    message = default_marshalling.to_internal_format(message_str)
    assert isinstance(message, Message)
    assert message.msg_id == "test_msg_id"
    assert message.payload == "test_payload"


def test_to_external_format(default_marshalling, message):
    message_str = default_marshalling.to_external_format(message)
    assert type(message_str) == str
    json.loads(message_str)