from dataclasses import dataclass

import pytest

from risc_lasagna.layer.dataclass_default_layer import DataClassDefaultLayer
from risc_lasagna.typedef import ConfigurationError


@dataclass
class NestedExample:
    nested_a: str


@dataclass
class Example:
    a: str
    b: str
    c: NestedExample


class NotADataClass:
    a: str


@pytest.mark.parametrize(
    "key,value",
    [("PROJECT_A", "a"), ("PROJECT_B", "b"), ("PROJECT_C_NESTED_A", "nested_a")],
)
def test_default_nested(key: str, value: str):
    layer = DataClassDefaultLayer(
        Example("a", "b", NestedExample("nested_a")), variable_prefix="PROJECT"
    )
    assert key in layer.keys()
    assert layer.get(key) == value


def test_not_a_dataclass():
    with pytest.raises(ConfigurationError):
        DataClassDefaultLayer(NotADataClass())


def test_keys():
    layer = DataClassDefaultLayer(
        Example("a", "b", NestedExample("nested_a")), variable_prefix="PROJECT"
    )
    assert len(layer.keys()) == 3
