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

Unique FakeServer when no connection params are provided #142

Merged
merged 2 commits into from
Apr 27, 2023
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
9 changes: 7 additions & 2 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import queue
import threading
import time
import uuid
import warnings
import weakref
from collections import defaultdict
Expand Down Expand Up @@ -51,7 +52,7 @@ def __init__(self, *args, **kwargs):
self.server_key = path
else:
host, port = kwargs.get('host'), kwargs.get('port')
self.server_key = 'shared' if host is None or port is None else f'{host}:{port}'
self.server_key = uuid.uuid4().hex if host is None or port is None else f'{host}:{port}'
self.server_key += f'v{version}'
self._server = FakeServer.get_server(self.server_key, version=version)
self._server.connected = connected
Expand Down Expand Up @@ -125,7 +126,11 @@ def __init__(self, *args, server=None, connected=True, version=7, **kwargs):
parameters = inspect.signature(redis.Redis.__init__).parameters
parameter_names = list(parameters.keys())
default_args = parameters.values()
kwds = {p.name: p.default for p in default_args if p.default != inspect.Parameter.empty}
ignore_default_param_values = {'host', 'port', 'db'}
kwds = {p.name: p.default
for p in default_args
if (p.default != inspect.Parameter.empty
and p.name not in ignore_default_param_values)}
kwds.update(kwargs)
if not kwds.get('connection_pool', None):
charset = kwds.get('charset', None)
Expand Down
32 changes: 16 additions & 16 deletions 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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "fakeredis"
packages = [
{ include = "fakeredis" },
]
version = "2.11.1"
version = "2.11.2"
description = "Fake implementation of redis API for testing purposes."
readme = "README.md"
keywords = ["redis", "RedisJson", ]
Expand Down
11 changes: 11 additions & 0 deletions test/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ def test_asyncioio_is_used():
def test_unknown_command(r):
with pytest.raises(redis.ResponseError):
raw_command(r, '0 3 3')


def test_new_server_when_no_params():
from fakeredis import FakeRedis

fake_redis_1 = FakeRedis()
fake_redis_2 = FakeRedis()

fake_redis_1.set("foo", "bar")

assert fake_redis_2.get("foo") is None
25 changes: 9 additions & 16 deletions test/test_init_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def test_multidb(r, create_redis):
class TestInitArgs:
def test_singleton(self):
shared_server = fakeredis.FakeServer()
r1 = fakeredis.FakeStrictRedis()
r2 = fakeredis.FakeStrictRedis(server=fakeredis.FakeServer())
r3 = fakeredis.FakeStrictRedis(server=shared_server)
r4 = fakeredis.FakeStrictRedis(server=shared_server)
r1 = fakeredis.FakeRedis()
r2 = fakeredis.FakeRedis(server=fakeredis.FakeServer())
r3 = fakeredis.FakeRedis(server=shared_server)
r4 = fakeredis.FakeRedis(server=shared_server)

r1.set('foo', 'bar')
r3.set('bar', 'baz')
Expand Down Expand Up @@ -66,13 +66,9 @@ def test_from_url_user_password(self):
assert db.get('foo') == b'bar'

def test_from_url_with_db_arg(self):
db = fakeredis.FakeStrictRedis.from_url(
'redis://localhost:6379/0')
db1 = fakeredis.FakeStrictRedis.from_url(
'redis://localhost:6379/1')
db2 = fakeredis.FakeStrictRedis.from_url(
'redis://localhost:6379/',
db=2)
db = fakeredis.FakeStrictRedis.from_url('redis://localhost:6379/0')
db1 = fakeredis.FakeStrictRedis.from_url('redis://localhost:6379/1')
db2 = fakeredis.FakeStrictRedis.from_url('redis://localhost:6379/', db=2)
db.set('foo', 'foo0')
db1.set('foo', 'foo1')
db2.set('foo', 'foo2')
Expand All @@ -82,14 +78,11 @@ def test_from_url_with_db_arg(self):

def test_from_url_db_value_error(self):
# In case of ValueError, should default to 0, or be absent in redis-py 4.0
db = fakeredis.FakeStrictRedis.from_url(
'redis://localhost:6379/a')
db = fakeredis.FakeStrictRedis.from_url('redis://localhost:6379/a')
assert db.connection_pool.connection_kwargs.get('db', 0) == 0

def test_can_pass_through_extra_args(self):
db = fakeredis.FakeStrictRedis.from_url(
'redis://localhost:6379/0',
decode_responses=True)
db = fakeredis.FakeStrictRedis.from_url('redis://localhost:6379/0', decode_responses=True)
db.set('foo', 'bar')
assert db.get('foo') == 'bar'

Expand Down