Utils¶
AsyncCallQueue¶
Async callqueue wrapper. Stores information about each function call to synchronized queue.
@callinfo.acallqueue
async def func(a, b):
return a + b
await func(1, 2)
>>> func.has_calls
True
>>> func.times_called
1
>>> func.next_call()
{'a': 1, 'b': 2}
- class testsuite.utils.callinfo.AsyncCallQueue(func: Callable, *, checker: Optional[Callable[[str], None]] = None)[source]¶
Function wrapper that puts information about function call into async queue.
This class provides methods to wait/check function underlying function calls.
- testsuite.utils.callinfo.acallqueue(func: Callable, *, checker: Optional[Callable[[str], None]] = None) AsyncCallQueue [source]¶
Turn function into async call queue.
- Parameters:
func – async or sync callable, can be decorated with @staticmethod
checker – optional function to check whether or not operation on callqueue is possible
Matching¶
Testsuite provides utility to perform inexact pattern matching. This might be useful when comparing objects. For instance when checking HTTP handle response. Special objects with custom __eq__() method are used for pattern matching. You can use them instead of explicit value when comparing objects.
Here is example assertion on order/create handle response when you do not need to know exact order id returned by handle.
from testsuite.utils import matching
def test_order_create(...):
response = await client.post('/order/create')
assert response.status_code == 200
assert response.json() == {
'order_id': matching.uuid_string,
...
}
String matching¶
Regular expressions¶
Predicates¶
any_string
, matches any stringdatetime_string
, matches any datetime string using dateutil.parseruuid_string
, string is uuid, e.g.: d08535a5904f4790bd8f95c51c1f3cbeobjectid_string
, String is Mongo objectid, e.g 5e64beab56d0bf70bd8eebcb
Integer matching¶
Gt¶
Ge¶
Lt¶
Le¶
Predicates¶
any_float
any_integer
any_numeric
positive_float
positive_integer
positive_numeric
negative_float
negative_integer
negative_numeric
non_negative_float
non_negative_integer
non_negative_numeric
Type matching¶
- class testsuite.utils.matching.IsInstance(types)[source]¶
Match value by its type.
Use this class when you only need to check value type.
assert response.json() == { # order_id must be a string 'order_id': matching.IsInstance(str), # int or float is acceptable here 'weight': matching.IsInstance([int, float]), ... }
Logical matching¶
And¶
Or¶
Not¶
Partial dict¶
- class testsuite.utils.matching.PartialDict(*args, **kwargs)[source]¶
Partial dictionary comparison.
Sometimes you only need to check dictionary subset ignoring all other keys.
PartialDict
is there for this purpose.PartialDict is wrapper around regular dict() when instantiated all arguments are passed as is to internal dict object.
Example:
assert {'foo': 1, 'bar': 2} == matching.PartialDict({ # Only check for foo >= 1 ignoring other keys 'foo': matching.Ge(1), })
Unordered list¶
- testsuite.utils.matching.unordered_list(sequence, *, key=None)[source]¶
Unordered list comparison.
You may want to compare lists without respect to order. For instance, when your service is serializing std::unordered_map to array.
unordered_list can help you with that. It sorts both array before comparison.
- Parameters:
sequence – Initial sequence
key – Sorting key function
Example:
assert [3, 2, 1] == matching.unordered_list([1, 2, 3])