Skip to content

Commit 1dfbfd9

Browse files
committed
fix(copy/move tests): unique key constraints
1 parent b94c506 commit 1dfbfd9

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

testgen/ui/views/test_definitions.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,12 +997,15 @@ def copy_move_test_dialog(
997997

998998
movable_test_definitions = []
999999
if target_table_group_id and target_test_suite_id:
1000-
collision_test_definitions = get_test_definitions_collision(selected_test_definitions, target_table_group_id, target_test_suite_id)
1000+
collision_test_definitions = get_test_definitions_collision(selected_test_definitions, target_table_group_id, target_test_suite_id, target_table_name, target_column_name)
1001+
overwrite_ids = []
10011002
if not collision_test_definitions.empty:
10021003
unlocked = collision_test_definitions[collision_test_definitions["lock_refresh"] == False]
10031004
locked = collision_test_definitions[collision_test_definitions["lock_refresh"] == True]
10041005
locked_tuples = [ (test["table_name"], test["column_name"], test["test_type"]) for test in locked.iterrows() ]
10051006
movable_test_definitions = [ test for test in selected_test_definitions if (test["table_name"], test["column_name"], test["test_type"]) not in locked_tuples ]
1007+
selected_ids = {str(item["id"]) for item in selected_test_definitions}
1008+
overwrite_ids = [id_ for id_ in unlocked["id"].tolist() if str(id_) not in selected_ids]
10061009

10071010
warning_message = f"""Auto-generated tests are present in the target test suite for the same column-test type combinations as the selected tests.
10081011
\nUnlocked tests that will be overwritten: {len(unlocked)}
@@ -1028,12 +1031,16 @@ def copy_move_test_dialog(
10281031

10291032
test_definition_ids = [item["id"] for item in movable_test_definitions]
10301033
if move:
1034+
if overwrite_ids:
1035+
TestDefinition.delete_where(TestDefinition.id.in_(overwrite_ids))
10311036
TestDefinition.move(test_definition_ids, target_table_group_id, target_test_suite_id, target_table_name, target_column_name)
10321037
success_message = "Test Definitions have been moved."
10331038
st.success(success_message)
10341039
time.sleep(1)
10351040
safe_rerun()
10361041
elif copy:
1042+
if overwrite_ids:
1043+
TestDefinition.delete_where(TestDefinition.id.in_(overwrite_ids))
10371044
TestDefinition.copy(test_definition_ids, target_table_group_id, target_test_suite_id, target_table_name, target_column_name)
10381045
success_message = "Test Definitions have been copied."
10391046
st.success(success_message)
@@ -1347,9 +1354,11 @@ def get_test_definitions_collision(
13471354
test_definitions: list[dict],
13481355
target_table_group_id: str,
13491356
target_test_suite_id: str,
1357+
target_table_name: str | None = None,
1358+
target_column_name: str | None = None,
13501359
) -> pd.DataFrame:
1351-
table_tests = [(item["table_name"], item["test_type"]) for item in test_definitions if item["column_name"] is None and item["table_name"] is not None]
1352-
column_tests = [(item["table_name"], item["column_name"], item["test_type"]) for item in test_definitions if item["column_name"] is not None]
1360+
table_tests = [(target_table_name or item["table_name"], item["test_type"]) for item in test_definitions if item["column_name"] is None and item["table_name"] is not None]
1361+
column_tests = [(target_table_name or item["table_name"], target_column_name or item["column_name"], item["test_type"]) for item in test_definitions if item["column_name"] is not None]
13531362
results = TestDefinition.select_minimal_where(
13541363
TestDefinition.table_groups_id == target_table_group_id,
13551364
TestDefinition.test_suite_id == target_test_suite_id,

0 commit comments

Comments
 (0)