def test_prepared_metadata_generation(self): """ Test to validate that result metadata is appropriately populated across protocol version Result metadata is set once upon the prepare, then re-used. This test ensures that it manifests it's self the same across multiple protocol versions. @since 3.6.0 @jira_ticket PYTHON-71 @expected_result result metadata is consistent. """ base_line = None for proto_version in get_supported_protocol_versions(): beta_flag = True if proto_version in ProtocolVersion.BETA_VERSIONS else False cluster = Cluster(protocol_version=proto_version, allow_beta_protocol_version=beta_flag) session = cluster.connect() select_statement = session.prepare("SELECT * FROM system.local") self.assertNotEqual(select_statement.result_metadata, None) future = session.execute_async(select_statement) results = future.result() if base_line is None: base_line = results[0]._asdict().keys() else: self.assertEqual(base_line, results[0]._asdict().keys()) cluster.shutdown()
def test_trace_ignores_row_factory(self): with Cluster(protocol_version=PROTOCOL_VERSION, execution_profiles={EXEC_PROFILE_DEFAULT: ExecutionProfile(row_factory=dict_factory)}) as cluster: s = cluster.connect() query = "SELECT * FROM system.local" statement = SimpleStatement(query) rs = s.execute(statement, trace=True) # Ensure this does not throw an exception trace = rs.get_query_trace() self.assertTrue(trace.events) str(trace) for event in trace.events: str(event)
def test_prepared_statement(self): """ Highlight the difference between Prepared and Bound statements """ cluster = Cluster(protocol_version=PROTOCOL_VERSION) session = cluster.connect() prepared = session.prepare('INSERT INTO test3rf.test (k, v) VALUES (?, ?)') prepared.consistency_level = ConsistencyLevel.ONE self.assertEqual(str(prepared), '<PreparedStatement query="INSERT INTO test3rf.test (k, v) VALUES (?, ?)", consistency=ONE>') bound = prepared.bind((1, 2)) self.assertEqual(str(bound), '<BoundStatement query="INSERT INTO test3rf.test (k, v) VALUES (?, ?)", values=(1, 2), consistency=ONE>') cluster.shutdown()
def test_prepare_on_all_hosts(self): """ Test to validate prepare_on_all_hosts flag is honored. Use a special ForcedHostSwitchPolicy to ensure prepared queries are cycled over nodes that should not have them prepared. Check the logs to insure they are being re-prepared on those nodes @since 3.4.0 @jira_ticket PYTHON-556 @expected_result queries will have to re-prepared on hosts that aren't the control connection # """ with Cluster(protocol_version=PROTOCOL_VERSION, prepare_on_all_hosts=False, reprepare_on_up=False, execution_profiles={ EXEC_PROFILE_DEFAULT: ExecutionProfile( load_balancing_policy=ForcedHostSwitchPolicy()) }) as cluster: session = cluster.connect(wait_for_all_pools=True) mock_handler = MockLoggingHandler() logger = logging.getLogger(dse.cluster.__name__) logger.addHandler(mock_handler) self.assertGreaterEqual(len(cluster.metadata.all_hosts()), 3) select_statement = session.prepare("SELECT * FROM system.local") reponse_first = session.execute(select_statement) reponse_second = session.execute(select_statement) reponse_third = session.execute(select_statement) self.assertEqual( len({ reponse_first.response_future.attempted_hosts[0], reponse_second.response_future.attempted_hosts[0], reponse_third.response_future.attempted_hosts[0] }), 3) self.assertEqual( 2, mock_handler.get_message_count('debug', "Re-preparing"))