File: test_dataclass.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (93 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations

import math
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

import pytest

from streamlink.utils.dataclass import FormattedDataclass
from streamlink.utils.times import fromtimestamp


if TYPE_CHECKING:
    from datetime import datetime, timedelta


@pytest.fixture(scope="module")
def baseclass() -> type:
    @dataclass(kw_only=True)
    class Foo(
        metaclass=FormattedDataclass,
        formatters={
            str: lambda s: f"{s.upper()!r}",
        },
    ):
        foo: str
        bar: int
        baz: float
        qux: str = field(repr=False)
        abc: datetime = field(default=fromtimestamp(0.5))
        xyz: timedelta = field(default=fromtimestamp(1.5) - fromtimestamp(0))

    return Foo


@pytest.fixture(scope="module")
def subclass(baseclass: type) -> type:
    @dataclass(kw_only=True)
    class Bar(
        baseclass,
        metaclass=FormattedDataclass,
        formatters={
            float: lambda x: f"{(x * 2.0):.3f}",
        },
        extra=["oof"],
    ):
        @property
        def oof(self) -> str:
            return self.foo[::-1]

    return Bar


@pytest.fixture(scope="module")
def subsubclass(subclass: type) -> type:
    @dataclass(kw_only=True)
    class Baz(subclass, metaclass=FormattedDataclass, extra=["barbar"]):
        @property
        def barbar(self) -> int:
            return self.bar * self.bar

    return Baz


@pytest.mark.parametrize(
    ("fixture", "expected"),
    [
        pytest.param(
            "baseclass",
            "Foo(foo='FOO', bar=123, baz=3.142, abc=1970-01-01T00:00:00.500000Z, xyz=0:00:01.500000)",
            id="baseclass",
        ),
        pytest.param(
            "subclass",
            "Bar(foo='FOO', bar=123, baz=6.283, abc=1970-01-01T00:00:00.500000Z, xyz=0:00:01.500000, oof='OOF')",
            id="subclass",
        ),
        pytest.param(
            "subsubclass",
            "Baz(foo='FOO', bar=123, baz=6.283, abc=1970-01-01T00:00:00.500000Z, xyz=0:00:01.500000, oof='OOF', barbar=15129)",
            id="subsubclass",
        ),
    ],
)
def test_serialize(request: pytest.FixtureRequest, fixture: str, expected: str):
    dc = request.getfixturevalue(fixture)
    item = dc(
        foo="foo",
        bar=123,
        baz=math.pi,
        qux="qux",
    )
    assert str(item) == repr(item) == expected