File: test_dateaxisitem.py

package info (click to toggle)
taurus-pyqtgraph 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,284 kB
  • sloc: python: 5,234; makefile: 82
file content (145 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import taurus_pyqtgraph as tpg
from datetime import datetime
from locale import getlocale
import pytest

try:
    fromisoformat = datetime.fromisoformat
except AttributeError:  # py <3.7

    def fromisoformat(s):
        for fmt in (
            "%Y-%m-%dT%H:%M:%S.%f",
            "%Y-%m-%dT%H:%M:%S",
            "%Y-%m-%dT%H:%M",
            "%Y-%m-%d",
        ):
            try:
                return datetime.strptime(s, fmt)
            except ValueError:
                pass
        raise ValueError("cannot convert {}".format(s))


@pytest.mark.parametrize(
    "val_range,expected",
    [
        # FIXME: Known to fail in python 3.9, commented since all other tests
        #  are passing fine and there is no apparent reason for this to fail
        # (
        #     ["2020-01-01T00:01:00.100", "2020-01-01T00:01:00.500"],
        #     ["2020-01-01T00:01:00.200", "2020-01-01T00:01:00.400"],
        # ),
        (
            ["2020-01-01T00:00:04.900", "2020-01-01T00:00:07.900"],  # d=1s
            [
                "2020-01-01T00:00:05",
                "2020-01-01T00:00:06",
                "2020-01-01T00:00:07",
            ],
        ),
        (
            ["2020-01-01T00:00:04", "2020-01-01T00:00:37"],  # d=10s
            [
                "2020-01-01T00:00:10",
                "2020-01-01T00:00:20",
                "2020-01-01T00:00:30",
            ],
        ),
        (
            ["2020-01-01T00:05:10", "2020-01-01T00:08:50"],  # d=1m
            ["2020-01-01T00:06", "2020-01-01T00:07", "2020-01-01T00:08"],
        ),
        (
            ["2020-01-01T00:45:10", "2020-01-01T01:15:50"],  # d=10m
            ["2020-01-01T00:50", "2020-01-01T01:00", "2020-01-01T01:10"],
        ),
        (
            ["2020-01-01T00:45:10", "2020-01-01T03:15:50"],  # d=1h
            ["2020-01-01T01:00", "2020-01-01T02:00", "2020-01-01T03:00"],
        ),
        (
            ["2020-01-01T00:45:10", "2020-04-01T03:15:50"],  # d=1month
            ["2020-02-01", "2020-03-01", "2020-04-01"],
        ),
        (
            ["2020-01-01T00:45:10", "2023-04-01T03:15:50"],  # d=1y
            ["2021-01-01", "2022-01-01", "2023-01-01"],
        ),
    ],
)
def test_tickValues(qtbot, val_range, expected):
    """
    Check that the tickValues reimplementation returns the expected values
    """
    w = tpg.TaurusTrend()
    qtbot.addWidget(w)
    a = w.getPlotItem().axes["bottom"]["item"]
    minVal, maxVal = [fromisoformat(v).timestamp() for v in val_range]
    exp = [fromisoformat(v).timestamp() for v in expected]
    assert a.tickValues(minVal, maxVal, 10000)[0][1] == exp


def test_tickValues_overflow(qtbot):
    """
    Check that the tickValues reimplementation returns the expected values
    """
    w = tpg.TaurusTrend()
    qtbot.addWidget(w)
    a = w.getPlotItem().axes["bottom"]["item"]
    # check that the datetime overflows do not break the call
    assert a.tickValues(-1e19, 0, 1e3) == [(1e19, [])]
    assert a.tickValues(0, 1e19, 1e3) == [(1e19, [])]


def _test_tickStrings(qtbot, values, expected):
    """
    Check that the tickStrings reimplementation returns the expected values
    """
    w = tpg.TaurusTrend()
    qtbot.addWidget(w)
    a = w.getPlotItem().axes["bottom"]["item"]

    dt = [fromisoformat(v).timestamp() for v in values]
    spacing = dt[-1] - dt[0]
    # check return values in the seconds scale
    assert a.tickStrings(dt, None, spacing) == expected


@pytest.mark.parametrize(
    "values,expected",
    [
        (
            ["2020-01-01T00:01", "2020-01-01T00:01:00.100"],
            ["[+000000ms]", "[+100000ms]"],
        ),
        (["2020-01-01", "2020-01-01T00:00:05"], ["00:00:00", "00:00:05"],),
        (["2020-01-01", "2020-01-01T00:00:30"], ["00:00:00", "00:00:30"],),
        (["2020-01-01", "2020-01-01T00:05"], ["00:00", "00:05"]),
        (["2020-01-01", "2023-01-01", "2037-01-01"], ["2020", "2023", "2037"]),
    ],
)
def test_tickStrings(qtbot, values, expected):
    """
    Check that the tickStrings reimplementation returns the expected values
    """
    _test_tickStrings(qtbot, values, expected)


@pytest.mark.skipif(not getlocale() \
                    or not getlocale()[0] \
                    or getlocale()[0][:2] != "en", reason="requires en* locale")
@pytest.mark.parametrize(
    "values,expected",
    [
        (["2020-01-01", "2020-01-01T05:00"], ["Jan/01-00h", "Jan/01-05h"],),
        (["2020-01-01", "2020-01-05"], ["Jan/01", "Jan/05"]),
        (["2020-01-01", "2020-05-01"], ["2020 Jan", "2020 May"]),
    ],
)
def test_tickStrings_english(qtbot, values, expected):
    """
    Check that the tickStrings reimplementation returns the expected values.
    Note: it assumes an English locale. Skipped otherwise.
    """
    _test_tickStrings(qtbot, values, expected)