import pytest
from pybrary.databrary.condition import Condition


@pytest.fixture
def expected_condition_dict():
    return {
        "key": "1250",
        "name": "Crawler",
        "category": "condition"
    }


@pytest.fixture
def databrary_condition_dict():
    return {
        "id": 1250,
        "category": 4,
        "measures": {
            "23": "Crawler"
        }
    }


@pytest.fixture
def databrary_condition_dict_fail():
    return {
        "category": 4,
        "measures": {
            "23": "Crawler"
        }
    }


def test_to_dict(expected_condition_dict):
    assert Condition(
        record_key=1250,
        record_id=1250,
        name="Crawler"
    ).to_dict() == expected_condition_dict


def test_from_databrary(databrary_condition_dict):
    condition = Condition.from_databrary(databrary_condition_dict)
    assert condition is not None


def test_from_databrary_fail(databrary_condition_dict_fail):
    with pytest.raises(Exception) as e:
        Condition.from_databrary(databrary_condition_dict_fail)
