@@ -122,6 +122,55 @@ 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+ ]
157+
158+ for supported , configured , expected in scenarios :
159+ with patch .dict ('cassandra.cluster.locally_supported_compressions' , supported , clear = True ):
160+ with patch .object (Cluster .connection_class , 'factory' , autospec = True , return_value = 'connection' ) as factory :
161+ cluster = Cluster (compression = configured )
162+ conn = cluster .connection_factory (endpoint )
163+
164+ assert conn == 'connection'
165+ assert factory .call_count == 1
166+ assert factory .call_args .kwargs ['compression' ] == expected
167+ assert cluster .compression == expected
168+
169+ def test_compression_none_not_allowed (self ):
170+ with patch .dict ('cassandra.cluster.locally_supported_compressions' , {'lz4' : ('c' , 'd' )}, clear = True ):
171+ with pytest .raises (TypeError ):
172+ Cluster (compression = None )
173+
125174
126175class SchedulerTest (unittest .TestCase ):
127176 # TODO: this suite could be expanded; for now just adding a test covering a ticket
0 commit comments