civiqhub_selene.testing

Contract kit: prove in ten lines that an estimator honours the contract.

Write a pytest class with three methods; it inherits seven tests:

from civiqhub_selene.testing import EstimatorContract

class TestMean(EstimatorContract):
    def make_estimator(self):
        return Mean()

    def make_data(self):
        return Numbers((1.0, 2.0))  # an input on which a value exists

    def make_empty_data(self):
        return Numbers(())  # an input with no answer, or None if there is none

A test of the kit that fails raises ContractError, whose message names the estimator and the rule it breaks. The kit runs under pytest but the package does not depend on it: pytest is imported only when a test needs it.

class civiqhub_selene.testing.EstimatorContract[source]

Bases: object

Base class of a pytest class that checks one estimator against the contract.

Write make_estimator(), make_data() and make_empty_data(); the seven test_* methods are inherited. Name the subclass Test... so that pytest collects it.

make_estimator()[source]

Return an estimator ready to use.

Return type:

Estimator[Any, Any]

make_data()[source]

Return an input on which a value exists.

Return type:

Any

make_empty_data()[source]

Return an input with no possible answer, or None if the method always answers.

Return type:

Any

test_identity_is_declared()[source]

The class declares its name, its version and a reference.

Return type:

None

test_estimator_is_a_frozen_value()[source]

The estimator is a frozen dataclass, equal to a copy of itself, and hashable.

Return type:

None

test_returns_a_result()[source]

estimate returns an available Result on the data of make_data.

Return type:

None

test_deterministic()[source]

Two calls with the same seed give the same fingerprints.

Return type:

None

test_provenance_is_complete()[source]

The provenance names the engine, the input, the settings and the packages.

Return type:

None

test_absence_is_explicit()[source]

An input with no answer gives an Unavailable with a detail, never None.

Return type:

None

test_rejects_foreign_input()[source]

An input of another type raises InvalidInputError.

Return type:

None

civiqhub_selene.testing.fingerprint_of(value)[source]

Return a stable fingerprint of a value: 16 hexadecimal characters.

value.fingerprint() when the object has one, otherwise the SHA-256 of its canonical JSON.

Raises:

ValueError – If the value holds NaN or an infinity, which have no canonical form.

Parameters:

value (object)

Return type:

str