| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import uuid |
| 12 |
|
| 13 |
import pytest |
| 14 |
from fakeredis import FakeRedis |
| 15 |
|
| 16 |
from hypothesis import settings, strategies as st |
| 17 |
from hypothesis.database import InMemoryExampleDatabase |
| 18 |
from hypothesis.errors import InvalidArgument |
| 19 |
from hypothesis.extra.redis import RedisExampleDatabase |
| 20 |
from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule |
| 21 |
|
| 22 |
from tests.cover.test_database_backend import _database_conforms_to_listener_api |
| 23 |
|
| 24 |
|
| 25 |
@pytest.mark.parametrize( |
| 26 |
"kw", |
| 27 |
[ |
| 28 |
{"redis": "not a redis instance"}, |
| 29 |
{"redis": FakeRedis(), "expire_after": 10}, # not a timedelta |
| 30 |
{"redis": FakeRedis(), "key_prefix": "not a bytestring"}, |
| 31 |
{"redis": FakeRedis(), "listener_channel": 2}, # not a str |
| 32 |
], |
| 33 |
) |
| 34 |
def test_invalid_args_raise(kw): |
| 35 |
with pytest.raises(InvalidArgument): |
| 36 |
RedisExampleDatabase(**kw) |
| 37 |
|
| 38 |
|
| 39 |
def test_all_methods(): |
| 40 |
db = RedisExampleDatabase(FakeRedis()) |
| 41 |
db.save(b"key1", b"value") |
| 42 |
assert list(db.fetch(b"key1")) == [b"value"] |
| 43 |
db.move(b"key1", b"key2", b"value") |
| 44 |
assert list(db.fetch(b"key1")) == [] |
| 45 |
assert list(db.fetch(b"key2")) == [b"value"] |
| 46 |
db.delete(b"key2", b"value") |
| 47 |
assert list(db.fetch(b"key2")) == [] |
| 48 |
db.delete(b"key2", b"unknown value") |
| 49 |
|
| 50 |
|
| 51 |
class DatabaseComparison(RuleBasedStateMachine): |
| 52 |
def __init__(self): |
| 53 |
super().__init__() |
| 54 |
server = FakeRedis(host=uuid.uuid4().hex) # Different (fake) server each time |
| 55 |
self.dbs = [InMemoryExampleDatabase(), RedisExampleDatabase(server)] |
| 56 |
|
| 57 |
keys = Bundle("keys") |
| 58 |
values = Bundle("values") |
| 59 |
|
| 60 |
@rule(target=keys, k=st.binary()) |
| 61 |
def k(self, k): |
| 62 |
return k |
| 63 |
|
| 64 |
@rule(target=values, v=st.binary()) |
| 65 |
def v(self, v): |
| 66 |
return v |
| 67 |
|
| 68 |
@rule(k=keys, v=values) |
| 69 |
def save(self, k, v): |
| 70 |
for db in self.dbs: |
| 71 |
db.save(k, v) |
| 72 |
|
| 73 |
@rule(k=keys, v=values) |
| 74 |
def delete(self, k, v): |
| 75 |
for db in self.dbs: |
| 76 |
db.delete(k, v) |
| 77 |
|
| 78 |
@rule(k1=keys, k2=keys, v=values) |
| 79 |
def move(self, k1, k2, v): |
| 80 |
for db in self.dbs: |
| 81 |
db.move(k1, k2, v) |
| 82 |
|
| 83 |
@rule(k=keys) |
| 84 |
def values_agree(self, k): |
| 85 |
last = None |
| 86 |
last_db = None |
| 87 |
for db in self.dbs: |
| 88 |
keys = set(db.fetch(k)) |
| 89 |
if last is not None: |
| 90 |
assert last == keys, (last_db, db) |
| 91 |
last = keys |
| 92 |
last_db = db |
| 93 |
|
| 94 |
|
| 95 |
TestDBs = DatabaseComparison.TestCase |
| 96 |
|
| 97 |
|
| 98 |
def flush_messages(db): |
| 99 |
# fake redis doesn't have the background polling for pubsub that an actual |
| 100 |
# redis server does, so we have to flush when we want them. |
| 101 |
if db._pubsub is None: |
| 102 |
return |
| 103 |
# arbitrarily high. |
| 104 |
for _ in range(100): |
| 105 |
db._pubsub.get_message() |
| 106 |
|
| 107 |
|
| 108 |
def test_redis_listener(): |
| 109 |
_database_conforms_to_listener_api( |
| 110 |
lambda _path: RedisExampleDatabase(FakeRedis()), |
| 111 |
flush=flush_messages, |
| 112 |
parent_settings=settings( |
| 113 |
max_examples=5, |
| 114 |
stateful_step_count=10, |
| 115 |
), |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def test_redis_listener_explicit(): |
| 120 |
calls = 0 |
| 121 |
|
| 122 |
def listener(event): |
| 123 |
nonlocal calls |
| 124 |
calls += 1 |
| 125 |
|
| 126 |
redis = FakeRedis() |
| 127 |
db = RedisExampleDatabase(redis) |
| 128 |
db.add_listener(listener) |
| 129 |
|
| 130 |
db.save(b"a", b"a") |
| 131 |
flush_messages(db) |
| 132 |
assert calls == 1 |
| 133 |
|
| 134 |
db.remove_listener(listener) |
| 135 |
db.delete(b"a", b"a") |
| 136 |
db.save(b"a", b"b") |
| 137 |
flush_messages(db) |
| 138 |
assert calls == 1 |
| 139 |
|
| 140 |
db.add_listener(listener) |
| 141 |
db.delete(b"a", b"b") |
| 142 |
db.save(b"a", b"c") |
| 143 |
flush_messages(db) |
| 144 |
assert calls == 3 |
| 145 |
|
| 146 |
db.save(b"a", b"c") |
| 147 |
flush_messages(db) |
| 148 |
assert calls == 3 |
| 149 |
|
| 150 |
|
| 151 |
def test_redis_move_from_key_without_value(): |
| 152 |
# explicit covering test for: |
| 153 |
# * moving a value from a key without that value |
| 154 |
redis = FakeRedis() |
| 155 |
db = RedisExampleDatabase(redis) |
| 156 |
db.save(b"a", b"x") |
| 157 |
db.save(b"b", b"x") |
| 158 |
db.move(b"a", b"b", b"y") |
| 159 |
|
| 160 |
|
| 161 |
def test_redis_move_into_key_with_value(): |
| 162 |
# explicit covering test for: |
| 163 |
# * moving a value into a key with that value |
| 164 |
redis = FakeRedis() |
| 165 |
db = RedisExampleDatabase(redis) |
| 166 |
db.save(b"a", b"y") |
| 167 |
db.save(b"b", b"x") |
| 168 |
db.move(b"a", b"b", b"x") |
| 169 |
|
| 170 |
|
| 171 |
def test_redis_move_to_same_key(): |
| 172 |
# explicit covering test for: |
| 173 |
# * moving a value where src == dest |
| 174 |
redis = FakeRedis() |
| 175 |
db = RedisExampleDatabase(redis) |
| 176 |
db.move(b"a", b"a", b"x") |
| 177 |
assert list(db.fetch(b"a")) == [b"x"] |
| 178 |
|
| 179 |
|
| 180 |
def test_redis_equality(): |
| 181 |
redis = FakeRedis() |
| 182 |
assert RedisExampleDatabase(redis) == RedisExampleDatabase(redis) |
| 183 |
# FakeRedis() != FakeRedis(), not much we can do here |
| 184 |
assert RedisExampleDatabase(FakeRedis()) != RedisExampleDatabase(FakeRedis()) |