mockify.cardinality - Classes for setting expected call cardinality

Module containing set of classes to be used with mockify.engine.Expectation.times() method.

You can also create your own classes to be used with that method. The only thing required from such class is to implement following interface:

is_satisfied(self, actual)

Return True if actual call count is satisfied by self, or False otherwise.

Here, actual is absolute call count expectation received so far. It is completely implementation-specific of which values of actual are said to be satisfied and which are not. For example, Exactly will compare actual with fixed value (given via constructor) and return True only if those two are equal.

adjust_by(self, minimal)

Adjust self by current minimal expected call count and return new instance of type(self).

In some complex expectation there could be a situation in which expectation must be computed again. This is not visible for library user, but must be done behind the scenes to properly process expectations. Such situation can be presented in this example:

>>> from mockify.actions import Return
>>> from mockify.mock import Function
>>> foo = Function('foo')
>>> foo.expect_call(1, 2).will_once(Return(1)).will_repeatedly(Return(2)).times(2)
<mockify.Expectation: foo(1, 2)>
>>> foo(1, 2)
1
>>> foo(1, 2)
2
>>> foo(1, 2)
2
>>> foo.assert_satisfied()

In example above we’ve used times(2) to tell that last repeated action is expected to be called twice, but real expected call count is 3 times, as will_once is used. Behind the scenes, this is recalculated using this metho.

format_expected(self)

Return textual representation of expected call count.

This is used by mockify.exc.Unsatisfied exception when error message is being rendered.

minimal(sefl) (property)

Property containing minimal call count that is considered valid for given instance.

For example, for AtLeast or Exactly it would be just its constructor argument, for :class`AtMost` it will be 0, for Between it will be its minimal argument.

class mockify.cardinality.AtLeast(minimal)

Bases: object

Used to set minimal expected call count.

If this is used, then expectation is said to be satisfied if actual call count is not less that minimal.

Parameters:minimal – Integer value representing minimal expected call count
class mockify.cardinality.AtMost(maximal)

Bases: object

Used to set maximal expected call count.

If this is used, then expectation is said to be satisfied if actual call count is not greater than maximal.

Parameters:maximal – Integer value representing maximal expected call count
class mockify.cardinality.Between(minimal, maximal)

Bases: object

Used to set a range of valid call counts.

If this is used, then expectation is said to be satisfied if actual call count is not less than minimal and not greater than maximal.

Parameters:
  • minimal – Integer value representing minimal expected call count
  • maximal – Integer value representing maximal expected call count
class mockify.cardinality.Exactly(expected)

Bases: object

Used to expect fixed call count to be made.

You do not have to use this class explicitly as its instances are automatically created when you call times method with integer value as argument.

Parameters:expected – Integer value representing expected call count