You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by di...@apache.org on 2022/10/18 14:01:02 UTC

[superset] branch fix/default-temporal-column created (now 0dcac0aa82)

This is an automated email from the ASF dual-hosted git repository.

diegopucci pushed a change to branch fix/default-temporal-column
in repository https://gitbox.apache.org/repos/asf/superset.git


      at 0dcac0aa82 Fix and add tests

This branch includes the following new commits:

     new 5bf605980d WIP
     new 0dcac0aa82 Fix and add tests

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[superset] 02/02: Fix and add tests

Posted by di...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

diegopucci pushed a commit to branch fix/default-temporal-column
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 0dcac0aa82be1aad9fc275ef5cabc3f751252a8b
Author: geido <di...@gmail.com>
AuthorDate: Tue Oct 18 17:00:23 2022 +0300

    Fix and add tests
---
 .../DatasourceControl/DatasourceControl.test.tsx   | 124 ++++++++++++++++++++-
 .../controls/DatasourceControl/index.jsx           |  31 +++---
 2 files changed, 136 insertions(+), 19 deletions(-)

diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx
index 4d01ec7a79..46a0fdaf61 100644
--- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx
+++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx
@@ -18,9 +18,10 @@
  */
 
 import React from 'react';
-import { render, screen, act } from 'spec/helpers/testing-library';
+import { render, screen, act, waitFor } from 'spec/helpers/testing-library';
 import userEvent from '@testing-library/user-event';
 import { SupersetClient, DatasourceType } from '@superset-ui/core';
+import fetchMock from 'fetch-mock';
 import DatasourceControl from '.';
 
 const SupersetClientGet = jest.spyOn(SupersetClient, 'get');
@@ -45,7 +46,10 @@ const createProps = () => ({
   },
   validationErrors: [],
   name: 'datasource',
-  actions: {},
+  actions: {
+    changeDatasource: jest.fn(),
+    setControlValue: jest.fn(),
+  },
   isEditable: true,
   user: {
     createdOn: '2021-04-27T18:12:38.952304',
@@ -62,6 +66,17 @@ const createProps = () => ({
   onDatasourceSave: jest.fn(),
 });
 
+fetchMock.post('glob:*/datasource/save/', {
+  ...createProps().datasource,
+});
+
+async function openAndSaveChanges() {
+  userEvent.click(screen.getByTestId('datasource-menu-trigger'));
+  userEvent.click(await screen.findByTestId('edit-dataset'));
+  userEvent.click(await screen.findByTestId('datasource-modal-save'));
+  userEvent.click(await screen.findByText('OK'));
+}
+
 test('Should render', async () => {
   const props = createProps();
   render(<DatasourceControl {...props} />);
@@ -229,3 +244,108 @@ test('Click on Save as dataset', async () => {
   expect(screen.getByRole('button', { name: /close/i })).toBeVisible();
   expect(dropdownField).toBeVisible();
 });
+
+test('should set the default temporal column', async () => {
+  const props = createProps();
+  const overrideProps = {
+    ...props,
+    form_data: {
+      granularity_sqla: 'test-col',
+    },
+    datasource: {
+      ...props.datasource,
+      main_dttm_col: 'test-default',
+      columns: [
+        {
+          column_name: 'test-col',
+          is_dttm: false,
+        },
+        {
+          column_name: 'test-default',
+          is_dttm: true,
+        },
+      ],
+    },
+  };
+  render(<DatasourceControl {...props} {...overrideProps} />, {
+    useRedux: true,
+  });
+
+  await openAndSaveChanges();
+  await waitFor(() => {
+    expect(props.actions.setControlValue).toHaveBeenCalledWith(
+      'granularity_sqla',
+      'test-default',
+    );
+  });
+});
+
+test('should set the first available temporal column', async () => {
+  const props = createProps();
+  const overrideProps = {
+    ...props,
+    form_data: {
+      granularity_sqla: 'test-col',
+    },
+    datasource: {
+      ...props.datasource,
+      main_dttm_col: null,
+      columns: [
+        {
+          column_name: 'test-col',
+          is_dttm: false,
+        },
+        {
+          column_name: 'test-first',
+          is_dttm: true,
+        },
+      ],
+    },
+  };
+  render(<DatasourceControl {...props} {...overrideProps} />, {
+    useRedux: true,
+  });
+
+  await openAndSaveChanges();
+  await waitFor(() => {
+    expect(props.actions.setControlValue).toHaveBeenCalledWith(
+      'granularity_sqla',
+      'test-first',
+    );
+  });
+});
+
+test('should set the temporal column at null', async () => {
+  const props = createProps();
+  const overrideProps = {
+    ...props,
+    form_data: {
+      granularity_sqla: null,
+    },
+    datasource: {
+      ...props.datasource,
+      main_dttm_col: null,
+      columns: [
+        {
+          column_name: 'test-col',
+          is_dttm: false,
+        },
+        {
+          column_name: 'test-col-2',
+          is_dttm: false,
+        },
+      ],
+    },
+  };
+  render(<DatasourceControl {...props} {...overrideProps} />, {
+    useRedux: true,
+  });
+
+  await openAndSaveChanges();
+  await waitFor(() => {
+    expect(props.actions.setControlValue).toHaveBeenCalledWith(
+      'granularity_sqla',
+      null,
+    );
+  });
+});
diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
index b30c172204..bca5639c9f 100644
--- a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
+++ b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
@@ -169,28 +169,25 @@ class DatasourceControl extends React.PureComponent {
     };
   }
 
-  onDatasourceSave = (datasource) => {
+  onDatasourceSave = datasource => {
     this.props.actions.changeDatasource(datasource);
     const { columns } = this.props.datasource;
     const timeCol = this.props.form_data?.granularity_sqla;
-    const timeColConf = columns.find(({ column_name }) => column_name === timeCol);
-    const firstDttmCol = columns.find(column => column.is_dttm)?.column_name || null;
-    const currentMainDttmCol = columns.find(c => c.column_name === datasource.main_dttm_col);
-    const setDttmCol = currentMainDttmCol?.is_dttm ? currentMainDttmCol.column_name : firstDttmCol;
-    console.log("firstDttmCol", firstDttmCol);
-    console.log("currentMainDttmCol", currentMainDttmCol);
-
-
+    const timeColConf = columns.find(
+      ({ column_name }) => column_name === timeCol,
+    );
+    const firstDttmCol =
+      columns.find(column => column.is_dttm)?.column_name || null;
+    const currentMainDttmCol = columns.find(
+      c => c.column_name === datasource.main_dttm_col,
+    );
+    const setDttmCol = currentMainDttmCol?.is_dttm
+      ? currentMainDttmCol.column_name
+      : firstDttmCol;
     if (datasource.type === 'table') {
       // resets new default time col or picks the first available one
-      if (
-        datasource.main_dttm_col !== timeCol || !timeColConf?.is_dttm
-      ) {
-          console.log("setDttmCol", setDttmCol);
-          this.props.actions.setControlValue(
-            'granularity_sqla',
-            setDttmCol,
-          );
+      if (!timeColConf?.is_dttm) {
+        this.props.actions.setControlValue('granularity_sqla', setDttmCol);
       }
     }
 


[superset] 01/02: WIP

Posted by di...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

diegopucci pushed a commit to branch fix/default-temporal-column
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 5bf605980d9258f9474b23f2b7b4d9c18f8060a6
Author: geido <di...@gmail.com>
AuthorDate: Tue Oct 18 14:30:59 2022 +0300

    WIP
---
 .../controls/DatasourceControl/index.jsx           | 34 ++++++++++++++--------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
index 9fa4a8a2ba..b30c172204 100644
--- a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
+++ b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx
@@ -169,21 +169,31 @@ class DatasourceControl extends React.PureComponent {
     };
   }
 
-  onDatasourceSave = datasource => {
+  onDatasourceSave = (datasource) => {
     this.props.actions.changeDatasource(datasource);
-    const timeCol = this.props.form_data?.granularity_sqla;
     const { columns } = this.props.datasource;
-    const firstDttmCol = columns.find(column => column.is_dttm);
-    if (
-      datasource.type === 'table' &&
-      !columns.find(({ column_name }) => column_name === timeCol)?.is_dttm
-    ) {
-      // set `granularity_sqla` to first datatime column name or null
-      this.props.actions.setControlValue(
-        'granularity_sqla',
-        firstDttmCol ? firstDttmCol.column_name : null,
-      );
+    const timeCol = this.props.form_data?.granularity_sqla;
+    const timeColConf = columns.find(({ column_name }) => column_name === timeCol);
+    const firstDttmCol = columns.find(column => column.is_dttm)?.column_name || null;
+    const currentMainDttmCol = columns.find(c => c.column_name === datasource.main_dttm_col);
+    const setDttmCol = currentMainDttmCol?.is_dttm ? currentMainDttmCol.column_name : firstDttmCol;
+    console.log("firstDttmCol", firstDttmCol);
+    console.log("currentMainDttmCol", currentMainDttmCol);
+
+
+    if (datasource.type === 'table') {
+      // resets new default time col or picks the first available one
+      if (
+        datasource.main_dttm_col !== timeCol || !timeColConf?.is_dttm
+      ) {
+          console.log("setDttmCol", setDttmCol);
+          this.props.actions.setControlValue(
+            'granularity_sqla',
+            setDttmCol,
+          );
+      }
     }
+
     if (this.props.onDatasourceSave) {
       this.props.onDatasourceSave(datasource);
     }