cosapp.utils.testing

Utility functions for testing purposes

Functions

DummySystemFactory(classname[, base])

Factory creating a dummy system class with custom attributes.

are_same(o1, o2)

Utility function to compare two objects for equality.

assert_all_type(collection, dtype)

Asserts that all the elements of a collecion are of a given type

assert_close_dict(actual, expected[, abs, rel])

Assert that actual and expected dictionaries are identical, within given tolerance bounds.

assert_keys(dictionary, *keys)

Utility function to test dictionary keys

get_args(*args, **kwargs)

Utility function to collect args and kwargs in a tuple

has_keys(dictionary, *keys)

Utility function to test if a dictionary has the given keys

no_exception(**expected_data)

No-op context manager yielding expected data as a dictionary.

not_raised(exception_cls)

Context manager to assert that a block does not raise exception_cls.

pickle_roundtrip(s)

Utility function to pickle and unpickle a System object.

rel_error(actual, expected)

Computes the relative error of actual compared to expected

cosapp.utils.testing.DummySystemFactory(classname: str, base: type[System] | None = None, **settings) type[System][source]

Factory creating a dummy system class with custom attributes. System is “dummy” in the sense it has no compute, and no connectors.

Parameters:

  • classname [str]: Output class name

  • base [type[System], optional]:

    Base class, derived from System. If not provided (default), base is System.

  • **settings [dict[str, args_kwargs]]:

    Class characteristics, as a dictionary. Keys are attribute names (e.g. inputs); values are (args, kwargs) forwarded to the associated method (e.g. add_input).

Possible Attributes:

  • inputs

  • outputs

  • inwards

  • outwards

  • modevars_in

  • modevars_out

  • transients

  • rates

  • properties

  • children

  • events

  • unknowns

  • equations

  • targets

  • design_methods

Examples:

>>> from cosapp.utils.testing import DummySystemFactory, get_args
>>>
>>> Dummy = DummySystemFactory(
>>>     inwards=[
>>>         get_args('h', 0.1, unit='m'),
>>>         get_args('L', 2.0, unit='m'),
>>>     ],
>>>     outwards=[
>>>         get_args('b_ratio', 0.0),
>>>     ],
>>>     events=[
>>>         get_args('kaboom', trigger='h > L / 2')
>>>     ],
>>>     properties=[
>>>         get_args('n', 12),
>>>     ],
>>>     equations=[
>>>         "b_ratio == 1",
>>>     ],
>>>     unknowns=[
>>>         "h",
>>>     ],
>>> )
>>>
>>> s = Dummy('s')
>>> assert s.assembled_problem().shape == (1, 1)
cosapp.utils.testing.are_same(o1: Driver | System | Distribution, o2: Driver | System | Distribution) bool[source]

Utility function to compare two objects for equality. It serializes both objects to JSON and compares the resulting strings.

cosapp.utils.testing.assert_all_type(collection, dtype)[source]

Asserts that all the elements of a collecion are of a given type

cosapp.utils.testing.assert_close_dict(actual: dict, expected: dict, abs=None, rel=None) None[source]

Assert that actual and expected dictionaries are identical, within given tolerance bounds. Works recursively with nested dictionaries.

cosapp.utils.testing.assert_keys(dictionary, *keys)[source]

Utility function to test dictionary keys

cosapp.utils.testing.get_args(*args, **kwargs) tuple[tuple[Any, ...], dict[str, Any]][source]

Utility function to collect args and kwargs in a tuple

cosapp.utils.testing.has_keys(dictionary: dict, *keys) bool[source]

Utility function to test if a dictionary has the given keys

cosapp.utils.testing.no_exception(**expected_data)[source]

No-op context manager yielding expected data as a dictionary. Raises AssertionError if any exception is raised within the context.

cosapp.utils.testing.not_raised(exception_cls: type[Exception])[source]

Context manager to assert that a block does not raise exception_cls.

cosapp.utils.testing.pickle_roundtrip(s: System) System[source]

Utility function to pickle and unpickle a System object. This is useful for testing serialization and deserialization of systems.

cosapp.utils.testing.rel_error(actual: Number | Iterable, expected: Number | Iterable) float | ndarray[source]

Computes the relative error of actual compared to expected