B u `@srddlZddlZddlmmZddlmZddlTddlTddl m Z m Z ej ddgZ Gdddej Z dS) N)unittest)*)MappingSequencecategoryZnottestc@s.eZdZdZeejj7ZdddZddZdS) TestCaseaoA Pyomo-specific class whose instances are single test cases. This class derives from unittest.TestCase and provides the following additional functionality: - extended suport for test categories - additional assertions: * :py:meth:`assertStructuredAlmostEqual` unittest.TestCase documentation ------------------------------- NFc Cstdd|||fDdkr$td|dk r6d| }|dk rB|}|dkrV|dkrVd}d} y|||||| WnR|jk r} z2||dt| ||tj|tj|f} Wdd} ~ XYnX| r|| dS) a Test that first and second are equal up to a tolerance This compares first and second using both an absolute (`abstol`) and relative (`reltol`) tolerance. It will recursively descend into Sequence and Mapping containers (allowing for the relative comparison of structured data including lists and dicts). `places` and `delta` is supported for compatibility with assertAlmostEqual. If `places` is supplied, `abstol` is computed as `10**-places`. `delta` is an alias for `abstol`. If none of {`abstol`, `reltol`, `places`, `delta`} are specified, `reltol` defaults to 1e-7. If `allow_second_superset` is True, then: - only key/value pairs found in mappings in `first` are compared to `second` (allowing mappings in `second` to contain extra keys) - only values found in sequences in `first` are compared to `second` (allowing sequences in `second` to contain extra values) The relative error is computed for numerical values as `abs(first - second) / max(abs(first), abs(second))`, only when first != second (thereby avoiding divide-by-zero errors). Parameters ---------- first: the first value to compare second: the second value to compare places: int `first` and `second` are considered equivalent if their difference is between `places` decimal places; equivalent to `abstol = 10**-places` (included for compatibility with assertAlmostEqual) msg: str the message to raise on failure delta: float alias for `abstol` abstol: float the absolute tolerance. `first` and `second` are considered equivalent if their absolute difference is less than `abstol` reltol: float the relative tolerance. `first` and `second` are considered equivalent if their absolute difference divided by the larget of `first` and `second` is less than `reltol` allow_second_superset: bool If True, then extra entries in containers found on second will not trigger a failure. css|]}|dk rdVqdS)N).0_r r 9/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/unittest.py esz7TestCase.assertStructuredAlmostEqual..rz7Cannot specify more than one of {places, delta, abstol}N gHz>z_%s Found when comparing with tolerance (abs=%s, rel=%s): first=%s second=%s) sum ValueError_assertStructuredAlmostEqualfailureExceptionZ_formatMessagestr _unittestcase safe_repr) selffirstsecondZplacesmsgdeltareltolabstolZallow_second_supersetZfailer r r assertStructuredAlmostEqual*s.;  $z$TestCase.assertStructuredAlmostEqualc Csf||f}tdd|Dr|rHt|t|krH|dt|t|fx|D]}||krr|dtj|fy||||||||WqN|jk r}z"|dt|tj|fWdd}~XYqNXqNWdStdd|Dr||kr@dSn>tdd|Dr|rHt|t|krH|d t|t|fxtt t ||D]b\} \} } y|| | |||Wn<|jk r}z|d t|| fWdd}~XYnXqXWdS||krdSybt |} t |} t | | } |dk r| |krdS|dk r0| t t | t | |kr0dSWn YnX|d tj|tj|fdS) z7Recursive implementation of assertStructuredAlmostEqualcss|]}t|tVqdS)N) isinstancer)r r r r r r sz8TestCase._assertStructuredAlmostEqual..z'mappings are different sizes (%s != %s)z'key (%s) from first not found in secondz"%s Found when comparing key %sNcss|]}t|tjVqdS)N)r six string_types)r r r r r r scss|]}t|tVqdS)N)r r)r r r r r r sz(sequences are different sizes (%s != %s)z%s Found at position %sz %s !~= %s)alllenrrrrrrany enumeratezipfloatabsmax) rrrrrexactargskeyrifsZdiffr r r rsj . *  & z%TestCase._assertStructuredAlmostEqual)NNNNNF)__name__ __module__ __qualname____doc__rrrrr r r r rs    Xr)mathr!Zpyutilib.th.pyunitthZpyunitZ_pyunitrrZ pyutilib.thZpyomo.common.collectionsrr__all__rr r r r  s