diff --git a/tests/integration/test_query.py b/tests/integration/test_query.py index d95e710d..2b80cfe8 100644 --- a/tests/integration/test_query.py +++ b/tests/integration/test_query.py @@ -44,3 +44,129 @@ class QueryTest(unittest.TestCase): str(statement.trace) for event in statement.trace.events: str(event) + +class PreparedStatementTests(unittest.TestCase): + + def test_routing_key(self): + """ + Simple code coverage to ensure routing_keys can be accessed + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare( + """ + INSERT INTO test3rf.test (k, v) VALUES (?, ?) + """) + + self.assertIsInstance(prepared, PreparedStatement) + bound = prepared.bind((1, None)) + self.assertEqual(bound.routing_key, '\x00\x00\x00\x01') + + def test_empty_routing_key_indexes(self): + """ + Ensure when routing_key_indexes are blank, + the routing key should be None + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare( + """ + INSERT INTO test3rf.test (k, v) VALUES (?, ?) + """) + prepared.routing_key_indexes = None + + self.assertIsInstance(prepared, PreparedStatement) + bound = prepared.bind((1, None)) + self.assertEqual(bound.routing_key, None) + + def test_predefined_routing_key(self): + """ + Basic test that ensures _set_routing_key() + overrides the current routing key + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare( + """ + INSERT INTO test3rf.test (k, v) VALUES (?, ?) + """) + + self.assertIsInstance(prepared, PreparedStatement) + bound = prepared.bind((1, None)) + bound._set_routing_key('fake_key') + self.assertEqual(bound.routing_key, 'fake_key') + + def test_multiple_routing_key_indexes(self): + """ + Basic test that uses a fake routing_key_index + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare( + """ + INSERT INTO test3rf.test (k, v) VALUES (?, ?) + """) + prepared.routing_key_indexes = {0: {0: 0}, 1: {1: 1}} + + self.assertIsInstance(prepared, PreparedStatement) + bound = prepared.bind((1, 2)) + self.assertEqual(bound.routing_key, '\x04\x00\x00\x00\x04\x00\x00\x00') + + def test_bound_keyspace(self): + """ + Ensure that bound.keyspace works as expected + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare( + """ + INSERT INTO test3rf.test (k, v) VALUES (?, ?) + """) + + self.assertIsInstance(prepared, PreparedStatement) + bound = prepared.bind((1, 2)) + self.assertEqual(bound.keyspace, 'test3rf') + + bound.prepared_statement.column_metadata = None + self.assertEqual(bound.keyspace, None) + +class PrintStatementTests(unittest.TestCase): + """ + Test that shows the format used when printing Statements + """ + + def test_simple_statement(self): + """ + Highlight the format of printing SimpleStatements + """ + + ss = SimpleStatement('SELECT * FROM test3rf.test') + self.assertEqual(str(ss), + '') + + def test_prepared_statement(self): + """ + Highlight the difference between Prepared and Bound statements + """ + + cluster = Cluster() + session = cluster.connect() + + prepared = session.prepare('INSERT INTO test3rf.test (k, v) VALUES (?, ?)') + + self.assertEqual(str(prepared), + '') + + bound = prepared.bind((1, 2)) + self.assertEqual(str(bound), + '') diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py index 13efd27b..0ade788e 100644 --- a/tests/unit/test_connection.py +++ b/tests/unit/test_connection.py @@ -93,7 +93,7 @@ class ConnectionTest(unittest.TestCase): c.defunct = Mock() # read in a SupportedMessage response - header = self.make_header_prefix(SupportedMessage, version=0x04) + header = self.make_header_prefix(SupportedMessage) options = self.make_options_body() message = self.make_msg(header, options) c.process_msg(message, -13) @@ -127,3 +127,18 @@ class ConnectionTest(unittest.TestCase): c.defunct.assert_called_once_with(ANY) args, kwargs = c.defunct.call_args self.assertIsInstance(args[0], ProtocolError) + + def test_not_implemented(self): + """ + Ensure the following methods throw NIE's. If not, come back and test them. + """ + + c = self.make_connection() + + self.assertRaises(NotImplementedError, c.close) + self.assertRaises(NotImplementedError, c.defunct, None) + self.assertRaises(NotImplementedError, c.send_msg, None, None) + self.assertRaises(NotImplementedError, c.wait_for_response, None) + self.assertRaises(NotImplementedError, c.wait_for_responses) + self.assertRaises(NotImplementedError, c.register_watcher, None, None) + self.assertRaises(NotImplementedError, c.register_watchers, None) diff --git a/tests/unit/test_metadata.py b/tests/unit/test_metadata.py index 1611f98e..74c9a605 100644 --- a/tests/unit/test_metadata.py +++ b/tests/unit/test_metadata.py @@ -1,10 +1,62 @@ import unittest import cassandra from cassandra.cluster import Cluster -from cassandra.metadata import TableMetadata, Murmur3Token, MD5Token, BytesToken +from cassandra.metadata import TableMetadata, Murmur3Token, MD5Token, BytesToken, ReplicationStrategy, NetworkTopologyStrategy, SimpleStrategy, LocalStrategy +from cassandra.policies import SimpleConvictionPolicy +from cassandra.pool import Host -class TestMetadata(unittest.TestCase): +class TestStrategies(unittest.TestCase): + + def test_replication_strategy(self): + """ + Basic code coverage testing that ensures different ReplicationStrategies + can be initiated using parameters correctly. + """ + + rs = ReplicationStrategy() + + self.assertEqual(rs.create('OldNetworkTopologyStrategy', None), None) + self.assertEqual(rs.create('xxxxxxOldNetworkTopologyStrategy', None), None) + + fake_options_map = {'options': 'map'} + self.assertIsInstance(rs.create('NetworkTopologyStrategy', fake_options_map), NetworkTopologyStrategy) + self.assertEqual(rs.create('NetworkTopologyStrategy', fake_options_map).dc_replication_factors, + NetworkTopologyStrategy(fake_options_map).dc_replication_factors) + + self.assertIsInstance(rs.create('xxxxxxNetworkTopologyStrategy', fake_options_map), NetworkTopologyStrategy) + self.assertEqual(rs.create('xxxxxxNetworkTopologyStrategy', fake_options_map).dc_replication_factors, + NetworkTopologyStrategy(fake_options_map).dc_replication_factors) + + fake_options_map = {'options': 'map'} + self.assertEqual(rs.create('SimpleStrategy', fake_options_map), None) + self.assertEqual(rs.create('xxxxxxSimpleStrategy', fake_options_map), None) + + fake_options_map = {'options': 'map'} + self.assertIsInstance(rs.create('LocalStrategy', fake_options_map), LocalStrategy) + self.assertIsInstance(rs.create('xxxxxxLocalStrategy', fake_options_map), LocalStrategy) + + fake_options_map = {'options': 'map', 'replication_factor': 3} + self.assertIsInstance(rs.create('SimpleStrategy', fake_options_map), SimpleStrategy) + self.assertEqual(rs.create('SimpleStrategy', fake_options_map).replication_factor, + SimpleStrategy(fake_options_map['replication_factor']).replication_factor) + + self.assertIsInstance(rs.create('xxxxxxSimpleStrategy', fake_options_map), SimpleStrategy) + self.assertEqual(rs.create('xxxxxxSimpleStrategy', fake_options_map).replication_factor, + SimpleStrategy(fake_options_map['replication_factor']).replication_factor) + + self.assertEqual(rs.create('xxxxxxxx', fake_options_map), None) + + self.assertRaises(NotImplementedError, rs.make_token_replica_map, None, None) + self.assertRaises(NotImplementedError, rs.export_for_schema) + + def test_nts(self): + # TODO: Cover NetworkTopologyStrategy.make_token_replica_map() + # TODO: Cover NetworkTopologyStrategy.export_for_schema() + pass + + +class TestTokens(unittest.TestCase): def test_protect_name(self): """ @@ -84,15 +136,18 @@ class TestMetadata(unittest.TestCase): murmur3_token = Murmur3Token(cassandra.metadata.MIN_LONG - 1) self.assertEqual(murmur3_token.hash_fn('123'), -7468325962851647638) self.assertEqual(murmur3_token.hash_fn(str(cassandra.metadata.MAX_LONG)), 7162290910810015547) + self.assertEqual(str(murmur3_token), '