From 27fdb4aa238a545214f86cde8d17414acd75c1fc Mon Sep 17 00:00:00 2001
From: Ryan Laurie <30528226+iethree@users.noreply.github.com>
Date: Thu, 16 Feb 2023 15:59:55 -0700
Subject: [PATCH] Custom Table E2E Tests 4: Allow reusable multi-dialect schema
 reset (#27987)

---
 frontend/test/__support__/e2e/db_tasks.js     |  8 ++++
 .../e2e/helpers/e2e-qa-databases-helpers.js   |  4 ++
 frontend/test/__support__/e2e/test_tables.js  | 24 +++++++++++
 ...45-cc-numeric-missing-summarize.cy.spec.js | 41 ++++---------------
 4 files changed, 43 insertions(+), 34 deletions(-)
 create mode 100644 frontend/test/__support__/e2e/test_tables.js

diff --git a/frontend/test/__support__/e2e/db_tasks.js b/frontend/test/__support__/e2e/db_tasks.js
index e332c0083d0..804ba1d323d 100644
--- a/frontend/test/__support__/e2e/db_tasks.js
+++ b/frontend/test/__support__/e2e/db_tasks.js
@@ -1,4 +1,6 @@
 import Knex from "knex";
+import { WRITABLE_DB_CONFIG } from "./cypress_data";
+import * as testTables from "./test_tables";
 
 const dbClients = {};
 
@@ -28,3 +30,9 @@ export async function connectAndQueryDB({ connectionConfig, query }) {
     return result;
   }
 }
+export async function resetTable({ type = "postgres", table = "testTable1" }) {
+  const dbClient = getDbClient(WRITABLE_DB_CONFIG[type]);
+
+  // eslint-disable-next-line import/namespace
+  return testTables?.[table]?.(dbClient);
+}
diff --git a/frontend/test/__support__/e2e/helpers/e2e-qa-databases-helpers.js b/frontend/test/__support__/e2e/helpers/e2e-qa-databases-helpers.js
index 7ce596d5497..56a975ebe29 100644
--- a/frontend/test/__support__/e2e/helpers/e2e-qa-databases-helpers.js
+++ b/frontend/test/__support__/e2e/helpers/e2e-qa-databases-helpers.js
@@ -172,6 +172,10 @@ export function queryWritableDB(query, type = "postgres") {
   });
 }
 
+export function resetTestTable({ type, table }) {
+  cy.task("resetTable", { type, table });
+}
+
 // will this work for multiple schemas?
 export function getTableId({ databaseId = WRITABLE_DB_ID, name }) {
   return cy
diff --git a/frontend/test/__support__/e2e/test_tables.js b/frontend/test/__support__/e2e/test_tables.js
new file mode 100644
index 00000000000..b9efc2d9c53
--- /dev/null
+++ b/frontend/test/__support__/e2e/test_tables.js
@@ -0,0 +1,24 @@
+// define test schema such that they can be used in multiple SQL dialects using
+// knex's schema builder https://knexjs.org/guide/schema-builder.html
+
+// we cannot use knex to define multi-dialect schemas because we can only pass
+// json-serializable data to cypress tasks (which run in node)
+// https://docs.cypress.io/api/commands/task#Arguments
+
+export const colors27745 = async dbClient => {
+  const tableName = "colors27745";
+
+  await dbClient.schema.dropTableIfExists(tableName);
+  await dbClient.schema.createTable(tableName, table => {
+    table.increments("id").primary();
+    table.string("name").unique().notNullable();
+  });
+
+  await dbClient(tableName).insert([
+    { name: "red" },
+    { name: "green" },
+    { name: "blue" },
+  ]);
+
+  return true;
+};
diff --git a/frontend/test/metabase/scenarios/custom-column/reproductions/27745-cc-numeric-missing-summarize.cy.spec.js b/frontend/test/metabase/scenarios/custom-column/reproductions/27745-cc-numeric-missing-summarize.cy.spec.js
index ac1678997ff..523173ba0dd 100644
--- a/frontend/test/metabase/scenarios/custom-column/reproductions/27745-cc-numeric-missing-summarize.cy.spec.js
+++ b/frontend/test/metabase/scenarios/custom-column/reproductions/27745-cc-numeric-missing-summarize.cy.spec.js
@@ -4,52 +4,25 @@ import {
   enterCustomColumnDetails,
   visualize,
   popover,
-  queryQADB,
+  resetTestTable,
 } from "__support__/e2e/helpers";
 
-const createTableQueries = {
-  postgres: `
-    DROP TABLE IF EXISTS colors;
-
-    CREATE TABLE colors (
-      id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
-      color VARCHAR ( 255 ) UNIQUE NOT NULL
-    );
-
-    INSERT INTO colors (color) VALUES ('red'), ('green'), ('blue');
-  `,
-  mysql: `
-    DROP TABLE IF EXISTS colors;
-
-    CREATE TABLE colors (
-      id INTEGER PRIMARY KEY AUTO_INCREMENT,
-      color VARCHAR ( 255 ) UNIQUE NOT NULL
-    );
-
-    INSERT INTO colors (color) VALUES ('red'), ('green'), ('blue');
-  `,
-};
-
-const snapshotMap = {
-  postgres: "postgres-12",
-  mysql: "mysql-8",
-};
-
 ["postgres", "mysql"].forEach(dialect => {
   describe.skip(`issue 27745 (${dialect})`, { tags: "@external" }, () => {
+    const tableName = "colors27745";
+
     beforeEach(() => {
-      restore(snapshotMap[dialect]);
+      restore(`${dialect}-writable`);
       cy.signInAsAdmin();
 
-      queryQADB(createTableQueries[dialect], dialect);
-
+      resetTestTable({ type: dialect, table: tableName });
       cy.request("POST", "/api/database/2/sync_schema");
     });
 
     it("should display all summarize options if the only numeric field is a custom column (metabase#27745)", () => {
       startNewQuestion();
-      cy.findByText(/QA/i).click();
-      cy.findByText("Colors").click();
+      cy.findByText(/Writable/i).click();
+      cy.findByText(/colors/i).click();
       cy.icon("add_data").click();
       enterCustomColumnDetails({
         formula: "case([ID] > 1, 25, 5)",
-- 
GitLab