You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2025/01/14 05:55:09 UTC

(superset) branch fix_warning created (now d51b37784a)

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

maximebeauchemin pushed a change to branch fix_warning
in repository https://gitbox.apache.org/repos/asf/superset.git


      at d51b37784a add tests

This branch includes the following new commits:

     new 487387b7bc fix: various console warnings
     new d51b37784a 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: add tests

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

maximebeauchemin pushed a commit to branch fix_warning
in repository https://gitbox.apache.org/repos/asf/superset.git

commit d51b37784a4897051367b7605cd0147c4706733f
Author: Maxime Beauchemin <ma...@gmail.com>
AuthorDate: Mon Jan 13 21:54:46 2025 -0800

    add tests
---
 superset-frontend/cypress-base/Dockerfile          |  34 +++++
 .../src/layers/common.test.ts                      | 148 +++++++++++++++++++++
 2 files changed, 182 insertions(+)

diff --git a/superset-frontend/cypress-base/Dockerfile b/superset-frontend/cypress-base/Dockerfile
new file mode 100644
index 0000000000..1d859666b2
--- /dev/null
+++ b/superset-frontend/cypress-base/Dockerfile
@@ -0,0 +1,34 @@
+FROM cypress/included:11.2.0
+
+# Install Python for the test runner
+RUN apt-get update && apt-get install -y \
+    python3 \
+    python3-pip \
+    && rm -rf /var/lib/apt/lists/*
+
+# Set working directory
+WORKDIR /app
+
+# Copy package files first for better caching
+COPY package*.json ./
+
+# Skip Cypress install since it's included in base image
+ENV CYPRESS_INSTALL_BINARY=0
+# Install TypeScript and ts-node globally
+RUN npm install
+
+# Copy your test runner script
+COPY scripts/cypress_run.py ./scripts/
+
+# Copy the rest of your application
+COPY . .
+
+# Verify Cypress is installed correctly
+RUN cypress verify
+
+# Environment variables
+ENV ELECTRON_DISABLE_GPU=true
+ENV TERM=xterm
+ENV NODE_ENV=test
+ENV BABEL_ENV=test
+ENTRYPOINT []
diff --git a/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.test.ts b/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.test.ts
new file mode 100644
index 0000000000..320aee136b
--- /dev/null
+++ b/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.test.ts
@@ -0,0 +1,148 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ * OF ANY KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { getAggFunc, commonLayerProps } from './common';
+import { JsonObject } from '@superset-ui/core';
+
+describe('getAggFunc', () => {
+  it('returns correct function for sum', () => {
+    const aggFunc = getAggFunc('sum');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(10);
+  });
+
+  it('returns correct function for min', () => {
+    const aggFunc = getAggFunc('min');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(1);
+  });
+
+  it('returns correct function for max', () => {
+    const aggFunc = getAggFunc('max');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(4);
+  });
+
+  it('returns correct function for mean', () => {
+    const aggFunc = getAggFunc('mean');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(2.5);
+  });
+
+  it('returns correct function for median', () => {
+    const aggFunc = getAggFunc('median');
+    const result = aggFunc([1, 2, 3, 4, 5]);
+    expect(result).toBe(3);
+  });
+
+  it('returns correct function for variance', () => {
+    const aggFunc = getAggFunc('variance');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(1.6666666666666667);
+  });
+
+  it('returns correct function for deviation', () => {
+    const aggFunc = getAggFunc('deviation');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBeCloseTo(1.29099, 5);
+  });
+
+  it('returns correct function for count', () => {
+    const aggFunc = getAggFunc('count');
+    const result = aggFunc([1, 2, 3, 4]);
+    expect(result).toBe(4);
+  });
+
+  it('returns correct function for p95 (percentiles)', () => {
+    const aggFunc = getAggFunc('p95');
+    const result = aggFunc([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+    expect(result).toBeCloseTo(9.55, 5);
+  });
+
+  it('throws an error for unsupported aggregation type', () => {
+    expect(() => getAggFunc('unsupported')).toThrow(
+      'Unsupported aggregation type: unsupported',
+    );
+  });
+});
+
+describe('commonLayerProps', () => {
+  const mockSetTooltip = jest.fn();
+  const mockSetTooltipContent = jest.fn(
+    () => (o: JsonObject) => `Tooltip for ${o}`,
+  );
+  const mockOnSelect = jest.fn();
+
+  it('returns correct props when js_tooltip is provided', () => {
+    const formData = { js_tooltip: 'tooltip => tooltip.content' } as any;
+    const props = commonLayerProps(
+      formData,
+      mockSetTooltip,
+      mockSetTooltipContent,
+    );
+    expect(props.pickable).toBe(true);
+    expect(props.onHover).toBeDefined();
+  });
+
+  it('calls onHover and sets tooltip', () => {
+    const formData = { js_tooltip: null } as any;
+    const props = commonLayerProps(
+      formData,
+      mockSetTooltip,
+      mockSetTooltipContent,
+    );
+
+    const mockObject = { picked: true, x: 10, y: 20 };
+    props.onHover(mockObject);
+    expect(mockSetTooltip).toHaveBeenCalledWith({
+      content: expect.any(Function), // Matches any function
+      x: 10,
+      y: 20,
+    });
+  });
+
+  it('calls onClick and opens link when js_onclick_href is provided', () => {
+    global.open = jest.fn();
+    const formData = {
+      js_onclick_href: 'o => `https://example.com/${o.id}`',
+    } as any;
+    const props = commonLayerProps(
+      formData,
+      mockSetTooltip,
+      mockSetTooltipContent,
+    );
+
+    const mockObject = { id: 42 };
+    props.onClick(mockObject);
+    expect(global.open).toHaveBeenCalledWith('https://example.com/42');
+  });
+
+  it('calls onSelect when table_filter is enabled', () => {
+    const formData = { table_filter: true, line_column: 'name' } as any;
+    const props = commonLayerProps(
+      formData,
+      mockSetTooltip,
+      mockSetTooltipContent,
+      mockOnSelect,
+    );
+
+    const mockObject = { object: { name: 'John Doe' } };
+    props.onClick(mockObject);
+    expect(mockOnSelect).toHaveBeenCalledWith('John Doe');
+  });
+});


(superset) 01/02: fix: various console warnings

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

maximebeauchemin pushed a commit to branch fix_warning
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 487387b7bc26e3a707ff18a8231ce43dfc298e47
Author: Maxime Beauchemin <ma...@gmail.com>
AuthorDate: Mon Jan 13 21:42:37 2025 -0800

    fix: various console warnings
    
    Hit this one today:
    ```
    superset_node         | WARNING in
    ./plugins/legacy-preset-chart-deckgl/src/layers/common.tsx 70:13-20
    superset_node         | export 'default' (imported as 'd3array') was not
    found in 'd3-array' (possible exports: ascending, bisect, bisectLeft,
    bisectRight, bisector, cross, descending, deviation, extent, histogram,
    max, mean, median, merge, mi
    n, pairs, permute, quantile, range, scan, shuffle, sum,
    thresholdFreedmanDiaconis, thresholdScott, thresholdSturges,
    tickIncrement, tickStep, ticks, transpose, variance, zip)
    ```
    
    Then took on fixing a few more warnings
---
 .../src/layers/common.tsx                          | 31 +++++++++++++++++++---
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.tsx b/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.tsx
index 4ff84f90e4..19c6f8889a 100644
--- a/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.tsx
+++ b/superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/common.tsx
@@ -11,15 +11,22 @@
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ * OF ANY KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 import { ReactNode } from 'react';
-import d3array, {
+import {
   ascending as d3ascending,
   quantile as d3quantile,
+  sum as d3sum,
+  mean as d3mean,
+  min as d3min,
+  max as d3max,
+  median as d3median,
+  variance as d3variance,
+  deviation as d3deviation,
 } from 'd3-array';
 import { JsonObject, JsonValue, QueryFormData } from '@superset-ui/core';
 import sandboxedEval from '../utils/sandbox';
@@ -79,6 +86,17 @@ const percentiles = {
   p99: 0.99,
 };
 
+/* Supported d3-array functions */
+const d3functions: Record<string, any> = {
+  sum: d3sum,
+  min: d3min,
+  max: d3max,
+  mean: d3mean,
+  median: d3median,
+  variance: d3variance,
+  deviation: d3deviation,
+};
+
 /* Get a stat function that operates on arrays, aligns with control=js_agg_function  */
 export function getAggFunc(
   type = 'sum',
@@ -87,10 +105,12 @@ export function getAggFunc(
   if (type === 'count') {
     return (arr: number[]) => arr.length;
   }
+
   let d3func: (
     iterable: Array<unknown>,
     accessor?: (object: JsonObject) => number | undefined,
   ) => number[] | number | undefined;
+
   if (type in percentiles) {
     d3func = (arr, acc: (object: JsonObject) => number | undefined) => {
       let sortedArr;
@@ -104,9 +124,12 @@ export function getAggFunc(
 
       return d3quantile(sortedArr, percentiles[type], acc);
     };
+  } else if (type in d3functions) {
+    d3func = d3functions[type];
   } else {
-    d3func = d3array[type];
+    throw new Error(`Unsupported aggregation type: ${type}`);
   }
+
   if (!accessor) {
     return (arr: JsonObject[]) => d3func(arr);
   }