Skip to content

Commit

Permalink
test: use stable random for stories (apache#861)
Browse files Browse the repository at this point in the history
* test: use stable random for stories

* Seed with superset-ui

* Add tests for random
  • Loading branch information
ktmud authored and zhaoyongjie committed Nov 24, 2021
1 parent bc0d76b commit b37675a
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/d3-time-format": "^2.1.0",
"@types/lodash": "^4.14.149",
"@types/rison": "0.0.6",
"@types/seedrandom": "^2.4.28",
"@vx/responsive": "^0.0.197",
"csstype": "^2.6.4",
"d3-format": "^1.3.2",
Expand All @@ -56,6 +57,7 @@
"react-error-boundary": "^1.2.5",
"reselect": "^4.0.0",
"rison": "^0.1.1",
"seedrandom": "^3.0.5",
"whatwg-fetch": "^3.0.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export { default as isRequired } from './isRequired';
export { default as makeSingleton } from './makeSingleton';
export { default as promiseTimeout } from './promiseTimeout';
export { default as logging } from './logging';
export * from './random';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 seedrandom from 'seedrandom';

let random = seedrandom('superset-ui');

export function seed(seed: string) {
random = seedrandom(seed);
return random;
}

export function seedRandom() {
return random();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 { seed, seedRandom } from '../../src';

describe('random', () => {
it('seeded random should return the same value', () => {
expect(seedRandom()).toEqual(0.7237953289342797);
});

it('should allow update seed', () => {
const a = seed('abc');
const b = seed('abc');
expect(a()).toEqual(b());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@storybook/addons": "^5.3.18",
"@storybook/preset-typescript": "^3.0.0",
"@storybook/react": "^5.3.18",
"@superset-ui/core": "0.15.17",
"@superset-ui/core": "file:../superset-ui-core/src",
"@superset-ui/legacy-plugin-chart-calendar": "0.15.17",
"@superset-ui/legacy-plugin-chart-chord": "0.15.17",
"@superset-ui/legacy-plugin-chart-country-map": "0.15.17",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { SuperChart } from '@superset-ui/core';
import { SuperChart, seedRandom } from '@superset-ui/core';
import dummyDatasource from '../../../../../shared/dummyDatasource';

const data: {
Expand All @@ -14,8 +14,8 @@ const LONG_LABEL =

for (let i = 0; i < 50; i += 1) {
data[0].values.push({
x: `${LONG_LABEL.substring(0, Math.round(Math.random() * LONG_LABEL.length))} ${i + 1}`,
y: Math.round(Math.random() * 10000),
x: `${LONG_LABEL.substring(0, Math.round(seedRandom() * LONG_LABEL.length))} ${i + 1}`,
y: Math.round(seedRandom() * 10000),
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/**
* 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 { seedRandom } from '@superset-ui/core';
import loadMap from '../../../../../../plugins/plugin-chart-choropleth-map/src/chart/loadMap';

const FRUITS = ['apple', 'banana', 'grape'];
Expand All @@ -20,7 +39,7 @@ export default async function generateFakeMapData(map: string) {
.map(f => metadata.keyAccessor(f))
.map(key => ({
key,
favoriteFruit: FRUITS[Math.round(Math.random() * 2)],
numStudents: Math.round(Math.random() * 100),
favoriteFruit: FRUITS[Math.round(seedRandom() * 2)],
numStudents: Math.round(seedRandom() * 100),
}));
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { SuperChart } from '@superset-ui/core';
import { SuperChart, seedRandom } from '@superset-ui/core';
import data from '../data/data';
import { LINE_PLUGIN_TYPE } from '../constants';
import dummyDatasource from '../../../../../shared/dummyDatasource';

const missingData = data.map(({ y, ...rest }) => ({
...rest,
y: Math.random() < 0.25 ? null : y,
y: seedRandom() < 0.25 ? null : y,
}));

const missing = () => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import cloudLayout, { Word } from 'd3-cloud';
import { PlainObject, createEncoderFactory, DeriveEncoding } from 'encodable';
import { SupersetThemeProps, withTheme } from '@superset-ui/core';
import { SupersetThemeProps, withTheme, seedRandom } from '@superset-ui/core';

export const ROTATION = {
flat: () => 0,
// this calculates a random rotation between -90 and 90 degrees.
random: () => Math.floor(Math.random() * 6 - 3) * 30,
square: () => Math.floor(Math.random() * 2) * 90,
random: () => Math.floor(seedRandom() * 6 - 3) * 30,
square: () => Math.floor(seedRandom() * 2) * 90,
};

export type RotationType = keyof typeof ROTATION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import d3 from 'd3';
import PropTypes from 'prop-types';
import cloudLayout from 'd3-cloud';
import { CategoricalColorNamespace } from '@superset-ui/core';
import { CategoricalColorNamespace, seedRandom } from '@superset-ui/core';

const ROTATION = {
square: () => Math.floor(Math.random() * 2) * 90,
square: () => Math.floor(seedRandom() * 2) * 90,
flat: () => 0,
random: () => Math.floor(Math.random() * 6 - 3) * 30,
random: () => Math.floor(seedRandom() * 6 - 3) * 30,
};

const propTypes = {
Expand Down
8 changes: 8 additions & 0 deletions superset-frontend/temporary_superset_ui/superset-ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4856,6 +4856,9 @@
conventional-changelog-cli "^2.0.12"
cz-conventional-changelog "^2.1.0"

"@superset-ui/core@file:packages/superset-ui-core/src":
version "0.0.0"

"@superset-ui/legacy-plugin-chart-word-cloud@^0.11.15":
version "0.11.15"
resolved "https://registry.yarnpkg.com/@superset-ui/legacy-plugin-chart-word-cloud/-/legacy-plugin-chart-word-cloud-0.11.15.tgz#70a146aaf3cf1977c29086c069f0216325f092b2"
Expand Down Expand Up @@ -5459,6 +5462,11 @@
resolved "https://registry.yarnpkg.com/@types/rison/-/rison-0.0.6.tgz#405605f63d4fe0ec0d66b2dd7eaf51a99a51144e"
integrity sha512-mE3eRK0fpTN/GnNBOIg2tGq2cFhchQXF6fCbrLxus75TgnoOECbdHikr948FGO/UAml7/ZhLMa5FbGkF5PKvmw==

"@types/seedrandom@^2.4.28":
version "2.4.28"
resolved "https://registry.yarnpkg.com/@types/seedrandom/-/seedrandom-2.4.28.tgz#9ce8fa048c1e8c85cb71d7fe4d704e000226036f"
integrity sha512-SMA+fUwULwK7sd/ZJicUztiPs8F1yCPwF3O23Z9uQ32ME5Ha0NmDK9+QTsYE4O2tHXChzXomSWWeIhCnoN1LqA==

"@types/shortid@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/shortid/-/shortid-0.0.29.tgz#8093ee0416a6e2bf2aa6338109114b3fbffa0e9b"
Expand Down

0 comments on commit b37675a

Please sign in to comment.