Skip to content

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

9.2 KB
Raw Blame
import { WRITABLE_DB_ID } from "e2e/support/cypress_data";

const { H } = cy;
const { TablePicker } = H.DataModel;

interface MetadataResponse {
  data_layer: string;
  view_count: number;
}

describe("Table editing", () => {
  beforeEach(() => {
    H.resetSnowplow();
    cy.signInAsAdmin();
    cy.intercept("GET", "/api/table/*/query_metadata*").as("metadata");
    cy.intercept("PUT", "/api/table/*").as("updateTable");
    cy.intercept("POST", "/api/ee/data-studio/table/publish-tables").as(
      "publishTables",
    );
    cy.intercept("POST", "/api/ee/data-studio/table/unpublish-tables").as(
      "unpublishTables",
    );
  });

  it(
    "should display metadata information and publish and unpublish a single table",
    { tags: ["@external"] },
    () => {
      H.restore("mysql-8");
      H.activateToken("pro-self-hosted");
      H.DataModel.visitDataStudio();
      TablePicker.getDatabase("QA MySQL8").click();
      TablePicker.getTable("Orders").click();

      cy.log("display metadata information");
      cy.wait<MetadataResponse>("@metadata").then(({ response }) => {
        const viewCount = response?.body.view_count ?? 0;

        cy.findByLabelText("Name in the database").should(
          "have.text",
          "ORDERS",
        );
        cy.findByLabelText("Last updated at").should("exist"); // Testing the actual value is done in TableMetadata.unit.spec.tsx
        cy.findByLabelText("View count").should("have.text", viewCount);
        cy.findByLabelText("Est. row count").should("not.exist");
        cy.findByLabelText("Dependencies").should("have.text", "0");
        cy.findByLabelText("Dependents").should("have.text", "0");

        H.DataModel.TableSection.get()
          .findByRole("link", { name: "Dependency graph" })
          .click();

        H.DataStudio.Dependencies.graph().should("be.visible");
      });
      cy.go("back");
      H.DataModel.TableSection.getNameInput().should("have.value", "Orders");

      cy.log("publish the table and verify it's published");
      TablePicker.getTable("Orders")
        .findByTestId("table-published")
        .should("not.exist");
      cy.findByRole("button", { name: /Publish/ }).click();
      H.modal().findByText("Create my semantic layer").click();
      H.modal().findByText("Publish this table").click();
      cy.wait("@publishTables");
      TablePicker.getTable("Orders")
        .findByTestId("table-published")
        .should("be.visible");
      H.undoToastListContainer().within(() => {
        cy.findByText("Published").should("be.visible");
        cy.findByRole("button", { name: /Go to Data/ }).click();
      });
      H.expectUnstructuredSnowplowEvent({
        event: "data_studio_table_published",
      });
      H.DataStudio.Library.tableItem("Orders").should("be.visible");
      cy.go("back");

      cy.log("unpublish the table and verify it's unpublished");
      cy.findByRole("button", { name: /Unpublish/ }).click();
      H.modal().findByText("Unpublish this table").click();
      cy.wait("@unpublishTables");
      TablePicker.getTable("Orders")
        .findByTestId("table-published")
        .should("not.exist");
      H.DataStudio.nav().findByLabelText("Semantic layer").click();
      H.DataStudio.Library.emptyStateRow(
        "Cleaned, pre-transformed data sources ready for exploring",
      ).should("be.visible");
      H.DataStudio.Library.allTableItems().should("have.length", 0);
    },
  );

  it("should allow to edit attributes", { tags: ["@external"] }, () => {
    H.restore("postgres-12");
    H.activateToken("pro-self-hosted");
    H.DataModel.visitDataStudio();
    TablePicker.getDatabase("QA Postgres12").click();
    TablePicker.getTable("Orders").click();

    cy.log("rename the table and verify the name is updated in the picker");
    H.DataModel.TableSection.getNameInput()
      .clear()
      .type("Renamed Orders")
      .blur();
    cy.wait("@updateTable");
    TablePicker.getTable("Renamed Orders").should("be.visible");
    H.undoToast().findByRole("img", { name: /Close/i }).click();

    H.selectHasValue("Owner", "No owner").click();
    H.selectDropdown().contains("Bobby Tables").click();

    H.undoToastListContainer()
      .findByText("Table owner updated")
      .should("be.visible");
    H.undoToast().findByRole("img", { name: /Close/i }).click();

    H.selectHasValue("Visibility layer", "Internal").click();
    H.selectDropdown().contains("Final").click();
    H.undoToastListContainer()
      .findByText("Table visibility layer updated")
      .should("be.visible");

    cy.log(
      "undo the change and verify the table updates to the reverted value",
    );
    H.undoToastListContainer()
      .findByText("Table visibility layer updated")
      .closest("[data-testid='toast-undo']")
      .button("Undo")
      .click();
    cy.wait("@updateTable");
    H.undoToastListContainer().findByText("Change undone").should("be.visible");
    H.undoToast().findByRole("img", { name: /Close/i }).click();
    H.selectHasValue("Visibility layer", "Internal");
    TablePicker.getTable("Orders")
      .findByTestId("table-data-layer")
      .should("have.text", "Internal");

    H.selectHasValue("Entity type", "Transaction").click();
    H.selectDropdown().contains("Person").click();
    H.undoToastListContainer()
      .findByText("Entity type updated")
      .should("be.visible");

    H.selectHasValue("Source", "Unspecified").click();
    H.selectDropdown().contains("Ingested").click();
    H.undoToastListContainer()
      .findByText("Table data source updated")
      .should("be.visible");

    // navigate away and back
    TablePicker.getTable("Products").click();
    TablePicker.getTable("Renamed Orders").click();

    H.selectHasValue("Owner", "Bobby Tables");
    H.selectHasValue("Visibility layer", "Internal");
    H.selectHasValue("Entity type", "Person");
    H.selectHasValue("Source", "Ingested");
  });

  it(
    "transform-created table should have link and disabled source edit",
    { tags: ["@external"] },
    () => {
      H.restore("postgres-writable");
      H.activateToken("pro-self-hosted");
      H.updateSetting("transforms-enabled", true);
      H.resetTestTable({ type: "postgres", table: "many_schemas" });

      const SOURCE_TABLE = "Animals";
      const TARGET_TABLE = "transform_table";
      const TARGET_SCHEMA = "Schema A";
      const TRANSFORM_TABLE_DISPLAY_NAME = "Transform Table";
      const TRANSFORM_NAME = "Test transform for animals";

      H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });

      // Create and run a transform to create a table
      H.createAndRunMbqlTransform({
        sourceTable: SOURCE_TABLE,
        targetTable: TARGET_TABLE,
        targetSchema: TARGET_SCHEMA,
        name: TRANSFORM_NAME,
      }).then(() => {
        H.DataModel.visitDataStudio();
        TablePicker.getDatabase("Writable Postgres12").click();
        TablePicker.getSchema(TARGET_SCHEMA).click();
        TablePicker.getTable(TRANSFORM_TABLE_DISPLAY_NAME).click();

        cy.findByRole("link", { name: new RegExp(TRANSFORM_NAME) }).should(
          "be.visible",
        );

        H.selectIsDisabled("Source");
      });
    },
  );

  describe("with remote sync enabled", () => {
    it(
      "should update the tree after publishing, unpublishing, and renaming a table (metabase#69554)",
      { tags: ["@external"] },
      () => {
        H.restore("mysql-8");
        H.activateToken("pro-self-hosted");
        H.setupGitSync();
        H.configureGit("read-write");

        H.DataModel.visitDataStudio();
        TablePicker.getDatabase("QA MySQL8").click();
        TablePicker.getTable("Orders").click();

        cy.log("publish the table and verify the tree updates");
        cy.findByRole("button", { name: /Publish/ }).click();
        H.modal().findByText("Create my semantic layer").click();
        H.modal().findByText("Publish this table").click();
        cy.wait("@publishTables");
        TablePicker.getTable("Orders")
          .findByTestId("table-published")
          .should("be.visible");

        cy.log("unpublish the table and verify the tree updates");
        cy.findByRole("button", { name: /Unpublish/ }).click();
        H.modal().findByText("Unpublish this table").click();
        cy.wait("@unpublishTables");
        TablePicker.getTable("Orders")
          .findByTestId("table-published")
          .should("not.exist");

        cy.log("rename the table and verify the tree updates");
        H.DataModel.TableSection.getNameInput()
          .clear()
          .type("Renamed Orders")
          .blur();
        cy.wait("@updateTable");
        TablePicker.getTable("Renamed Orders").should("be.visible");

        cy.log("update the owner and verify it is reflected");
        TablePicker.getTable("Renamed Orders").should(
          "not.contain.text",
          "Bobby Tables",
        );
        H.selectHasValue("Owner", "No owner").click();
        H.selectDropdown().contains("Bobby Tables").click();
        cy.wait("@updateTable");
        H.undoToastListContainer()
          .findByText("Table owner updated")
          .should("be.visible");
        H.selectHasValue("Owner", "Bobby Tables");
        TablePicker.getTable("Renamed Orders").should(
          "contain.text",
          "Bobby Tables",
        );
      },
    );
  });
});