@@ -945,13 +945,70 @@ def _assert_shuffle(self, patched_shuffle, cluster, keyspace, routing_key):
945945 else :
946946 assert set (replicas ) == set (qplan [:2 ])
947947 assert hosts [:2 ] == qplan [2 :]
948- if is_tablets :
949- child_policy .make_query_plan .assert_called_with (keyspace , query )
950- assert child_policy .make_query_plan .call_count == 2
951- else :
952- child_policy .make_query_plan .assert_called_once_with (keyspace , query )
948+ # After optimization, child.make_query_plan should be called once for both tablets and vnodes
949+ child_policy .make_query_plan .assert_called_once_with (keyspace , query )
953950 assert patched_shuffle .call_count == 1
954951
952+ def test_child_make_query_plan_called_once (self ):
953+ """
954+ Test to validate that child.make_query_plan is called only once
955+ in all scenarios (with/without tablets, with/without routing key)
956+
957+ @test_category policy
958+ """
959+ # Test with vnodes (no tablets)
960+ hosts = [Host (DefaultEndPoint (str (i )), SimpleConvictionPolicy ) for i in range (4 )]
961+ for host in hosts :
962+ host .set_up ()
963+
964+ cluster = Mock (spec = Cluster )
965+ cluster .metadata = Mock (spec = Metadata )
966+ cluster .metadata ._tablets = Mock (spec = Tablets )
967+ cluster .metadata ._tablets .table_has_tablets .return_value = False
968+ replicas = hosts [2 :]
969+ cluster .metadata .get_replicas .return_value = replicas
970+
971+ child_policy = Mock ()
972+ child_policy .make_query_plan .return_value = hosts
973+ child_policy .distance .return_value = HostDistance .LOCAL
974+
975+ policy = TokenAwarePolicy (child_policy )
976+ policy .populate (cluster , hosts )
977+
978+ # Test case 1: With routing key and keyspace (should call once)
979+ child_policy .reset_mock ()
980+ keyspace = 'keyspace'
981+ routing_key = 'routing_key'
982+ query = Statement (routing_key = routing_key , keyspace = keyspace )
983+ qplan = list (policy .make_query_plan (keyspace , query ))
984+ child_policy .make_query_plan .assert_called_once_with (keyspace , query )
985+
986+ # Test case 2: Without routing key (should call once)
987+ child_policy .reset_mock ()
988+ query = Statement (routing_key = None , keyspace = keyspace )
989+ qplan = list (policy .make_query_plan (keyspace , query ))
990+ child_policy .make_query_plan .assert_called_once_with (keyspace , query )
991+
992+ # Test case 3: Without keyspace (should call once)
993+ child_policy .reset_mock ()
994+ query = Statement (routing_key = routing_key , keyspace = None )
995+ qplan = list (policy .make_query_plan (None , query ))
996+ child_policy .make_query_plan .assert_called_once_with (None , query )
997+
998+ # Test case 4: With tablets (should call once)
999+ cluster .metadata ._tablets .table_has_tablets .return_value = True
1000+ tablet = Mock (spec = Tablet )
1001+ tablet .replicas = [(hosts [0 ].host_id , None ), (hosts [1 ].host_id , None )]
1002+ cluster .metadata ._tablets .get_tablet_for_key .return_value = tablet
1003+ cluster .metadata .token_map = Mock ()
1004+ cluster .metadata .token_map .token_class = Mock ()
1005+ cluster .metadata .token_map .token_class .from_key .return_value = 'token'
1006+
1007+ child_policy .reset_mock ()
1008+ query = Statement (routing_key = routing_key , keyspace = keyspace , table = 'test_table' )
1009+ qplan = list (policy .make_query_plan (keyspace , query ))
1010+ child_policy .make_query_plan .assert_called_once_with (keyspace , query )
1011+
9551012
9561013class ConvictionPolicyTest (unittest .TestCase ):
9571014 def test_not_implemented (self ):
0 commit comments