Skip to content

Commit 592ebf7

Browse files
committed
Make it compression=None a valid case
This regression was cased by 78f5542 And missed during review. compression=None is the same as compression=False, but it is valid now.
1 parent ba3aeaa commit 592ebf7

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

cassandra/cluster.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ class Cluster(object):
685685
Used for testing new protocol features incrementally before the new version is complete.
686686
"""
687687

688-
compression: Union[bool, str] = True
688+
compression: Union[bool, str, None] = True
689689
"""
690690
Controls compression for communications between the driver and Cassandra.
691691
If left as the default of :const:`True`, either lz4 or snappy compression
@@ -695,7 +695,7 @@ class Cluster(object):
695695
You may also set this to 'snappy' or 'lz4' to request that specific
696696
compression type.
697697
698-
Setting this to :const:`False` disables compression.
698+
Setting this to :const:`False` or :const:`None` disables compression.
699699
"""
700700

701701
_application_info: Optional[ApplicationInfoBase] = None
@@ -1172,7 +1172,7 @@ def token_metadata_enabled(self, enabled):
11721172
def __init__(self,
11731173
contact_points=_NOT_SET,
11741174
port=9042,
1175-
compression: Union[bool, str] = True,
1175+
compression: Union[bool, str, None] = True,
11761176
auth_provider=None,
11771177
load_balancing_policy=None,
11781178
reconnection_policy=None,
@@ -1285,7 +1285,8 @@ def __init__(self,
12851285

12861286
self._resolve_hostnames()
12871287

1288-
if isinstance(compression, bool):
1288+
if isinstance(compression, bool) or compression is None:
1289+
compression = bool(compression)
12891290
if compression and not locally_supported_compressions:
12901291
log.error(
12911292
"Compression is enabled, but no compression libraries are available. "

tests/unit/test_cluster.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,51 @@ def test_port_range(self):
122122
with pytest.raises(ValueError):
123123
cluster = Cluster(contact_points=['127.0.0.1'], port=invalid_port)
124124

125+
def test_compression_autodisabled_without_libraries(self):
126+
with patch.dict('cassandra.cluster.locally_supported_compressions', {}, clear=True):
127+
with patch('cassandra.cluster.log') as patched_logger:
128+
cluster = Cluster(compression=True)
129+
130+
patched_logger.error.assert_called_once()
131+
assert cluster.compression is False
132+
133+
def test_compression_validates_requested_algorithm(self):
134+
with patch.dict('cassandra.cluster.locally_supported_compressions', {}, clear=True):
135+
with pytest.raises(ValueError):
136+
Cluster(compression='lz4')
137+
138+
with patch.dict('cassandra.cluster.locally_supported_compressions', {'lz4': ('c', 'd')}, clear=True):
139+
with patch('cassandra.cluster.log') as patched_logger:
140+
cluster = Cluster(compression='lz4')
141+
142+
patched_logger.error.assert_not_called()
143+
assert cluster.compression == 'lz4'
144+
145+
def test_compression_type_validation(self):
146+
with pytest.raises(TypeError):
147+
Cluster(compression=123)
148+
149+
def test_connection_factory_passes_compression_kwarg(self):
150+
endpoint = Mock(address='127.0.0.1')
151+
scenarios = [
152+
({}, True, False),
153+
({'snappy': ('c', 'd')}, True, True),
154+
({'lz4': ('c', 'd')}, 'lz4', 'lz4'),
155+
({'lz4': ('c', 'd'), 'snappy': ('c', 'd')}, False, False),
156+
({'lz4': ('c', 'd'), 'snappy': ('c', 'd')}, None, False),
157+
]
158+
159+
for supported, configured, expected in scenarios:
160+
with patch.dict('cassandra.cluster.locally_supported_compressions', supported, clear=True):
161+
with patch.object(Cluster.connection_class, 'factory', autospec=True, return_value='connection') as factory:
162+
cluster = Cluster(compression=configured)
163+
conn = cluster.connection_factory(endpoint)
164+
165+
assert conn == 'connection'
166+
assert factory.call_count == 1
167+
assert factory.call_args.kwargs['compression'] == expected
168+
assert cluster.compression == expected
169+
125170

126171
class SchedulerTest(unittest.TestCase):
127172
# TODO: this suite could be expanded; for now just adding a test covering a ticket

0 commit comments

Comments
 (0)