Skip to content

Add tests for MarketUtil.parse_date and fix its utcnow() deprecation - #81

Open
propcgamer20-png wants to merge 2 commits into
cuemacro:masterfrom
propcgamer20-png:marketutil-tests-and-utcnow
Open

propcgamer20-png wants to merge 2 commits into
cuemacro:masterfrom
propcgamer20-png:marketutil-tests-and-utcnow

Conversation

@propcgamer20-png

Copy link
Copy Markdown

Toward #71 ("Increase test coverage").

MarketUtil.parse_date (src/finmarketpy/util/marketutil.py) had no test coverage. Adds tests/test_marketutil.py (13 tests) covering:

  • the relative-keyword forms: midnight, decade, year, month, week, day, hour
  • the four documented string date formats (%b %d %Y %H:%M, %d %b %Y %H:%M, %b %d %Y, %d %b %Y)
  • non-string passthrough (pd.Timestamp / datetime / date)
  • the pd.Timestamp return type

Also replaces the deprecated datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - a drop-in that preserves the existing naive-UTC semantics (so pd.Timestamp(date1) stays tz-naive as before).

$ python -m pytest tests/ -q
18 passed

Stacked on #79 (lazy-chartpy-import) - without it from finmarketpy.util.marketutil import MarketUtil can't be imported (and no test collects) when the optional chartpy plotting stack is unavailable. Once #79 merges, this PR's diff narrows to just the two files here.

Toward #71

… without it

`import finmarketpy` - and therefore `pytest` collecting any test - hard
failed whenever the optional `chartpy` plotting stack could not be
imported (e.g. a plotly version mismatch inside chartpy). Five modules
imported `chartpy` at module load:

  economics/quickchart.py, economics/report.py,
  backtest/backtestcomparison.py, backtest/backtestengine.py,
  backtest/tradeanalysis.py

and finmarketpy/__init__ eagerly imports all of them, so the whole test
suite errored at collection ("Interrupted: 1 error during collection").

Changes:
  - quickchart.py: import Chart / Style locally in the methods that draw
    (dropped the unused ChartConstants import).
  - report.py / backtestcomparison.py / backtestengine.py /
    tradeanalysis.py: wrap the top-level `from chartpy import ...` in
    try/except ImportError, falling back to None, and guard the
    class-body `ChartConstants()` / `Style()` evaluations and
    TradeAnalysis.__init__'s default engine arg.

When chartpy is installed, behaviour is unchanged. When it is missing or
broken, the modules import (chart attributes are None) and only actually
drawing a chart fails. `pytest tests/` now collects and passes (5 passed,
was 0 collected / 1 error). `ruff check` on the touched files is
unchanged (81 pre-existing findings, 0 added).

Closes cuemacro#71
MarketUtil.parse_date had no test coverage. Adds tests/test_marketutil.py
covering the relative-keyword forms ("midnight", "year", "week", "day",
"hour", "decade"), the four documented string date formats, the
non-string passthrough, and the Timestamp return type.

Also replaces the deprecated `datetime.datetime.utcnow()` (removed in a
future Python) with `datetime.datetime.now(datetime.timezone.utc)
.replace(tzinfo=None)`, which keeps the existing naive-UTC semantics.

Toward cuemacro#71 (increase test coverage). Stacked on the chartpy lazy-import
change so the tests can collect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant