1- import typing
1+ from typing import ClassVar , TypedDict
22
33from testgen .commands .queries .rollup_scores_query import CRollupScoresSQL
44from testgen .common import date_service , read_template_sql_file
5- from testgen .common .database import database_service
5+ from testgen .common .database . database_service import get_flavor_service , replace_params
66from testgen .common .read_file import replace_templated_functions
77
88
9+ class CATTestParams (TypedDict ):
10+ schema_name : str
11+ table_name : str
12+ cat_sequence : int
13+ test_measures : str
14+ test_conditions : str
15+
16+
917class CCATExecutionSQL :
1018 project_code = ""
1119 flavor = ""
@@ -16,11 +24,9 @@ class CCATExecutionSQL:
1624 table_groups_id = ""
1725 max_query_chars = ""
1826 exception_message = ""
19-
20- # Test Set Parameters
2127 target_schema = ""
2228 target_table = ""
23- dctTestParms : typing . ClassVar = {}
29+ cat_test_params : ClassVar [ CATTestParams ] = {}
2430
2531 _rollup_scores_sql : CRollupScoresSQL = None
2632
@@ -29,7 +35,7 @@ def __init__(self, strProjectCode, strTestSuiteId, strTestSuite, strSQLFlavor, m
2935 self .test_suite_id = strTestSuiteId
3036 self .test_suite = strTestSuite
3137 self .project_code = strProjectCode
32- flavor_service = database_service . get_flavor_service (strSQLFlavor )
38+ flavor_service = get_flavor_service (strSQLFlavor )
3339 self .concat_operator = flavor_service .get_concat_operator ()
3440 self .flavor = strSQLFlavor
3541 self .max_query_chars = max_query_chars
@@ -41,83 +47,78 @@ def _get_rollup_scores_sql(self) -> CRollupScoresSQL:
4147 self ._rollup_scores_sql = CRollupScoresSQL (self .test_run_id , self .table_groups_id )
4248
4349 return self ._rollup_scores_sql
44-
45- def _ReplaceParms (self , strInputString ):
46- strInputString = strInputString .replace ("{MAX_QUERY_CHARS}" , str (self .max_query_chars ))
47- strInputString = strInputString .replace ("{TEST_RUN_ID}" , self .test_run_id )
48- strInputString = strInputString .replace ("{PROJECT_CODE}" , self .project_code )
49- strInputString = strInputString .replace ("{TEST_SUITE}" , self .test_suite )
50- strInputString = strInputString .replace ("{TEST_SUITE_ID}" , self .test_suite_id )
51- strInputString = strInputString .replace ("{TABLE_GROUPS_ID}" , self .table_groups_id )
52-
53- strInputString = strInputString .replace ("{SQL_FLAVOR}" , self .flavor )
54- strInputString = strInputString .replace ("{ID_SEPARATOR}" , "`" if self .flavor == "databricks" else '"' )
55- strInputString = strInputString .replace ("{CONCAT_OPERATOR}" , self .concat_operator )
56-
57- strInputString = strInputString .replace ("{SCHEMA_NAME}" , self .target_schema )
58- strInputString = strInputString .replace ("{TABLE_NAME}" , self .target_table )
59-
60- strInputString = strInputString .replace ("{RUN_DATE}" , self .run_date )
61- strInputString = strInputString .replace ("{NOW_DATE}" , "GETDATE()" )
62- strInputString = strInputString .replace ("{START_TIME}" , self .today )
63- strInputString = strInputString .replace (
64- "{NOW}" , date_service .get_now_as_string_with_offset (self .minutes_offset )
65- )
66- strInputString = strInputString .replace ("{EXCEPTION_MESSAGE}" , self .exception_message .strip ())
67-
68- for parm , value in self .dctTestParms .items ():
69- strInputString = strInputString .replace ("{" + parm .upper () + "}" , str (value ))
70-
71- strInputString = strInputString .replace ("{RUN_DATE}" , self .run_date )
72-
73- strInputString = replace_templated_functions (strInputString , self .flavor )
74-
75- if self .flavor != "databricks" :
50+
51+ def _get_query (self , template_file_name : str , sub_directory : str | None = "exec_cat_tests" , no_bind : bool = False ) -> tuple [str , dict | None ]:
52+ query = read_template_sql_file (template_file_name , sub_directory )
53+ params = {
54+ "MAX_QUERY_CHARS" : self .max_query_chars ,
55+ "TEST_RUN_ID" : self .test_run_id ,
56+ "PROJECT_CODE" : self .project_code ,
57+ "TEST_SUITE" : self .test_suite ,
58+ "TEST_SUITE_ID" : self .test_suite_id ,
59+ "TABLE_GROUPS_ID" : self .table_groups_id ,
60+ "SQL_FLAVOR" : self .flavor ,
61+ "ID_SEPARATOR" : "`" if self .flavor == "databricks" else '"' ,
62+ "CONCAT_OPERATOR" : self .concat_operator ,
63+ "SCHEMA_NAME" : self .target_schema ,
64+ "TABLE_NAME" : self .target_table ,
65+ "NOW_DATE" : "GETDATE()" ,
66+ "START_TIME" : self .today ,
67+ "NOW_TIMESTAMP" : date_service .get_now_as_string_with_offset (self .minutes_offset ),
68+ "EXCEPTION_MESSAGE" : self .exception_message .strip (),
69+ ** {key .upper (): value for key , value in self .cat_test_params .items ()},
70+ # This has to be replaced at the end
71+ "RUN_DATE" : self .run_date ,
72+ }
73+ query = replace_params (query , params )
74+ query = replace_templated_functions (query , self .flavor )
75+
76+ if no_bind and self .flavor != "databricks" :
7677 # Adding escape character where ':' is referenced
77- strInputString = strInputString .replace (":" , "\\ :" )
78+ query = query .replace (":" , "\\ :" )
7879
79- return strInputString
80+ return query , None if no_bind else params
8081
81- def GetDistinctTablesSQL (self ):
82- # Runs on DK DB
83- strQ = self ._ReplaceParms (read_template_sql_file ("ex_cat_get_distinct_tables.sql" , "exec_cat_tests" ))
84- return strQ
82+ def GetDistinctTablesSQL (self ) -> tuple [str , dict ]:
83+ # Runs on App database
84+ return self ._get_query ("ex_cat_get_distinct_tables.sql" )
8585
86- def GetAggregateTableTestSQL (self ):
87- # Runs on DK DB
88- strQ = self ._ReplaceParms (read_template_sql_file ("ex_cat_build_agg_table_tests.sql" , "exec_cat_tests" ))
89- return strQ
86+ def GetAggregateTableTestSQL (self ) -> tuple [str , None ]:
87+ # Runs on App database
88+ return self ._get_query ("ex_cat_build_agg_table_tests.sql" , no_bind = True )
9089
91- def GetAggregateTestParmsSQL (self ):
92- # Runs on DK DB
93- strQ = self ._ReplaceParms (read_template_sql_file ("ex_cat_retrieve_agg_test_parms.sql" , "exec_cat_tests" ))
94- return strQ
90+ def GetAggregateTestParmsSQL (self ) -> tuple [str , dict ]:
91+ # Runs on App database
92+ return self ._get_query ("ex_cat_retrieve_agg_test_parms.sql" )
9593
96- def PrepCATQuerySQL (self ):
97- strQ = self . _ReplaceParms ( read_template_sql_file ( "ex_cat_test_query.sql" , "exec_cat_tests" ))
98- return strQ
94+ def PrepCATQuerySQL (self ) -> tuple [ str , None ] :
95+ # Runs on Target database
96+ return self . _get_query ( "ex_cat_test_query.sql" , no_bind = True )
9997
100- def GetCATResultsParseSQL (self ):
101- strQ = self . _ReplaceParms ( read_template_sql_file ( "ex_cat_results_parse.sql" , "exec_cat_tests" ))
102- return strQ
98+ def GetCATResultsParseSQL (self ) -> tuple [ str , dict ] :
99+ # Runs on App database
100+ return self . _get_query ( "ex_cat_results_parse.sql" )
103101
104- def FinalizeTestResultsSQL (self ):
105- strQ = self . _ReplaceParms ( read_template_sql_file ( "ex_finalize_test_run_results.sql" , "execution" ))
106- return strQ
102+ def FinalizeTestResultsSQL (self ) -> tuple [ str , dict ] :
103+ # Runs on App database
104+ return self . _get_query ( "ex_finalize_test_run_results.sql" , "execution" )
107105
108- def PushTestRunStatusUpdateSQL (self ):
109- strQ = self . _ReplaceParms ( read_template_sql_file ( "ex_update_test_record_in_testrun_table.sql" , "execution" ))
110- return strQ
106+ def PushTestRunStatusUpdateSQL (self ) -> tuple [ str , dict ] :
107+ # Runs on App database
108+ return self . _get_query ( "ex_update_test_record_in_testrun_table.sql" , "execution" )
111109
112- def FinalizeTestSuiteUpdateSQL (self ):
113- strQ = self . _ReplaceParms ( read_template_sql_file ( "ex_update_test_suite.sql" , "execution" ))
114- return strQ
110+ def FinalizeTestSuiteUpdateSQL (self ) -> tuple [ str , dict ] :
111+ # Runs on App database
112+ return self . _get_query ( "ex_update_test_suite.sql" , "execution" )
115113
116- def CalcPrevalenceTestResultsSQL (self ):
117- return self ._ReplaceParms (read_template_sql_file ("ex_calc_prevalence_test_results.sql" , "execution" ))
114+ def CalcPrevalenceTestResultsSQL (self ) -> tuple [str , None ]:
115+ # Runs on App database
116+ return self ._get_query ("ex_calc_prevalence_test_results.sql" , "execution" , no_bind = True )
118117
119- def TestScoringRollupRunSQL (self ):
118+ def TestScoringRollupRunSQL (self ) -> tuple [str , dict ]:
119+ # Runs on App database
120120 return self ._get_rollup_scores_sql ().GetRollupScoresTestRunQuery ()
121121
122- def TestScoringRollupTableGroupSQL (self ):
122+ def TestScoringRollupTableGroupSQL (self ) -> tuple [str , dict ]:
123+ # Runs on App database
123124 return self ._get_rollup_scores_sql ().GetRollupScoresTestTableGroupQuery ()
0 commit comments