File: test_formatter.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 (49 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download | duplicates (4)
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
from argparse import Namespace
from unittest.mock import Mock

import pytest

from streamlink.plugin import Plugin
from streamlink_cli.main import get_formatter
from streamlink_cli.utils import datetime


@pytest.fixture(scope="module")
def plugin():
    class FakePlugin(Plugin):
        __module__ = "FAKE"

        def _get_streams(self):  # pragma: no cover
            pass

    plugin = FakePlugin(Mock(), "https://foo/bar")
    plugin.id = "ID"
    plugin.author = "AUTHOR"
    plugin.category = "CATEGORY"
    plugin.title = "TITLE"

    return plugin


@pytest.mark.parametrize(
    ("formatterinput", "expected"),
    [
        pytest.param("{url}", "https://foo/bar", id="url"),
        pytest.param("{plugin}", "FAKE", id="plugin-name"),
        pytest.param("{id}", "ID", id="id"),
        pytest.param("{author}", "AUTHOR", id="author"),
        pytest.param("{category}", "CATEGORY", id="category"),
        pytest.param("{game}", "CATEGORY", id="category-fallback-game"),
        pytest.param("{title}", "TITLE", id="title"),
        pytest.param("{time}", "2000-01-01_00-00-00", id="time"),
        pytest.param("{time:%Y}", "2000", id="time-formatted"),
    ],
)
def test_get_formatter(monkeypatch: pytest.MonkeyPatch, plugin: Plugin, formatterinput: str, expected: str):
    # workaround for freezegun not being able to patch the subclassed datetime class in streamlink_cli.utils
    # which defines the default datetime->str conversion format (needed for path outputs)
    monkeypatch.setattr("streamlink_cli.utils.datetime.now", Mock(return_value=datetime(2000, 1, 1, 0, 0, 0, 0)))
    monkeypatch.setattr("streamlink_cli.main.args", Namespace(url="https://foo/bar"))

    formatter = get_formatter(plugin)
    assert formatter.title(formatterinput) == expected