Changeset View
Changeset View
Standalone View
Standalone View
swh/journal/tests/test_client.py
Show All 9 Lines | |||||
from confluent_kafka import Producer | from confluent_kafka import Producer | ||||
from swh.model.hypothesis_strategies import revisions | from swh.model.hypothesis_strategies import revisions | ||||
from swh.journal.client import JournalClient | from swh.journal.client import JournalClient | ||||
from swh.journal.serializers import key_to_kafka, value_to_kafka | from swh.journal.serializers import key_to_kafka, value_to_kafka | ||||
def test_client( | def test_client_objects( | ||||
kafka_prefix: str, | kafka_prefix: str, | ||||
kafka_server: Tuple[Popen, int]): | kafka_server: Tuple[Popen, int]): | ||||
(_, port) = kafka_server | (_, port) = kafka_server | ||||
kafka_prefix += '.swh.journal.objects' | kafka_prefix += '.swh.journal.objects' | ||||
producer = Producer({ | producer = Producer({ | ||||
'bootstrap.servers': 'localhost:{}'.format(port), | 'bootstrap.servers': 'localhost:{}'.format(port), | ||||
'client.id': 'test producer', | 'client.id': 'test producer', | ||||
Show All 16 Lines | config = { | ||||
'max_messages': 1, | 'max_messages': 1, | ||||
} | } | ||||
client = JournalClient(**config) | client = JournalClient(**config) | ||||
worker_fn = MagicMock() | worker_fn = MagicMock() | ||||
client.process(worker_fn) | client.process(worker_fn) | ||||
worker_fn.assert_called_once_with({'revision': [rev.to_dict()]}) | worker_fn.assert_called_once_with({'revision': [rev.to_dict()]}) | ||||
def test_client_metadata( | |||||
kafka_prefix: str, | |||||
kafka_server: Tuple[Popen, int]): | |||||
(_, port) = kafka_server | |||||
kafka_prefix += '.swh.journal.metadata' | |||||
producer = Producer({ | |||||
'bootstrap.servers': 'localhost:{}'.format(port), | |||||
'client.id': 'test producer', | |||||
'enable.idempotence': 'true', | |||||
}) | |||||
tool = { | |||||
b'name': b'some-tool', | |||||
b'version': b'some-version', | |||||
b'configuration': {b'debian-package': b'some-package'}, | |||||
} | |||||
key = { | |||||
b'origin': b'http://example.org/', | |||||
b'tool': tool, | |||||
} | |||||
douardda: Not sure this is really necessary. The key is not used in any way in this test, so an hardcoded… | |||||
value = { | |||||
'@context': 'https://doi.org/10.5063/schema/codemeta-2.0', | |||||
'type': 'SoftwareSourceCode', | |||||
'author': ['Someone'], | |||||
} | |||||
# Fill Kafka | |||||
producer.produce( | |||||
topic=kafka_prefix + '.origin_intrinsic_metadata', | |||||
key=key_to_kafka(key), | |||||
value=value_to_kafka(value), | |||||
) | |||||
producer.flush() | |||||
config = { | |||||
'brokers': 'localhost:%d' % kafka_server[1], | |||||
'group_id': 'replayer', | |||||
'prefix': kafka_prefix, | |||||
'max_messages': 1, | |||||
'object_types': ['origin_intrinsic_metadata'], | |||||
} | |||||
client = JournalClient(**config) | |||||
worker_fn = MagicMock() | |||||
client.process(worker_fn) | |||||
worker_fn.assert_called_once_with({'origin_intrinsic_metadata': [value]}) |
Not sure this is really necessary. The key is not used in any way in this test, so an hardcoded value like
would be enough and make the test easier to read. No need to 'obfuscate' the code with unneeded content.