1+ import random
12import typing as T
23
34from cumulusci .core .enums import StrEnum
910class SelectStrategy (StrEnum ):
1011 """Enum defining the different selection strategies requested."""
1112
12- RANDOM = "random "
13+ STANDARD = "standard "
1314 SIMILARITY = "similarity"
15+ RANDOM = "random"
1416
1517
16- def random_generate_query (
17- sobject : str , fields : T .List [str ], num_records : float
18+ class SelectOperationExecutor :
19+ def __init__ (self , strategy : SelectStrategy ):
20+ self .strategy = strategy
21+
22+ def select_generate_query (
23+ self , sobject : str , fields : T .List [str ], num_records : int
24+ ):
25+ # For STANDARD strategy
26+ if self .strategy == SelectStrategy .STANDARD :
27+ return standard_generate_query (sobject = sobject , num_records = num_records )
28+ # For SIMILARITY strategy
29+ elif self .strategy == SelectStrategy .SIMILARITY :
30+ return similarity_generate_query (sobject = sobject , fields = fields )
31+ # For RANDOM strategy
32+ elif self .strategy == SelectStrategy .RANDOM :
33+ return standard_generate_query (sobject = sobject , num_records = num_records )
34+
35+ def select_post_process (
36+ self , load_records , query_records : list , num_records : int , sobject : str
37+ ):
38+ # For STANDARD strategy
39+ if self .strategy == SelectStrategy .STANDARD :
40+ return standard_post_process (
41+ query_records = query_records , num_records = num_records , sobject = sobject
42+ )
43+ # For SIMILARITY strategy
44+ elif self .strategy == SelectStrategy .SIMILARITY :
45+ return similarity_post_process (
46+ load_records = load_records , query_records = query_records , sobject = sobject
47+ )
48+ # For RANDOM strategy
49+ elif self .strategy == SelectStrategy .RANDOM :
50+ return random_post_process (
51+ query_records = query_records , num_records = num_records , sobject = sobject
52+ )
53+
54+
55+ def standard_generate_query (
56+ sobject : str , num_records : int
1857) -> T .Tuple [str , T .List [str ]]:
19- """Generates the SOQL query for the random selection strategy"""
58+ """Generates the SOQL query for the standard (as well as random) selection strategy"""
2059 # Get the WHERE clause from DEFAULT_DECLARATIONS if available
2160 declaration = DEFAULT_DECLARATIONS .get (sobject )
2261 if declaration :
@@ -32,10 +71,10 @@ def random_generate_query(
3271 return query , ["Id" ]
3372
3473
35- def random_post_process (
36- load_records , query_records : list , num_records : float , sobject : str
74+ def standard_post_process (
75+ query_records : list , num_records : int , sobject : str
3776) -> T .Tuple [T .List [dict ], T .Union [str , None ]]:
38- """Processes the query results for the random selection strategy"""
77+ """Processes the query results for the standard selection strategy"""
3978 # Handle case where query returns 0 records
4079 if not query_records :
4180 error_message = f"No records found for { sobject } in the target org."
@@ -59,9 +98,8 @@ def random_post_process(
5998def similarity_generate_query (
6099 sobject : str ,
61100 fields : T .List [str ],
62- num_records : float ,
63101) -> T .Tuple [str , T .List [str ]]:
64- """Generates the SOQL query for the random selection strategy"""
102+ """Generates the SOQL query for the similarity selection strategy"""
65103 # Get the WHERE clause from DEFAULT_DECLARATIONS if available
66104 declaration = DEFAULT_DECLARATIONS .get (sobject )
67105 if declaration :
@@ -81,7 +119,7 @@ def similarity_generate_query(
81119
82120
83121def similarity_post_process (
84- load_records , query_records : list , num_records : float , sobject : str
122+ load_records : list , query_records : list , sobject : str
85123) -> T .Tuple [T .List [dict ], T .Union [str , None ]]:
86124 """Processes the query results for the similarity selection strategy"""
87125 # Handle case where query returns 0 records
@@ -100,6 +138,26 @@ def similarity_post_process(
100138 return closest_records , None
101139
102140
141+ def random_post_process (
142+ query_records : list , num_records : int , sobject : str
143+ ) -> T .Tuple [T .List [dict ], T .Union [str , None ]]:
144+ """Processes the query results for the random selection strategy"""
145+
146+ if not query_records :
147+ error_message = f"No records found for { sobject } in the target org."
148+ return [], error_message
149+
150+ selected_records = []
151+ for _ in range (num_records ): # Loop 'num_records' times
152+ # Randomly select one record from query_records
153+ random_record = random .choice (query_records )
154+ selected_records .append (
155+ {"id" : random_record [0 ], "success" : True , "created" : False }
156+ )
157+
158+ return selected_records , None
159+
160+
103161def find_closest_record (load_record : list , query_records : list ):
104162 closest_distance = float ("inf" )
105163 closest_record = query_records [0 ]
0 commit comments