Skip to content

Commit

Permalink
lint with black
Browse files Browse the repository at this point in the history
  • Loading branch information
kjaymiller committed Jun 27, 2024
1 parent 93bee9b commit 1130170
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 37 deletions.
62 changes: 47 additions & 15 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def test_repr_contains_db_info_unix(self):
expected = "path=abc,db=0,client_name=test-client"
assert expected in repr(pool)


@pytest.mark.parametrize("connection_protocol", ["valkey", "redis"])
class TestConnectionPoolURLParsing:
def test_hostname(self, connection_protocol):
Expand All @@ -311,7 +312,9 @@ def test_hostname(self, connection_protocol):
assert pool.connection_kwargs == {"host": "my.host"}

def test_quoted_hostname(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://my %2F host %2B%3D+")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://my %2F host %2B%3D+"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "my / host +=+"}

Expand All @@ -322,7 +325,9 @@ def test_port(self, connection_protocol):

@skip_if_server_version_lt("6.0.0")
def test_username(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://myuser:@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://myuser:@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "username": "myuser"}

Expand All @@ -338,7 +343,9 @@ def test_quoted_username(self, connection_protocol):
}

def test_password(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://:mypassword@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://:mypassword@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "password": "mypassword"}

Expand All @@ -354,7 +361,9 @@ def test_quoted_password(self, connection_protocol):

@skip_if_server_version_lt("6.0.0")
def test_username_and_password(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://myuser:mypass@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://myuser:mypass@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {
"host": "localhost",
Expand All @@ -363,17 +372,23 @@ def test_username_and_password(self, connection_protocol):
}

def test_db_as_argument(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 1}

def test_db_in_path(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost/2", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost/2", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 2}

def test_db_in_querystring(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost/2?db=3", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost/2?db=3", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 3}

Expand All @@ -393,7 +408,6 @@ def test_extra_typed_querystring_options(self, connection_protocol):
}
assert pool.max_connections == 10


def test_client_name_in_querystring(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://location?client_name=test-client"
Expand All @@ -407,19 +421,24 @@ def test_invalid_extra_typed_querystring_options(self, connection_protocol):
)

def test_extra_querystring_options(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost?a=1&b=2")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost?a=1&b=2"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "a": "1", "b": "2"}

def test_calling_from_subclass_returns_correct_instance(self, connection_protocol):
pool = valkey.BlockingConnectionPool.from_url(f"{connection_protocol}://localhost")
pool = valkey.BlockingConnectionPool.from_url(
f"{connection_protocol}://localhost"
)
assert isinstance(pool, valkey.BlockingConnectionPool)

def test_client_creates_connection_pool(self, connection_protocol):
r = valkey.Valkey.from_url(f"{connection_protocol}://myhost")
assert r.connection_pool.connection_class == valkey.Connection
assert r.connection_pool.connection_kwargs == {"host": "myhost"}


def test_invalid_scheme_raises_error():
with pytest.raises(ValueError) as cm:
valkey.ConnectionPool.from_url("localhost")
Expand All @@ -428,6 +447,7 @@ def test_invalid_scheme_raises_error():
"(valkey://, valkeys://, redis://, rediss://, unix://)"
)


def test_boolean_parsing():
for expected, value in (
(None, None),
Expand All @@ -448,6 +468,7 @@ def test_boolean_parsing():
):
assert expected is to_bool(value)


class TestBlockingConnectionPoolURLParsing:
def test_extra_typed_querystring_options(self):
pool = valkey.BlockingConnectionPool.from_url(
Expand Down Expand Up @@ -542,6 +563,7 @@ def test_extra_querystring_options(self):
assert pool.connection_class == valkey.UnixDomainSocketConnection
assert pool.connection_kwargs == {"path": "/socket", "a": "1", "b": "2"}


@pytest.mark.skipif(not SSL_AVAILABLE, reason="SSL not installed")
@pytest.mark.parametrize("connection_protocol", ["valkeys", "rediss"])
class TestSSLConnectionURLParsing:
Expand All @@ -557,19 +579,29 @@ class DummyConnectionPool(valkey.ConnectionPool):
def get_connection(self, *args, **kwargs):
return self.make_connection()

pool = DummyConnectionPool.from_url(f"{connection_protocol}://?ssl_cert_reqs=none")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=none"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_NONE

pool = DummyConnectionPool.from_url(f"{connection_protocol}://?ssl_cert_reqs=optional")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=optional"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_OPTIONAL

pool = DummyConnectionPool.from_url(f"{connection_protocol}://?ssl_cert_reqs=required")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=required"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_REQUIRED

pool = DummyConnectionPool.from_url(f"{connection_protocol}://?ssl_check_hostname=False")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_check_hostname=False"
)
assert pool.get_connection("_").check_hostname is False

pool = DummyConnectionPool.from_url(f"{connection_protocol}://?ssl_check_hostname=True")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_check_hostname=True"
)
assert pool.get_connection("_").check_hostname is True


Expand Down
68 changes: 49 additions & 19 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def test_repr_contains_db_info_unix(self):
expected = "path=abc,db=0,client_name=test-client"
assert expected in repr(pool)


@pytest.mark.parametrize("connection_protocol", ["valkey", "redis"])
class TestConnectionPoolURLParsing:
def test_hostname(self, connection_protocol):
Expand All @@ -204,7 +205,9 @@ def test_hostname(self, connection_protocol):
assert pool.connection_kwargs == {"host": "my.host"}

def test_quoted_hostname(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://my %2F host %2B%3D+")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://my %2F host %2B%3D+"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "my / host +=+"}

Expand All @@ -215,7 +218,9 @@ def test_port(self, connection_protocol):

@skip_if_server_version_lt("6.0.0")
def test_username(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://myuser:@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://myuser:@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "username": "myuser"}

Expand All @@ -231,7 +236,9 @@ def test_quoted_username(self, connection_protocol):
}

def test_password(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://:mypassword@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://:mypassword@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "password": "mypassword"}

Expand All @@ -247,7 +254,9 @@ def test_quoted_password(self, connection_protocol):

@skip_if_server_version_lt("6.0.0")
def test_username_and_password(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://myuser:mypass@localhost")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://myuser:mypass@localhost"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {
"host": "localhost",
Expand All @@ -256,17 +265,23 @@ def test_username_and_password(self, connection_protocol):
}

def test_db_as_argument(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 1}

def test_db_in_path(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost/2", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost/2", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 2}

def test_db_in_querystring(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost/2?db=3", db=1)
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost/2?db=3", db=1
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "db": 3}

Expand Down Expand Up @@ -299,7 +314,9 @@ def test_invalid_extra_typed_querystring_options(self, connection_protocol):
)

def test_extra_querystring_options(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://localhost?a=1&b=2")
pool = valkey.ConnectionPool.from_url(
f"{connection_protocol}://localhost?a=1&b=2"
)
assert pool.connection_class == valkey.Connection
assert pool.connection_kwargs == {"host": "localhost", "a": "1", "b": "2"}

Expand All @@ -312,6 +329,7 @@ def test_client_creates_connection_pool(self, connection_protocol):
assert r.connection_pool.connection_class == valkey.Connection
assert r.connection_pool.connection_kwargs == {"host": "myhost"}


def test_invalid_scheme_raises_error():
with pytest.raises(ValueError) as cm:
valkey.ConnectionPool.from_url("localhost")
Expand All @@ -320,6 +338,7 @@ def test_invalid_scheme_raises_error():
"(valkey://, valkeys://, redis://, rediss://, unix://)"
)


def test_invalid_scheme_raises_error_when_double_slash_missing():
with pytest.raises(ValueError) as cm:
valkey.ConnectionPool.from_url("valkey:foo.bar.com:12345")
Expand All @@ -328,6 +347,7 @@ def test_invalid_scheme_raises_error_when_double_slash_missing():
"(valkey://, valkeys://, redis://, rediss://, unix://)"
)


def test_boolean_parsing():
for expected, value in (
(None, None),
Expand Down Expand Up @@ -454,42 +474,52 @@ class MyConnection(valkey.UnixDomainSocketConnection):


@pytest.mark.skipif(not SSL_AVAILABLE, reason="SSL not installed")
@pytest.mark
@pytest.mark.parametrize("connection_protocol", ["valkeys", "rediss"])
class TestSSLConnectionURLParsing:
def test_host(self):
pool = valkey.ConnectionPool.from_url("valkeys://my.host")
def test_host(self, connection_protocol):
pool = valkey.ConnectionPool.from_url(f"{connection_protocol}://my.host")
assert pool.connection_class == valkey.SSLConnection
assert pool.connection_kwargs == {"host": "my.host"}

def test_connection_class_override(self):
def test_connection_class_override(self, connection_protocol):
class MyConnection(valkey.SSLConnection):
pass

pool = valkey.ConnectionPool.from_url(
"valkeys://my.host", connection_class=MyConnection
f"{connection_protocol}://my.host", connection_class=MyConnection
)
assert pool.connection_class == MyConnection

def test_cert_reqs_options(self):
def test_cert_reqs_options(self, connection_protocol):
import ssl

class DummyConnectionPool(valkey.ConnectionPool):
def get_connection(self, *args, **kwargs):
return self.make_connection()

pool = DummyConnectionPool.from_url("valkeys://?ssl_cert_reqs=none")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=none"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_NONE

pool = DummyConnectionPool.from_url("valkeys://?ssl_cert_reqs=optional")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=optional"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_OPTIONAL

pool = DummyConnectionPool.from_url("valkeys://?ssl_cert_reqs=required")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_cert_reqs=required"
)
assert pool.get_connection("_").cert_reqs == ssl.CERT_REQUIRED

pool = DummyConnectionPool.from_url("valkeys://?ssl_check_hostname=False")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_check_hostname=False"
)
assert pool.get_connection("_").check_hostname is False

pool = DummyConnectionPool.from_url("valkeys://?ssl_check_hostname=True")
pool = DummyConnectionPool.from_url(
f"{connection_protocol}://?ssl_check_hostname=True"
)
assert pool.get_connection("_").check_hostname is True


Expand Down
3 changes: 1 addition & 2 deletions valkey/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ def parse_url(url: str) -> ConnectKwargs:
"rediss://",
"unix://",
)

if not any([url.startswith(scheme) for scheme in valid_schemes]):
raise ValueError(
"Valkey URL must specify one of the following "
Expand Down Expand Up @@ -1080,7 +1080,6 @@ def parse_url(url: str) -> ConnectKwargs:
if parsed.scheme in ("valkeys", "rediss"):
kwargs["connection_class"] = SSLConnection


return kwargs


Expand Down
2 changes: 1 addition & 1 deletion valkey/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def parse_url(url):
"rediss://",
"unix://",
)

if not any([url.startswith(scheme) for scheme in valid_schemes]):
raise ValueError(
"Valkey URL must specify one of the following "
Expand Down

0 comments on commit 1130170

Please sign in to comment.