CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_user_interface.py66 linesDownload Raw Back to tests
1import string
2import timeit
3
4import pytest
5
6from sklearn.utils._user_interface import _message_with_time, _print_elapsed_time
7
8
9@pytest.mark.parametrize(
10    ["source", "message", "is_long"],
11    [
12        ("ABC", string.ascii_lowercase, False),
13        ("ABCDEF", string.ascii_lowercase, False),
14        ("ABC", string.ascii_lowercase * 3, True),
15        ("ABC" * 10, string.ascii_lowercase, True),
16        ("ABC", string.ascii_lowercase + "\u1048", False),
17    ],
18)
19@pytest.mark.parametrize(
20    ["time", "time_str"],
21    [
22        (0.2, "   0.2s"),
23        (20, "  20.0s"),
24        (2000, "33.3min"),
25        (20000, "333.3min"),
26    ],
27)
28def test_message_with_time(source, message, is_long, time, time_str):
29    out = _message_with_time(source, message, time)
30    if is_long:
31        assert len(out) > 70
32    else:
33        assert len(out) == 70
34
35    assert out.startswith("[" + source + "] ")
36    out = out[len(source) + 3 :]
37
38    assert out.endswith(time_str)
39    out = out[: -len(time_str)]
40    assert out.endswith(", total=")
41    out = out[: -len(", total=")]
42    assert out.endswith(message)
43    out = out[: -len(message)]
44    assert out.endswith(" ")
45    out = out[:-1]
46
47    if is_long:
48        assert not out
49    else:
50        assert list(set(out)) == ["."]
51
52
53@pytest.mark.parametrize(
54    ["message", "expected"],
55    [
56        ("hello", _message_with_time("ABC", "hello", 0.1) + "\n"),
57        ("", _message_with_time("ABC", "", 0.1) + "\n"),
58        (None, ""),
59    ],
60)
61def test_print_elapsed_time(message, expected, capsys, monkeypatch):
62    monkeypatch.setattr(timeit, "default_timer", lambda: 0)
63    with _print_elapsed_time("ABC", message):
64        monkeypatch.setattr(timeit, "default_timer", lambda: 0.1)
65    assert capsys.readouterr().out == expected
66 
Aluode/PerceptionLabPortable · CoolFace