diff --git a/README.rst b/README.rst index 398bebb..3201454 100644 --- a/README.rst +++ b/README.rst @@ -4,6 +4,10 @@ .. image:: https://img.shields.io/pypi/status/grafanimate.svg :target: https://pypi.org/project/grafanimate/ +.. image:: https://codecov.io/gh/grafana-toolbox/grafanimate/branch/main/graph/badge.svg + :target: https://codecov.io/gh/grafana-toolbox/grafanimate + :alt: Code coverage + .. image:: https://img.shields.io/pypi/l/grafanimate.svg :alt: License :target: https://pypi.org/project/grafanimate/ diff --git a/tests/test_model.py b/tests/test_model.py index a860cd7..a6cff2c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,11 +1,14 @@ +import re from datetime import datetime, timezone +import pytest from dateutil.relativedelta import relativedelta from dateutil.rrule import DAILY, MINUTELY from dateutil.tz import tzutc from freezegun import freeze_time from grafanimate.model import AnimationScenario, AnimationSequence, SequencingMode +from grafanimate.timeutil import RecurrenceInfo def test_sequence_datetime(): @@ -173,6 +176,60 @@ def test_sequence_relative_with_now(): ] +@freeze_time("2021-11-19T20:34:17Z") +def test_sequence_recurrence(): + seq = AnimationSequence( + start="-7d", + stop="now", + recurrence=RecurrenceInfo( + frequency=DAILY, interval=1, duration=relativedelta(days=+1, seconds=-1) + ), + ) + + assert seq.start == datetime(2021, 11, 12, 20, 34, 17, tzinfo=tzutc()) + assert seq.stop == datetime(2021, 11, 19, 20, 34, 17, tzinfo=tzutc()) + + assert seq.recurrence.every is None + assert seq.recurrence.frequency == DAILY + assert seq.recurrence.interval == 1 + assert seq.recurrence.duration == relativedelta(days=+1, seconds=-1) + + assert list(seq.get_timeranges_isoformat()) == [ + "2021-11-12T20:34:17+00:00/2021-11-13T20:34:16+00:00", + "2021-11-13T20:34:17+00:00/2021-11-14T20:34:16+00:00", + "2021-11-14T20:34:17+00:00/2021-11-15T20:34:16+00:00", + "2021-11-15T20:34:17+00:00/2021-11-16T20:34:16+00:00", + "2021-11-16T20:34:17+00:00/2021-11-17T20:34:16+00:00", + "2021-11-17T20:34:17+00:00/2021-11-18T20:34:16+00:00", + "2021-11-18T20:34:17+00:00/2021-11-19T20:34:16+00:00", + "2021-11-19T20:34:17+00:00/2021-11-20T20:34:16+00:00", + ] + + +def test_sequence_needs_recurrence_or_every(): + with pytest.raises(ValueError) as ex: + AnimationSequence( + start="-7d", + stop="now", + ) + assert ex.match("Parameter `every` is mandatory when `recurrence` is not given") + + +@freeze_time("2021-11-19T20:34:17Z") +def test_sequence_start_greater_than_stop(): + with pytest.raises(ValueError) as ex: + AnimationSequence( + start="+1d", + stop="-1d", + every="10m", + ) + assert ex.match( + re.escape( + "Timestamp start=2021-11-20T20:34:17+00:00 is after stop=2021-11-18T20:34:17+00:00" + ) + ) + + def test_scenario_basic(): scenario = AnimationScenario( grafana_url="https://daq.example.org/grafana/", diff --git a/tests/test_timeutil.py b/tests/test_timeutil.py index c700d15..5db8459 100644 --- a/tests/test_timeutil.py +++ b/tests/test_timeutil.py @@ -1,7 +1,10 @@ +import re + +import pytest from dateutil.relativedelta import relativedelta from dateutil.rrule import DAILY, HOURLY, MINUTELY, MONTHLY, SECONDLY, WEEKLY, YEARLY -from grafanimate.timeutil import get_freq_delta +from grafanimate.timeutil import convert_absolute_timestamp, get_freq_delta def test_freq_delta_legacy(): @@ -131,3 +134,13 @@ def test_freq_delta_pytimeparse(): assert recurrence.frequency == YEARLY assert recurrence.interval == 3 assert recurrence.duration == relativedelta(years=+3, months=+5, seconds=-1) + + +def test_convert_absolute_timestamp_wrong_type(): + with pytest.raises(TypeError) as ex: + convert_absolute_timestamp(42.42) + assert ex.match( + re.escape( + "Unknown data type for `start` or `stop` value: 42.42 ()" + ) + )