Changeset View
Changeset View
Standalone View
Standalone View
swh/journal/tests/test_replay.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import functools | import functools | ||||
import random | import random | ||||
from subprocess import Popen | from subprocess import Popen | ||||
from typing import Tuple | from typing import Tuple | ||||
import dateutil | import dateutil | ||||
from kafka import KafkaProducer | from kafka import KafkaProducer | ||||
from hypothesis import strategies, given, settings | |||||
from swh.storage import get_storage | from swh.storage import get_storage | ||||
from swh.storage.in_memory import ENABLE_ORIGIN_IDS | from swh.storage.in_memory import ENABLE_ORIGIN_IDS | ||||
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 | ||||
from swh.journal.replay import process_replay_objects | from swh.journal.replay import process_replay_objects, is_hash_in_bytearray | ||||
from .conftest import OBJECT_TYPE_KEYS | from .conftest import OBJECT_TYPE_KEYS | ||||
from .utils import MockedJournalClient, MockedKafkaWriter | from .utils import MockedJournalClient, MockedKafkaWriter | ||||
def test_storage_play( | def test_storage_play( | ||||
kafka_prefix: str, | kafka_prefix: str, | ||||
kafka_server: Tuple[Popen, int]): | kafka_server: Tuple[Popen, int]): | ||||
▲ Show 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | if ENABLE_ORIGIN_IDS: | ||||
}] | }] | ||||
else: | else: | ||||
assert visits == [{ | assert visits == [{ | ||||
'visit': 1, | 'visit': 1, | ||||
'origin': 'http://example.com/', | 'origin': 'http://example.com/', | ||||
'date': now, | 'date': now, | ||||
'type': 'git', | 'type': 'git', | ||||
}] | }] | ||||
hash_strategy = strategies.binary(min_size=20, max_size=20) | |||||
@settings(max_examples=500) | |||||
@given(strategies.sets(hash_strategy, min_size=0, max_size=500), | |||||
strategies.sets(hash_strategy, min_size=10)) | |||||
def test_is_hash_in_bytearray(haystack, needles): | |||||
array = b''.join(sorted(haystack)) | |||||
needles |= haystack # Exhaustively test for all objects in the array | |||||
for needle in needles: | |||||
assert is_hash_in_bytearray(needle, array, len(haystack)) == \ | |||||
(needle in haystack) |