Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-1570 Transform has changes after bluring source table input and still has changes after cancelling them (#77712)

Kamil Mielnik479d7ff67b27
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.tsx
index 3304eaa93a3..9aca5abf0dd 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.tsx
@@ -37,7 +37,9 @@ export function AliasInput({
}
function handleBlur() {
- onChange(value);
+ if (value !== selection.alias) {
+ onChange(value);
+ }
}
return (
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.unit.spec.tsx
new file mode 100644
index 00000000000..aeb2dbaf27c
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/AliasInput/AliasInput.unit.spec.tsx
@@ -0,0 +1,62 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen } from "__support__/ui";
+import { createMockTable } from "metabase-types/api/mocks";
+
+import type { TableSelection } from "../types";
+
+import { AliasInput } from "./AliasInput";
+
+const TABLE = createMockTable();
+
+const SELECTION: TableSelection = {
+ tableId: 1,
+ alias: "test_table",
+};
+
+function setup() {
+ const onChange = jest.fn();
+
+ renderWithProviders(
+ <AliasInput
+ selection={SELECTION}
+ table={TABLE}
+ usedAliases={new Set([SELECTION.alias])}
+ onChange={onChange}
+ />,
+ );
+
+ return { onChange };
+}
+
+describe("AliasInput", () => {
+ it("should not call onChange when blurred without changes (GDGT-1570)", async () => {
+ const { onChange } = setup();
+
+ await userEvent.click(screen.getByDisplayValue("test_table"));
+ await userEvent.tab();
+
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it("should call onChange with the new value when blurred after a change", async () => {
+ const { onChange } = setup();
+
+ await userEvent.type(screen.getByDisplayValue("test_table"), "x");
+ await userEvent.tab();
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith("test_tablex");
+ });
+
+ it("should not call onChange when a change is reverted before blurring (GDGT-1570)", async () => {
+ const { onChange } = setup();
+
+ const input = screen.getByDisplayValue("test_table");
+ await userEvent.type(input, "x");
+ await userEvent.type(input, "{backspace}");
+ await userEvent.tab();
+
+ expect(onChange).not.toHaveBeenCalled();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.ts b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.ts
index 4442187e65f..5a7c2240898 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.ts
@@ -56,7 +56,7 @@ export function updateTransformSignature(
// Replace existing Args section
updatedScript = updatedScript.replace(
argsRegex,
- ` $1
+ `$1
Args:
${newArgsSection}
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.unit.spec.ts
new file mode 100644
index 00000000000..1abaa089689
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/utils.unit.spec.ts
@@ -0,0 +1,55 @@
+import type { PythonTransformTableAliases } from "metabase-types/api";
+import { createMockTable } from "metabase-types/api/mocks";
+
+import { updateTransformSignature } from "./utils";
+
+const TEMPLATE_BODY = `# Write your Python transformation script here
+import common
+import pandas as pd
+
+def transform():
+ """
+ Your transformation function.
+
+ Select tables above to add them as function parameters.
+
+ Returns:
+ DataFrame to write to the destination table
+ """
+ # Your transformation logic here
+ return pd.DataFrame([{"message": "Hello from Python transform!"}])`;
+
+const TABLE = createMockTable();
+
+const TABLES: PythonTransformTableAliases = [
+ { alias: "orders", table_id: 1, schema: "PUBLIC", database_id: 1 },
+];
+
+describe("updateTransformSignature", () => {
+ it("should add the table alias to the signature and the Args docstring section", () => {
+ const script = updateTransformSignature(TEMPLATE_BODY, TABLES, [TABLE]);
+
+ expect(script).toContain("def transform(orders):");
+ expect(script).toContain("Args:");
+ expect(script).toContain("orders: DataFrame containing the data");
+ });
+
+ it("should be idempotent, so re-applying the same tables does not change the script (GDGT-1570)", () => {
+ const script = updateTransformSignature(TEMPLATE_BODY, TABLES, [TABLE]);
+ const scriptAfter2ndPass = updateTransformSignature(script, TABLES, [
+ TABLE,
+ ]);
+
+ expect(scriptAfter2ndPass).toBe(script);
+ });
+
+ it("should not leave trailing whitespace on the signature line when replacing an existing Args section (GDGT-1570)", () => {
+ const script = updateTransformSignature(TEMPLATE_BODY, TABLES, [TABLE]);
+ const scriptAfter2ndPass = updateTransformSignature(script, TABLES, [
+ TABLE,
+ ]);
+
+ expect(scriptAfter2ndPass).toContain("def transform(orders):\n");
+ expect(scriptAfter2ndPass).not.toMatch(/[ ]+\n/);
+ });
+});