Skip to content

Commit

Permalink
Merge pull request #58 from khaeru/feature/assert-units
Browse files Browse the repository at this point in the history
Add `.testing.assert_units()`
  • Loading branch information
khaeru committed Mar 31, 2022
2 parents 81f1ead + 63deeeb commit 42a56cf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,10 @@ Utilities

.. automodule:: genno.util
:members:

Utilities for testing
=====================

.. automodule:: genno.testing
:members:
:exclude-members: parametrize_quantity_class
6 changes: 4 additions & 2 deletions doc/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ What's new
:backlinks: none
:depth: 1

.. Next release
.. ============
Next release
============

- Add :func:`.testing.assert_units` (:pull:`58`).

v1.9.2 (2022-03-03)
===================
Expand Down
16 changes: 11 additions & 5 deletions genno/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def add_test_data(c: Computer):
}


def get_test_quantity(key):
def get_test_quantity(key: Key) -> Quantity:
"""Computation that returns test data."""
value, unit = _TEST_DATA[key]
return Quantity(value, name=key.name, units=unit)
Expand Down Expand Up @@ -160,10 +160,9 @@ def assert_logs(caplog, message_or_messages=None, at_level=None):
Example
-------
def test_foo(caplog):
with assert_logs(caplog, 'a message'):
logging.getLogger(__name__).info('this is a message!')
>>> def test_foo(caplog):
... with assert_logs(caplog, 'a message'):
... logging.getLogger(__name__).info('this is a message!')
Parameters
----------
Expand Down Expand Up @@ -320,6 +319,13 @@ def assert_qty_allclose(
assert a.attrs == b.attrs


def assert_units(qty: Quantity, exp: str) -> None:
"""Assert that `qty` has units `exp`."""
assert (
qty.units / qty.units._REGISTRY(exp)
).dimensionless, f"Units '{qty.units:~}'; expected {repr(exp)}"


@pytest.fixture(params=["AttrSeries", "SparseDataArray"])
def parametrize_quantity_class(request):
"""Fixture to run tests twice, for both Quantity implementations."""
Expand Down
12 changes: 11 additions & 1 deletion genno/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import pytest

from genno.testing import assert_logs, assert_qty_allclose, assert_qty_equal
from genno import Quantity
from genno.testing import (
assert_logs,
assert_qty_allclose,
assert_qty_equal,
assert_units,
)

log = logging.getLogger(__name__)

Expand All @@ -17,6 +23,10 @@ def test_assert_logs(caplog):
log.warning("spam and eggs")


def test_assert_units():
assert_units(Quantity(), "")


def test_assert_check_type():
"""Mismatched types in :func:`assert_qty_equal` and :func:`assert_qty_allclose`."""
with pytest.raises(AssertionError):
Expand Down

0 comments on commit 42a56cf

Please sign in to comment.