Verifying custom implementations#
Contracts are abstract test suites, shipped with the library, that describe what every implementation of a part has
to do. If you write your own part, e.g. a KvStorage for your database, a contract tells
you whether it behaves as the library expects.
An implementation is valid if all the tests of its contract pass.
Available contracts#
Each contract is mixed with a test case class: unittest.TestCase or, when some of its use cases are async,
unittest.IsolatedAsyncioTestCase.
Contract |
Verifies |
Test case class |
|---|---|---|
Channel repositories, whatever their storages, listeners, queues or custom repositories |
|
|
Channel classes: managing connections and delivering messages |
|
|
Channel classes, when queues and listeners fail |
|
|
|
|
|
|
|
eric_sse.contracts.doubles holds the failing parts and recording error handlers the contracts use. You can
reuse them in your own tests.
How to verify an implementation#
For a step by step guide, with the repository contract as example, see How to test a custom channel repository.
Every contract takes a ChannelRepositoryBuilder as fixture, so you only need to write a
builder configured with your parts:
Subclass the contract together with its test case class.
Implement
create_builder(), returning a builder that uses your parts. It is called once per test.ReliableQueueContract.create_builderalso receives the visibility timeout to give your reliable queues.Implement
channel_data(), returning the data passed to the repository’screatefor the builder’s channel class, withoutchannel_id.Run it with your test runner, e.g.
pytest test_my_storage.py.
from unittest import TestCase
from eric_sse.builder import ChannelRepositoryBuilder
from eric_sse.contracts.repository import ChannelRepositoryContract
from eric_sse.prefabs import SSEChannel
from my_package import RedisStorage
class RedisStorageTestCase(ChannelRepositoryContract, TestCase):
def create_builder(self) -> ChannelRepositoryBuilder:
return ChannelRepositoryBuilder(SSEChannel, RedisStorage())
def channel_data(self) -> dict:
return {'retry_timeout_milliseconds': 10}
Contracts are not TestCase themselves, so test runners do not collect the abstract suite.
Rules#
Start every test from empty storages. Some tests check that a repository returns exactly what was persisted. With an external storage, either clean it in
setUp/tearDown, or havecreate_builder()return a builder over a new namespace (e.g. a random key prefix) each time.Do not redefine or skip contract tests. A skipped or redefined test is not a passed one, so the implementation cannot be considered valid.
You can add your own tests to the same class: they do not affect the contract.
See Contracts for the contract classes.