import numpy as np
import pytest

from karabo_bridge import serialize, deserialize

from .utils import compare_nested_dict


def test_serialize(data, protocol_version):
    msg = serialize(data, protocol_version=protocol_version)
    assert isinstance(msg, list)

    d, m = deserialize(msg)
    compare_nested_dict(data, d)
    assert m['source1'] == {'timestamp.tid': 9876543210, 'timestamp': 12345678}
    assert m['XMPL/DET/MOD0'] == {}


def test_serialize_with_metadata(data, metadata, protocol_version):
    msg = serialize(data, metadata, protocol_version=protocol_version)

    d, m = deserialize(msg)
    compare_nested_dict(metadata, m)


def test_serialize_with_dummy_timestamps(data, protocol_version):
    msg = serialize(data, protocol_version=protocol_version,
                    dummy_timestamps=True)

    d, m = deserialize(msg)
    assert set(m['XMPL/DET/MOD0']) == {'timestamp', 'timestamp.sec', 'timestamp.frac'}
    assert set(m['source1']) == {'timestamp', 'timestamp.tid'}
    assert m['source1']['timestamp.tid'] == 9876543210
    assert m['source1']['timestamp'] == 12345678


def test_serialize_with_metadata_and_dummy_timestamp(data, metadata, protocol_version):
    msg = serialize(data, metadata, protocol_version=protocol_version,
                    dummy_timestamps=True)

    d, m = deserialize(msg)
    compare_nested_dict(metadata, m)


def test_wrong_version(data):
    with pytest.raises(ValueError):
        serialize(data, protocol_version='3.0')

[((179, 229), 'karabo_bridge.serialize', 'serialize', (['data'], {'protocol_version': 'protocol_version'}), '(data, protocol_version=protocol_version)\n', (188, 229), False, 'from karabo_bridge import serialize, deserialize\n'), ((275, 291), 'karabo_bridge.deserialize', 'deserialize', (['msg'], {}), '(msg)\n', (286, 291), False, 'from karabo_bridge import serialize, deserialize\n'), ((521, 581), 'karabo_bridge.serialize', 'serialize', (['data', 'metadata'], {'protocol_version': 'protocol_version'}), '(data, metadata, protocol_version=protocol_version)\n', (530, 581), False, 'from karabo_bridge import serialize, deserialize\n'), ((594, 610), 'karabo_bridge.deserialize', 'deserialize', (['msg'], {}), '(msg)\n', (605, 610), False, 'from karabo_bridge import serialize, deserialize\n'), ((726, 799), 'karabo_bridge.serialize', 'serialize', (['data'], {'protocol_version': 'protocol_version', 'dummy_timestamps': '(True)'}), '(data, protocol_version=protocol_version, dummy_timestamps=True)\n', (735, 799), False, 'from karabo_bridge import serialize, deserialize\n'), ((832, 848), 'karabo_bridge.deserialize', 'deserialize', (['msg'], {}), '(msg)\n', (843, 848), False, 'from karabo_bridge import serialize, deserialize\n'), ((1203, 1290), 'karabo_bridge.serialize', 'serialize', (['data', 'metadata'], {'protocol_version': 'protocol_version', 'dummy_timestamps': '(True)'}), '(data, metadata, protocol_version=protocol_version,\n    dummy_timestamps=True)\n', (1212, 1290), False, 'from karabo_bridge import serialize, deserialize\n'), ((1319, 1335), 'karabo_bridge.deserialize', 'deserialize', (['msg'], {}), '(msg)\n', (1330, 1335), False, 'from karabo_bridge import serialize, deserialize\n'), ((1414, 1439), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (1427, 1439), False, 'import pytest\n'), ((1449, 1488), 'karabo_bridge.serialize', 'serialize', (['data'], {'protocol_version': '"""3.0"""'}), "(data, protocol_version='3.0')\n", (1458, 1488), False, 'from karabo_bridge import serialize, deserialize\n')]