velofy/tests/db/fake_data/factories/utils.py
Alexander Hess 042256eb99
All checks were successful
CI / Tests (Python 3.13) (push) Successful in 1m17s
Switch to Python 3.13
The dependencies stay in the same series as on Python 3.8
where those support 3.13.

Only the unavoidable upgrades are taken:
 - `matplotlib` to v3.9
 - `numpy` to v2.1
 - `pandas` to v2.2
 - `scipy` (via statsmodels)

Required code changes:
- `numpy` v2.1 prints its scalars as `np.float64(...)`,
  which `psycopg2` then writes into the SQL
  => Adjust `Forecast.from_dataframe()` to convert the
     rounded values into plain `float`s
- Python 3.12 no longer accepts `float`s in `random.randint()`
  => The test factories now pass the seconds as `int`s

CI and `nox` use a `u`v-managed Python, which ships the C headers
needed to build `psycopg2` and `rpy2`.

`flake8` v4 does not run on Python 3.12+
and is upgraded in a future commit.
2026-11-25 11:30:33 +01:00

29 lines
807 B
Python

"""Utilities used in all `*Factory` classes."""
import datetime as dt
import random
from tests import config as test_config
def random_timespan( # noqa:WPS211
*,
min_hours=0,
min_minutes=0,
min_seconds=0,
max_hours=0,
max_minutes=0,
max_seconds=0,
):
"""A randomized `timedelta` object between the specified arguments."""
total_min_seconds = min_hours * 3600 + min_minutes * 60 + min_seconds
total_max_seconds = max_hours * 3600 + max_minutes * 60 + max_seconds
return dt.timedelta(
seconds=random.randint(int(total_min_seconds), int(total_max_seconds))
)
def early_in_the_morning():
"""A randomized `datetime` object early in the morning."""
early = dt.datetime(*test_config.DATE, 3, 0)
return early + random_timespan(max_hours=2)