@@ -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
126171class SchedulerTest (unittest .TestCase ):
127172 # TODO: this suite could be expanded; for now just adding a test covering a ticket
0 commit comments