Skip to content

Commit

Permalink
feat: 处理 getMultiData 获取数据方式
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Nov 9, 2023
1 parent 157b675 commit 1188ca2
Show file tree
Hide file tree
Showing 14 changed files with 713 additions and 222 deletions.
5 changes: 2 additions & 3 deletions packages/s2-core/__tests__/unit/facet/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ export function getMockPivotMeta() {
const sortedDimensionValues = {};
const rawRowPivotMeta = new Map();
const rawColPivotMeta = new Map();
const rawIndexesData = [];
const rawIndexesData = {};
const { fields, data, totalData } = assembleDataCfg();
return transformIndexesData({
rows: fields.rows,
columns: fields.columns,
values: fields.values,
originData: data,
data: data.concat(totalData),
indexesData: rawIndexesData,
totalData,
sortedDimensionValues,
rowPivotMeta: rawRowPivotMeta,
colPivotMeta: rawColPivotMeta,
Expand Down
366 changes: 366 additions & 0 deletions packages/s2-core/__tests__/unit/utils/dataset/pivot-data-set-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
import { MULTI_VALUE } from '@/common/constant/basic';
import type { PivotMeta, SortedDimensionValues } from '@/data-set/interface';
import {
existDimensionTotalGroup,
flattenDimensionValues,
} from '@/utils/dataset/pivot-data-set';

describe('pivot-data-set utils test', () => {
let fields: string[];
let sortedDimensionValues: SortedDimensionValues;
let pivotMeta: PivotMeta;

beforeEach(() => {
fields = ['province', 'city', 'type', 'subType'];

sortedDimensionValues = {
province: ['浙江省', '四川省', '$$total$$'],
city: [
'$$total$$[&]$$total$$',
'四川省[&]$$total$$',
'四川省[&]成都市',
'四川省[&]绵阳市',
'浙江省[&]$$total$$',
'浙江省[&]杭州市',
'浙江省[&]舟山市',
],
type: [
'$$total$$[&]$$total$$[&]$$total$$',
'四川省[&]$$total$$[&]$$total$$',
'四川省[&]成都市[&]$$total$$',
'四川省[&]成都市[&]家具',
'四川省[&]成都市[&]办公用品',
'四川省[&]绵阳市[&]$$total$$',
'四川省[&]绵阳市[&]家具',
'四川省[&]绵阳市[&]办公用品',
'浙江省[&]$$total$$[&]$$total$$',
'浙江省[&]杭州市[&]$$total$$',
'浙江省[&]杭州市[&]家具',
'浙江省[&]杭州市[&]办公用品',
'浙江省[&]舟山市[&]$$total$$',
'浙江省[&]舟山市[&]家具',
'浙江省[&]舟山市[&]办公用品',
],

subType: [
'$$total$$[&]$$total$$[&]$$total$$[&]$$total$$',
'四川省[&]$$total$$[&]$$total$$[&]$$total$$',
'四川省[&]成都市[&]$$total$$[&]$$total$$',
'四川省[&]成都市[&]家具[&]$$total$$',
'四川省[&]成都市[&]家具[&]桌子',
'四川省[&]成都市[&]家具[&]沙发',
'四川省[&]成都市[&]办公用品[&]$$total$$',
'四川省[&]成都市[&]办公用品[&]笔',
'四川省[&]成都市[&]办公用品[&]纸张',
'四川省[&]绵阳市[&]$$total$$[&]$$total$$',
'四川省[&]绵阳市[&]家具[&]$$total$$',
'四川省[&]绵阳市[&]家具[&]桌子',
'四川省[&]绵阳市[&]家具[&]沙发',
'四川省[&]绵阳市[&]办公用品[&]$$total$$',
'四川省[&]绵阳市[&]办公用品[&]笔',
'四川省[&]绵阳市[&]办公用品[&]纸张',
'浙江省[&]$$total$$[&]$$total$$[&]$$total$$',
'浙江省[&]杭州市[&]$$total$$[&]$$total$$',
'浙江省[&]杭州市[&]家具[&]$$total$$',
'浙江省[&]杭州市[&]家具[&]桌子',
'浙江省[&]杭州市[&]家具[&]沙发',
'浙江省[&]杭州市[&]办公用品[&]$$total$$',
'浙江省[&]杭州市[&]办公用品[&]笔',
'浙江省[&]杭州市[&]办公用品[&]纸张',
'浙江省[&]舟山市[&]$$total$$[&]$$total$$',
'浙江省[&]舟山市[&]家具[&]$$total$$',
'浙江省[&]舟山市[&]家具[&]桌子',
'浙江省[&]舟山市[&]家具[&]沙发',
'浙江省[&]舟山市[&]办公用品[&]$$total$$',
'浙江省[&]舟山市[&]办公用品[&]笔',
'浙江省[&]舟山市[&]办公用品[&]纸张',
],
};

pivotMeta = new Map([
[
'四川省',
{
childField: 'city',
level: 1,
children: new Map([
[
'成都市',
{
childField: 'type',
level: 1,
children: new Map([
[
'家具',
{
childField: 'subType',
level: 1,
children: new Map([
[
'桌子',
{ childFiled: null, level: 1, children: new Map() },
],
[
'沙发',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
[
'办公用品',
{
childField: 'subType',
level: 2,
children: new Map([
[
'笔',
{ childFiled: null, level: 1, children: new Map() },
],
[
'纸张',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
]),
},
],
[
'绵阳市',
{
childField: 'type',
level: 2,
children: new Map([
[
'家具',
{
childField: 'subType',
level: 1,
children: new Map([
[
'桌子',
{ childFiled: null, level: 1, children: new Map() },
],
[
'沙发',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
[
'办公用品',
{
childField: 'subType',
level: 2,
children: new Map([
[
'笔',
{ childFiled: null, level: 1, children: new Map() },
],
[
'纸张',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
]),
},
],
]),
},
],
[
'浙江省',
{
childField: 'city',
level: 2,
children: new Map([
[
'杭州市',
{
childField: 'type',
level: 1,
children: new Map([
[
'家具',
{
childField: 'subType',
level: 1,
children: new Map([
[
'桌子',
{ childFiled: null, level: 1, children: new Map() },
],
[
'沙发',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
[
'办公用品',
{
childField: 'subType',
level: 2,
children: new Map([
[
'笔',
{ childFiled: null, level: 1, children: new Map() },
],
[
'纸张',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
]),
},
],
[
'舟山市',
{
childField: 'type',
level: 2,
children: new Map([
[
'家具',
{
childField: 'subType',
level: 1,
children: new Map([
[
'桌子',
{ childFiled: null, level: 1, children: new Map() },
],
[
'沙发',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
[
'办公用品',
{
childField: 'subType',
level: 2,
children: new Map([
[
'笔',
{ childFiled: null, level: 1, children: new Map() },
],
[
'纸张',
{ childFiled: null, level: 2, children: new Map() },
],
]),
},
],
]),
},
],
]),
},
],
]);
});

test(`should return false if doesn't exist total group`, () => {
expect(
existDimensionTotalGroup(['家具', '纸张', MULTI_VALUE, MULTI_VALUE]),
).toBeFalse();

expect(
existDimensionTotalGroup([
MULTI_VALUE,
MULTI_VALUE,
MULTI_VALUE,
MULTI_VALUE,
]),
).toBeFalse();

expect(
existDimensionTotalGroup(['四川省', '成都市', '办公用品', '纸张']),
).toBeFalse();
});

test('should return true if exist total group', () => {
expect(
existDimensionTotalGroup(['四川省', MULTI_VALUE, '家具', MULTI_VALUE]),
).toBeTrue();

expect(
existDimensionTotalGroup([MULTI_VALUE, MULTI_VALUE, '家具', MULTI_VALUE]),
).toBeTrue();

expect(
existDimensionTotalGroup([MULTI_VALUE, MULTI_VALUE, MULTI_VALUE, '纸张']),
).toBeTrue();
});

test(`should return flatten dimension values if doesn't exist total group`, () => {
expect(
flattenDimensionValues(
fields,
['四川省', '成都市', '办公用品', '纸张'],
sortedDimensionValues,
),
).toEqual([['四川省', '成都市', '办公用品', '纸张']]);

expect(
flattenDimensionValues(
fields,
['四川省', '成都市', MULTI_VALUE, MULTI_VALUE],
sortedDimensionValues,
),
).toEqual([['四川省', '成都市', MULTI_VALUE, MULTI_VALUE]]);
});

test(`should return flatten dimension values if exist total group`, () => {
expect(
flattenDimensionValues(
fields,
[MULTI_VALUE, '成都市', MULTI_VALUE, MULTI_VALUE],
sortedDimensionValues,
),
).toEqual([['四川省', '成都市', MULTI_VALUE, MULTI_VALUE]]);

expect(
flattenDimensionValues(
fields,
[MULTI_VALUE, MULTI_VALUE, '办公用品', MULTI_VALUE],
sortedDimensionValues,
),
).toEqual([
['四川省', '成都市', '办公用品', MULTI_VALUE],
['四川省', '绵阳市', '办公用品', MULTI_VALUE],
['浙江省', '杭州市', '办公用品', MULTI_VALUE],
['浙江省', '舟山市', '办公用品', MULTI_VALUE],
]);

expect(
flattenDimensionValues(
fields,
['四川省', MULTI_VALUE, '办公用品', MULTI_VALUE],
sortedDimensionValues,
),
).toEqual([
['四川省', '成都市', '办公用品', MULTI_VALUE],
['四川省', '绵阳市', '办公用品', MULTI_VALUE],
]);

expect(
flattenDimensionValues(
fields,
[MULTI_VALUE, '成都市', MULTI_VALUE, '纸张'],
sortedDimensionValues,
),
).toEqual([['四川省', '成都市', '办公用品', '纸张']]);
});
});
Loading

0 comments on commit 1188ca2

Please sign in to comment.