Skip to content

Commit 5122080

Browse files
committed
test_query.py: Fix usage of MockLoggingHandler
Previous code was incorrect and couldn't possibly work.
1 parent 92abab2 commit 5122080

File tree

1 file changed

+65
-71
lines changed

1 file changed

+65
-71
lines changed

tests/integration/standard/test_query.py

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,6 @@ def test_prepared_metadata_generation(self):
503503

504504

505505
class PreparedStatementArgTest(unittest.TestCase):
506-
507-
def setUp(self):
508-
self.mock_handler = MockLoggingHandler()
509-
self.logger = logging.getLogger(cluster.__name__)
510-
self.logger.addHandler(self.mock_handler)
511-
512-
def tearDown(self):
513-
self.logger.removeHandler(self.mock_handler)
514-
515506
def test_prepare_on_all_hosts(self):
516507
"""
517508
Test to validate prepare_on_all_hosts flag is honored.
@@ -523,14 +514,15 @@ def test_prepare_on_all_hosts(self):
523514
@jira_ticket PYTHON-556
524515
@expected_result queries will have to re-prepared on hosts that aren't the control connection
525516
"""
526-
clus = TestCluster(prepare_on_all_hosts=False, reprepare_on_up=False)
527-
self.addCleanup(clus.shutdown)
517+
with MockLoggingHandler().set_module_name(cluster.__name__) as mock_handler:
518+
clus = TestCluster(prepare_on_all_hosts=False, reprepare_on_up=False)
519+
self.addCleanup(clus.shutdown)
528520

529-
session = clus.connect(wait_for_all_pools=True)
530-
select_statement = session.prepare("SELECT k FROM test3rf.test WHERE k = ?")
531-
for host in clus.metadata.all_hosts():
532-
session.execute(select_statement, (1, ), host=host)
533-
self.assertEqual(2, self.mock_handler.get_message_count('debug', "Re-preparing"))
521+
session = clus.connect(wait_for_all_pools=True)
522+
select_statement = session.prepare("SELECT k FROM test3rf.test WHERE k = ?")
523+
for host in clus.metadata.all_hosts():
524+
session.execute(select_statement, (1, ), host=host)
525+
self.assertEqual(2, mock_handler.get_message_count('debug', "Re-preparing"))
534526

535527
def test_prepare_batch_statement(self):
536528
"""
@@ -542,39 +534,40 @@ def test_prepare_batch_statement(self):
542534
@expected_result queries will have to re-prepared on hosts that aren't the control connection
543535
and the batch statement will be sent.
544536
"""
545-
policy = ForcedHostIndexPolicy()
546-
clus = TestCluster(
547-
execution_profiles={
548-
EXEC_PROFILE_DEFAULT: ExecutionProfile(load_balancing_policy=policy),
549-
},
550-
prepare_on_all_hosts=False,
551-
reprepare_on_up=False,
552-
)
553-
self.addCleanup(clus.shutdown)
537+
with MockLoggingHandler().set_module_name(cluster.__name__) as mock_handler:
538+
policy = ForcedHostIndexPolicy()
539+
clus = TestCluster(
540+
execution_profiles={
541+
EXEC_PROFILE_DEFAULT: ExecutionProfile(load_balancing_policy=policy),
542+
},
543+
prepare_on_all_hosts=False,
544+
reprepare_on_up=False,
545+
)
546+
self.addCleanup(clus.shutdown)
554547

555-
table = "test3rf.%s" % self._testMethodName.lower()
548+
table = "test3rf.%s" % self._testMethodName.lower()
556549

557-
session = clus.connect(wait_for_all_pools=True)
550+
session = clus.connect(wait_for_all_pools=True)
558551

559-
session.execute("DROP TABLE IF EXISTS %s" % table)
560-
session.execute("CREATE TABLE %s (k int PRIMARY KEY, v int )" % table)
552+
session.execute("DROP TABLE IF EXISTS %s" % table)
553+
session.execute("CREATE TABLE %s (k int PRIMARY KEY, v int )" % table)
561554

562-
insert_statement = session.prepare("INSERT INTO %s (k, v) VALUES (?, ?)" % table)
555+
insert_statement = session.prepare("INSERT INTO %s (k, v) VALUES (?, ?)" % table)
563556

564-
# This is going to query a host where the query
565-
# is not prepared
566-
policy.set_host(1)
567-
batch_statement = BatchStatement(consistency_level=ConsistencyLevel.ONE)
568-
batch_statement.add(insert_statement, (1, 2))
569-
session.execute(batch_statement)
557+
# This is going to query a host where the query
558+
# is not prepared
559+
policy.set_host(1)
560+
batch_statement = BatchStatement(consistency_level=ConsistencyLevel.ONE)
561+
batch_statement.add(insert_statement, (1, 2))
562+
session.execute(batch_statement)
570563

571-
# To verify our test assumption that queries are getting re-prepared properly
572-
self.assertEqual(1, self.mock_handler.get_message_count('debug', "Re-preparing"))
564+
# To verify our test assumption that queries are getting re-prepared properly
565+
self.assertEqual(1, mock_handler.get_message_count('debug', "Re-preparing"))
573566

574-
select_results = session.execute(SimpleStatement("SELECT * FROM %s WHERE k = 1" % table,
575-
consistency_level=ConsistencyLevel.ALL))
576-
first_row = select_results[0][:2]
577-
self.assertEqual((1, 2), first_row)
567+
select_results = session.execute(SimpleStatement("SELECT * FROM %s WHERE k = 1" % table,
568+
consistency_level=ConsistencyLevel.ALL))
569+
first_row = select_results[0][:2]
570+
self.assertEqual((1, 2), first_row)
578571

579572
def test_prepare_batch_statement_after_alter(self):
580573
"""
@@ -587,44 +580,45 @@ def test_prepare_batch_statement_after_alter(self):
587580
@expected_result queries will have to re-prepared on hosts that aren't the control connection
588581
and the batch statement will be sent.
589582
"""
590-
clus = TestCluster(prepare_on_all_hosts=False, reprepare_on_up=False)
591-
self.addCleanup(clus.shutdown)
583+
with MockLoggingHandler().set_module_name(cluster.__name__) as mock_handler:
584+
clus = TestCluster(prepare_on_all_hosts=False, reprepare_on_up=False)
585+
self.addCleanup(clus.shutdown)
592586

593-
table = "test3rf.%s" % self._testMethodName.lower()
587+
table = "test3rf.%s" % self._testMethodName.lower()
594588

595-
session = clus.connect(wait_for_all_pools=True)
589+
session = clus.connect(wait_for_all_pools=True)
596590

597-
session.execute("DROP TABLE IF EXISTS %s" % table)
598-
session.execute("CREATE TABLE %s (k int PRIMARY KEY, a int, b int, d int)" % table)
599-
insert_statement = session.prepare("INSERT INTO %s (k, b, d) VALUES (?, ?, ?)" % table)
591+
session.execute("DROP TABLE IF EXISTS %s" % table)
592+
session.execute("CREATE TABLE %s (k int PRIMARY KEY, a int, b int, d int)" % table)
593+
insert_statement = session.prepare("INSERT INTO %s (k, b, d) VALUES (?, ?, ?)" % table)
600594

601-
# Altering the table might trigger an update in the insert metadata
602-
session.execute("ALTER TABLE %s ADD c int" % table)
595+
# Altering the table might trigger an update in the insert metadata
596+
session.execute("ALTER TABLE %s ADD c int" % table)
603597

604-
values_to_insert = [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
598+
values_to_insert = [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
605599

606-
# We query the three hosts in order (due to the ForcedHostIndexPolicy)
607-
# the first three queries will have to be repreapred and the rest should
608-
# work as normal batch prepared statements
609-
hosts = clus.metadata.all_hosts()
610-
for i in range(10):
611-
value_to_insert = values_to_insert[i % len(values_to_insert)]
612-
batch_statement = BatchStatement(consistency_level=ConsistencyLevel.ONE)
613-
batch_statement.add(insert_statement, value_to_insert)
614-
session.execute(batch_statement, host=hosts[i % len(hosts)])
600+
# We query the three hosts in order (due to the ForcedHostIndexPolicy)
601+
# the first three queries will have to be repreapred and the rest should
602+
# work as normal batch prepared statements
603+
hosts = clus.metadata.all_hosts()
604+
for i in range(10):
605+
value_to_insert = values_to_insert[i % len(values_to_insert)]
606+
batch_statement = BatchStatement(consistency_level=ConsistencyLevel.ONE)
607+
batch_statement.add(insert_statement, value_to_insert)
608+
session.execute(batch_statement, host=hosts[i % len(hosts)])
615609

616-
select_results = session.execute("SELECT * FROM %s" % table)
617-
expected_results = [
618-
(1, None, 2, None, 3),
619-
(2, None, 3, None, 4),
620-
(3, None, 4, None, 5),
621-
(4, None, 5, None, 6)
622-
]
610+
select_results = session.execute("SELECT * FROM %s" % table)
611+
expected_results = [
612+
(1, None, 2, None, 3),
613+
(2, None, 3, None, 4),
614+
(3, None, 4, None, 5),
615+
(4, None, 5, None, 6)
616+
]
623617

624-
self.assertEqual(set(expected_results), set(select_results._current_rows))
618+
self.assertEqual(set(expected_results), set(select_results._current_rows))
625619

626-
# To verify our test assumption that queries are getting re-prepared properly
627-
self.assertEqual(3, self.mock_handler.get_message_count('debug', "Re-preparing"))
620+
# To verify our test assumption that queries are getting re-prepared properly
621+
self.assertEqual(3, mock_handler.get_message_count('debug', "Re-preparing"))
628622

629623

630624
class PrintStatementTests(unittest.TestCase):

0 commit comments

Comments
 (0)