"""Unit tests for relations serialization."""
from __future__ import annotations

from types import SimpleNamespace

import pytest

from relations.bp.data.routes import _serialize_rel


class TestSerializeRel:
    def test_all_fields(self):
        rel = SimpleNamespace(
            id=1,
            parent_type="page",
            parent_id=100,
            child_type="calendar",
            child_id=200,
            sort_order=0,
            label="Main Calendar",
            relation_type="container",
            metadata_={"key": "val"},
        )
        result = _serialize_rel(rel)
        assert result == {
            "id": 1,
            "parent_type": "page",
            "parent_id": 100,
            "child_type": "calendar",
            "child_id": 200,
            "sort_order": 0,
            "label": "Main Calendar",
            "relation_type": "container",
            "metadata": {"key": "val"},
        }

    def test_none_optionals(self):
        rel = SimpleNamespace(
            id=2,
            parent_type="post",
            parent_id=1,
            child_type="market",
            child_id=1,
            sort_order=1,
            label=None,
            relation_type=None,
            metadata_=None,
        )
        result = _serialize_rel(rel)
        assert result["label"] is None
        assert result["relation_type"] is None
        assert result["metadata"] is None

    def test_dict_structure(self):
        rel = SimpleNamespace(
            id=3, parent_type="a", parent_id=1,
            child_type="b", child_id=2,
            sort_order=0, label="", relation_type="link",
            metadata_={},
        )
        result = _serialize_rel(rel)
        assert set(result.keys()) == {
            "id", "parent_type", "parent_id",
            "child_type", "child_id", "sort_order",
            "label", "relation_type", "metadata",
        }
