Skip to content

Commit

Permalink
Tests: Slightly improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 3, 2024
1 parent 2e4e147 commit a2c0816
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
57 changes: 57 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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/",
Expand Down
15 changes: 14 additions & 1 deletion tests/test_timeutil.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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 (<class 'float'>)"
)
)

0 comments on commit a2c0816

Please sign in to comment.