Skip to content

Commit bad261e

Browse files
committed
fix(test definitions): bugs in validate test query
1 parent f171aa3 commit bad261e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

testgen/ui/views/test_definitions.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import testgen.ui.services.form_service as fm
1414
from testgen.common import date_service
15+
from testgen.common.database.database_service import get_flavor_service, replace_params
1516
from testgen.common.models import with_database_session
1617
from testgen.common.models.connection import Connection
1718
from testgen.common.models.table_group import TableGroup, TableGroupMinimal
@@ -1179,17 +1180,33 @@ def get_column_names(table_groups_id: str, table_name: str) -> list[str]:
11791180
def validate_test(test_definition, table_group: TableGroupMinimal):
11801181
schema = test_definition["schema_name"]
11811182
table_name = test_definition["table_name"]
1183+
connection = Connection.get_by_table_group(table_group.id)
11821184

11831185
if test_definition["test_type"] == "Condition_Flag":
11841186
condition = test_definition["custom_query"]
1187+
concat_operator = get_flavor_service(connection.sql_flavor).get_concat_operator()
11851188
query = f"""
11861189
SELECT
1187-
COALESCE(CAST(SUM(CASE WHEN {condition} THEN 1 ELSE 0 END) AS VARCHAR(1000) ) || '|' ,'<NULL>|')
1190+
COALESCE(
1191+
CAST(
1192+
SUM(
1193+
CASE WHEN {condition} THEN 1 ELSE 0 END
1194+
) AS VARCHAR(1000)
1195+
)
1196+
{concat_operator} '|',
1197+
'<NULL>|'
1198+
)
11881199
FROM {schema}.{table_name};
11891200
"""
11901201
else:
1191-
query = test_definition["custom_query"]
1192-
query = query.replace("{DATA_SCHEMA}", schema)
1202+
query = replace_params(
1203+
f"""
1204+
SELECT COUNT(*)
1205+
FROM (
1206+
{test_definition["custom_query"]}
1207+
) TEST
1208+
""",
1209+
{"DATA_SCHEMA": schema},
1210+
)
11931211

1194-
connection = Connection.get_by_table_group(table_group.id)
11951212
fetch_from_target_db(connection, query)

0 commit comments

Comments
 (0)