Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor fakeredis tests #3852

Merged
merged 8 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/test-fakeredis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ jobs:
run: |
poetry run pytest test/ \
--ignore test/test_hypothesis.py \
--ignore test/test_geo_commands.py \
--ignore test/test_bitmap_commands.py \
--ignore test/test_json/ \
--ignore test/test_mixins/test_bitmap_commands.py \
--junit-xml=results-tests.xml --html=report-tests.html -v
Expand Down
50 changes: 25 additions & 25 deletions tests/fakeredis/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/fakeredis/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ maintainers = [
[tool.poetry.dependencies]
python = "^3.10"
redis = ">=5"
fakeredis = { version = "^2.23", extras = ["json", "bf", "cf", "lua"] }
fakeredis = { version = "^2.25", extras = ["json", "bf", "cf", "lua"] }
hypothesis = "^6.111"
pytest = "^8.3"
pytest-timeout = "^2.3.1"
Expand Down
129 changes: 1 addition & 128 deletions tests/fakeredis/test/test_asyncredis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import re
import sys

if sys.version_info >= (3, 11):
Expand All @@ -11,14 +10,10 @@
import redis
import redis.asyncio

from fakeredis import FakeServer, aioredis, FakeAsyncRedis, FakeStrictRedis
from fakeredis import FakeServer, aioredis
from test import testtools


pytestmark = []
fake_only = pytest.mark.parametrize(
"async_redis", [pytest.param("fake", marks=pytest.mark.fake)], indirect=True
)
pytestmark.extend(
[
pytest.mark.asyncio,
Expand Down Expand Up @@ -183,31 +178,6 @@ async def test_failed_script_error7(self, async_redis):
await async_redis.eval('return redis.call("ZCOUNT", KEYS[1])', 1, "foo")


@testtools.run_test_if_redispy_ver("gte", "5.1")
async def test_repr_redis_51(async_redis: redis.asyncio.Redis):
assert re.fullmatch(
r"<redis.asyncio.connection.ConnectionPool("
r"<fakeredis.aioredis.FakeConnection(server=<fakeredis._server.FakeServer object at .*>,db=0)>)>",
repr(async_redis.connection_pool),
)


@fake_only
@pytest.mark.disconnected
async def test_not_connected(async_redis: redis.asyncio.Redis):
with pytest.raises(redis.asyncio.ConnectionError):
await async_redis.ping()


@fake_only
async def test_disconnect_server(async_redis, fake_server):
await async_redis.ping()
fake_server.connected = False
with pytest.raises(redis.asyncio.ConnectionError):
await async_redis.ping()
fake_server.connected = True


async def test_type(async_redis: redis.asyncio.Redis):
await async_redis.set("string_key", "value")
await async_redis.lpush("list_key", "value")
Expand Down Expand Up @@ -238,78 +208,6 @@ async def test_xdel(async_redis: redis.asyncio.Redis):
assert await async_redis.xdel(stream, m2, m3) == 2


@pytest.mark.fake
async def test_from_url():
r0 = aioredis.FakeRedis.from_url("redis://localhost?db=0")
r1 = aioredis.FakeRedis.from_url("redis://localhost?db=1")
# Check that they are indeed different databases
await r0.set("foo", "a")
await r1.set("foo", "b")
assert await r0.get("foo") == b"a"
assert await r1.get("foo") == b"b"
await r0.connection_pool.disconnect()
await r1.connection_pool.disconnect()


@pytest.mark.fake
async def test_from_url_with_version():
r0 = aioredis.FakeRedis.from_url("redis://localhost?db=0", version=(6,))
r1 = aioredis.FakeRedis.from_url("redis://localhost?db=1", version=(6,))
# Check that they are indeed different databases
await r0.set("foo", "a")
await r1.set("foo", "b")
assert await r0.get("foo") == b"a"
assert await r1.get("foo") == b"b"
await r0.connection_pool.disconnect()
await r1.connection_pool.disconnect()


@fake_only
async def test_from_url_with_server(async_redis, fake_server):
r2 = aioredis.FakeRedis.from_url("redis://localhost", server=fake_server)
await async_redis.set("foo", "bar")
assert await r2.get("foo") == b"bar"
await r2.connection_pool.disconnect()


@pytest.mark.fake
async def test_without_server():
r = aioredis.FakeRedis()
assert await r.ping()


@pytest.mark.fake
async def test_without_server_disconnected():
r = aioredis.FakeRedis(connected=False)
with pytest.raises(redis.asyncio.ConnectionError):
await r.ping()


@pytest.mark.fake
async def test_async():
# arrange
cache = aioredis.FakeRedis()
# act
await cache.set("fakeredis", "plz")
x = await cache.get("fakeredis")
# assert
assert x == b"plz"


@testtools.run_test_if_redispy_ver("gte", "4.4.0")
@pytest.mark.parametrize("nowait", [False, True])
@pytest.mark.fake
async def test_connection_disconnect(nowait):
server = FakeServer()
r = aioredis.FakeRedis(server=server)
conn = await r.connection_pool.get_connection("_")
assert conn is not None

await conn.disconnect(nowait=nowait)

assert conn._sock is None


async def test_connection_with_username_and_password():
server = FakeServer()
r = aioredis.FakeRedis(server=server, username="username", password="password")
Expand All @@ -320,31 +218,6 @@ async def test_connection_with_username_and_password():
assert result.decode() == test_value


@pytest.mark.fake
async def test_init_args():
sync_r1 = FakeStrictRedis()
r1 = FakeAsyncRedis()
r5 = FakeAsyncRedis()
r2 = FakeAsyncRedis(server=FakeServer())

shared_server = FakeServer()
r3 = FakeAsyncRedis(server=shared_server)
r4 = FakeAsyncRedis(server=shared_server)

await r1.set("foo", "bar")
await r3.set("bar", "baz")

assert await r1.get("foo") == b"bar"
assert await r5.get("foo") is None
assert sync_r1.get("foo") is None
assert await r2.get("foo") is None
assert await r3.get("foo") is None

assert await r3.get("bar") == b"baz"
assert await r4.get("bar") == b"baz"
assert await r1.get("bar") is None


@pytest.mark.asyncio
async def test_cause_fakeredis_bug(async_redis):
if sys.version_info < (3, 11):
Expand Down
Loading
Loading