Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Jan 8, 2025
1 parent b75aac6 commit dbf4d81
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 64 deletions.
7 changes: 4 additions & 3 deletions tests/fakeredis/test/test_hypothesis/_server_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fakeredis._server import _create_version
from typing import Any, Tuple, Union
import redis
from typing import Tuple, Union

import pytest
import redis
from fakeredis._server import _create_version


def server_info() -> Tuple[str, Union[None, Tuple[int, ...]]]:
Expand Down
41 changes: 2 additions & 39 deletions tests/fakeredis/test/test_hypothesis/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import functools
import math
import operator
import sys
from typing import Any

Expand Down Expand Up @@ -41,7 +40,6 @@ def sample_attr(draw, name):
lambda x: repr(default_normalize(x)).encode(), st.floats(width=32)
)
counts = st.integers(min_value=-3, max_value=3) | ints
limits = st.just(()) | st.tuples(st.just("limit"), counts, counts)
# Redis has an integer overflow bug in swapdb, so we confine the numbers to
# a limited range (https://github.com/antirez/redis/issues/5737).
dbnums = st.integers(min_value=0, max_value=3) | st.integers(
Expand All @@ -51,11 +49,9 @@ def sample_attr(draw, name):
patterns = st.text(
alphabet=st.sampled_from("[]^$*.?-azAZ\\\r\n\t")
) | st.binary().filter(lambda x: b"\0" not in x)
score_tests = scores | st.builds(lambda x: b"(" + repr(x).encode(), scores)
string_tests = st.sampled_from([b"+", b"-"]) | st.builds(
operator.add, st.sampled_from([b"(", b"["]), fields
)

# Redis has integer overflow bugs in time computations, which is why we set a maximum.

expires_seconds = st.integers(min_value=100000, max_value=MAX_INT)
expires_ms = st.integers(min_value=100000000, max_value=MAX_INT)

Expand Down Expand Up @@ -211,39 +207,6 @@ def commands(*args, **kwargs):
| commands(st.just("sort"), keys, *zero_or_more("asc", "desc", "alpha"))
)


def build_zstore(command, dest, sources, weights, aggregate):
args = [command, dest, len(sources)]
args += [source[0] for source in sources]
if weights:
args.append("weights")
args += [source[1] for source in sources]
if aggregate:
args += ["aggregate", aggregate]
return Command(args)


zset_no_score_create_commands = commands(
st.just("zadd"), keys, st.lists(st.tuples(st.just(0), fields), min_size=1)
)
zset_no_score_commands = ( # TODO: test incr
commands(
st.just("zadd"),
keys,
*zero_or_more("nx", "xx", "ch", "incr"),
st.lists(st.tuples(st.just(0), fields)),
)
| commands(st.just("zlexcount"), keys, string_tests, string_tests)
| commands(
st.sampled_from(["zrangebylex", "zrevrangebylex"]),
keys,
string_tests,
string_tests,
limits,
)
| commands(st.just("zremrangebylex"), keys, string_tests, string_tests)
)

attrs = st.fixed_dictionaries(
{
"keys": st.lists(st.binary(), min_size=2, max_size=5, unique=True),
Expand Down
3 changes: 2 additions & 1 deletion tests/fakeredis/test/test_hypothesis/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from test.test_hypothesis.base import BaseTest, commands, values, common_commands
import hypothesis.strategies as st

from test.test_hypothesis.base import BaseTest, commands, values, common_commands


class TestConnection(BaseTest):
# TODO: tests for select
Expand Down
7 changes: 4 additions & 3 deletions tests/fakeredis/test/test_hypothesis/test_hash.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
values,
keys,
common_commands,
counts,
int_as_bytes,
fields,
ints,
expires_seconds,
)
import hypothesis.strategies as st


class TestHash(BaseTest):
Expand Down
2 changes: 1 addition & 1 deletion tests/fakeredis/test/test_hypothesis/test_joint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from turtle import st
import hypothesis.strategies as st

from test.test_hypothesis.base import BaseTest, common_commands, commands
from test.test_hypothesis.test_connection import TestConnection
Expand Down
9 changes: 3 additions & 6 deletions tests/fakeredis/test/test_hypothesis/test_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
values,
keys,
common_commands,
counts,
int_as_bytes,
fields,
)
import hypothesis.strategies as st
from test.test_hypothesis.test_string import TestString


class TestServer(BaseTest):
Expand Down
6 changes: 2 additions & 4 deletions tests/fakeredis/test/test_hypothesis/test_set.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
values,
keys,
common_commands,
counts,
int_as_bytes,
fields,
)
import hypothesis.strategies as st


class TestSet(BaseTest):
Expand Down
7 changes: 6 additions & 1 deletion tests/fakeredis/test/test_hypothesis/test_string.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
Expand All @@ -6,8 +8,11 @@
common_commands,
counts,
int_as_bytes,
zero_or_more,
ints,
expires_seconds,
expires_ms,
)
import hypothesis.strategies as st

optional_bitcount_range = st.just(()) | st.tuples(int_as_bytes, int_as_bytes)

Expand Down
9 changes: 6 additions & 3 deletions tests/fakeredis/test/test_hypothesis/test_transaction.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
values,
keys,
common_commands,
counts,
int_as_bytes,
fields,
zero_or_more,
ints,
expires_seconds,
expires_ms,
)
import hypothesis.strategies as st
from test.test_hypothesis.test_string import TestString


class TestTransaction(BaseTest):
Expand Down
49 changes: 46 additions & 3 deletions tests/fakeredis/test/test_hypothesis/test_zset.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
import operator

import hypothesis.strategies as st

from test.test_hypothesis.base import (
BaseTest,
commands,
values,
keys,
common_commands,
counts,
int_as_bytes,
fields,
zero_or_more,
scores,
optional,
Command,
float_as_bytes,
)
import hypothesis.strategies as st

score_tests = scores | st.builds(lambda x: b"(" + repr(x).encode(), scores)
limits = st.just(()) | st.tuples(st.just("limit"), counts, counts)
string_tests = st.sampled_from([b"+", b"-"]) | st.builds(
operator.add, st.sampled_from([b"(", b"["]), fields
)
zset_no_score_create_commands = commands(
st.just("zadd"), keys, st.lists(st.tuples(st.just(0), fields), min_size=1)
)
zset_no_score_commands = ( # TODO: test incr
commands(
st.just("zadd"),
keys,
*zero_or_more("nx", "xx", "ch", "incr"),
st.lists(st.tuples(st.just(0), fields)),
)
| commands(st.just("zlexcount"), keys, string_tests, string_tests)
| commands(
st.sampled_from(["zrangebylex", "zrevrangebylex"]),
keys,
string_tests,
string_tests,
limits,
)
| commands(st.just("zremrangebylex"), keys, string_tests, string_tests)
)


def build_zstore(command, dest, sources, weights, aggregate) -> Command:
args = [command, dest, len(sources)]
args += [source[0] for source in sources]
if weights:
args.append("weights")
args += [source[1] for source in sources]
if aggregate:
args += ["aggregate", aggregate]
return Command(args)


class TestZSet(BaseTest):
Expand Down

0 comments on commit dbf4d81

Please sign in to comment.