cosapp.utils.testing¶
Utility functions for testing purposes
Functions
|
Factory creating a dummy system class with custom attributes. |
|
Utility function to compare two objects for equality. |
|
Asserts that all the elements of a collecion are of a given type |
|
Assert that actual and expected dictionaries are identical, within given tolerance bounds. |
|
Utility function to test dictionary keys |
|
Utility function to collect args and kwargs in a tuple |
|
Utility function to test if a dictionary has the given keys |
|
No-op context manager yielding expected data as a dictionary. |
|
Context manager to assert that a block does not raise exception_cls. |
Utility function to pickle and unpickle a System object. |
|
|
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.