89 lines
2.2 KiB
Python
89 lines
2.2 KiB
Python
"""Tests for the playlist.data.base module."""
|
|
import dataclasses
|
|
import datetime
|
|
import typing
|
|
|
|
import marshmallow # type: ignore [import]
|
|
import pytest # type: ignore [import]
|
|
|
|
from playlist.data import base # type: ignore [import]
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Dummy(base.BaseData["Dummy"]): # type: ignore [misc]
|
|
"""Dummy class for tests."""
|
|
|
|
name: str
|
|
some_id: int
|
|
calc: float
|
|
flag: bool
|
|
modified: datetime.datetime
|
|
|
|
|
|
@pytest.fixture # type: ignore [misc]
|
|
def dummyannotations() -> dict[str, typing.Any]:
|
|
"""Make a dummy annotation dict for testing."""
|
|
return {
|
|
"name": str,
|
|
"some_id": int,
|
|
"calc": float,
|
|
"flag": bool,
|
|
"modified": str,
|
|
}
|
|
|
|
|
|
@pytest.fixture # type: ignore [misc]
|
|
def modified_date() -> datetime.datetime:
|
|
"""Make a reusable datetime for testing."""
|
|
return datetime.datetime.now()
|
|
|
|
|
|
@pytest.fixture # type: ignore [misc]
|
|
def dummydict(modified_date: datetime.datetime) -> dict[str, typing.Any]:
|
|
"""Make a dummy dictionary for testing."""
|
|
return {
|
|
"name": "Something",
|
|
"some_id": 1,
|
|
"calc": 0.1,
|
|
"flag": True,
|
|
"modified": "T".join(str(modified_date).split(" ")),
|
|
}
|
|
|
|
|
|
@pytest.fixture # type: ignore [misc]
|
|
def dummyobj(modified_date: datetime.datetime) -> Dummy:
|
|
"""Make a dummy object for testing."""
|
|
return Dummy(
|
|
name="Something",
|
|
some_id=1,
|
|
calc=0.1,
|
|
flag=True,
|
|
modified=modified_date,
|
|
)
|
|
|
|
|
|
def test_schema() -> None:
|
|
"""Validate that <data class>.Schema works."""
|
|
result = isinstance(Dummy.Schema, marshmallow.Schema)
|
|
|
|
assert result
|
|
|
|
|
|
def test_dict(dummyannotations: dict[str, typing.Any]) -> None:
|
|
"""Validate that <data class>.Dict works."""
|
|
result = Dummy.Dict.__annotations__ == dummyannotations
|
|
|
|
assert result
|
|
|
|
|
|
def test_load(dummydict: dict[str, typing.Any], dummyobj: Dummy) -> None:
|
|
"""Validate that <data class>.load() works."""
|
|
result = Dummy.load(dummydict)
|
|
assert result == dummyobj
|
|
|
|
|
|
def test_dump(dummydict: dict[str, typing.Any], dummyobj: Dummy) -> None:
|
|
"""Validate that <data class instance>.dump() works."""
|
|
result = dummyobj.dump()
|
|
assert result == dummydict
|