Skip to content

Commit

Permalink
refactor(dataSet): 批量获取单元格数据 API 保持风格统一 (#2366)
Browse files Browse the repository at this point in the history
* refactor(data-set): 批量获取单元格数据 API 保持风格统一

* chore: update

* docs: typo
  • Loading branch information
lijinke666 authored Oct 31, 2023
1 parent 5131feb commit 16e05e2
Show file tree
Hide file tree
Showing 29 changed files with 296 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ describe('Custom Tree Dataset Test', () => {
sub_type: '桌子',
[EXTRA_FIELD]: 'measure-a',
},
isTotals: true,
})!
.getOrigin(),
).toContainEntries([['measure-a', 1]]);
Expand All @@ -114,7 +113,6 @@ describe('Custom Tree Dataset Test', () => {
sub_type: '椅子',
[EXTRA_FIELD]: 'measure-e',
},
isTotals: true,
})!
.getOrigin(),
).toContainEntries([['measure-e', 55]]);
Expand Down
59 changes: 32 additions & 27 deletions packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('Pivot Dataset Test', () => {
expect(cell2!.getOrigin()).toContainEntries([['number', 352]]);
});

describe('getMultiData function', () => {
describe('getCellMultiData function', () => {
beforeEach(() => {
dataSet.setDataCfg(assembleDataCfg());
});
Expand All @@ -204,86 +204,91 @@ describe('Pivot Dataset Test', () => {
[EXTRA_FIELD]: 'number',
};

expect(dataSet.getMultiData(specialQuery)).toHaveLength(1);
expect(dataSet.getCellMultiData({ query: specialQuery })).toHaveLength(
1,
);
expect(
dataSet.getMultiData(specialQuery)[0].getOrigin(),
dataSet.getCellMultiData({ query: specialQuery })[0].getOrigin(),
).toContainEntries([['number', 7789]]);
});

test('should get all detail data when child dimension is not specified', () => {
expect(
dataSet.getMultiData(
{
dataSet.getCellMultiData({
query: {
province: '浙江省',
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
},
{
totals: {
row: { totalDimensions: false },
column: { totalDimensions: false },
},
),
}),
).toHaveLength(4);

expect(
dataSet.getMultiData(
{
dataSet.getCellMultiData({
query: {
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
},
{
totals: {
row: { totalDimensions: false },
column: { totalDimensions: false },
},
),
}),
).toHaveLength(8);

expect(
dataSet.getMultiData(
{
dataSet.getCellMultiData({
query: {
type: '家具',
[EXTRA_FIELD]: 'number',
},
{
totals: {
row: { totalDimensions: false },
column: { totalDimensions: false },
},
),
}),
).toHaveLength(16);

expect(
dataSet.getMultiData(
{
dataSet.getCellMultiData({
query: {
[EXTRA_FIELD]: 'number',
},
{
totals: {
row: { totalDimensions: false },
column: { totalDimensions: false },
},
),
}),
).toHaveLength(32);
});

test('should only query grand total data', () => {
expect(
dataSet.getMultiData(
{ [EXTRA_FIELD]: 'number' },
{ row: { grandTotalOnly: true }, column: { grandTotalOnly: true } },
),
dataSet.getCellMultiData({
query: { [EXTRA_FIELD]: 'number' },
totals: {
row: { grandTotalOnly: true },
column: { grandTotalOnly: true },
},
}),
).toHaveLength(1);
});

test('should query all grand total and sub total data in columns for all cities', () => {
expect(
dataSet.getMultiData(
{ [EXTRA_FIELD]: 'number' },
{
dataSet.getCellMultiData({
query: { [EXTRA_FIELD]: 'number' },
totals: {
row: { totalDimensions: false },
column: { grandTotalOnly: false, subTotalOnly: true },
},
),
}),
).toHaveLength(24);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { get, keys } from 'lodash';
import * as multiDataCfg from 'tests/data/simple-data.json';
import * as mockData from 'tests/data/mock-dataset.json';
import { assembleDataCfg, TOTALS_OPTIONS } from '../../util';
import type { Query } from '../../../src/data-set/interface';
import { EXTRA_FIELD, TOTAL_VALUE, VALUE_FIELD } from '@/common/constant';
import {
type S2DataConfig,
Expand Down Expand Up @@ -552,46 +553,54 @@ describe('Pivot Dataset Total Test', () => {
});
});

test('getMultiData function', () => {
const specialQuery = {
test('getCellMultiData function', () => {
const specialQuery: Query = {
province: '浙江省',
city: '杭州市',
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
};

expect(dataSet.getMultiData(specialQuery)).toHaveLength(1);
expect(dataSet.getCellMultiData({ query: specialQuery })).toHaveLength(1);
expect(
dataSet.getMultiData(specialQuery)[0].getOrigin(),
dataSet.getCellMultiData({ query: specialQuery })[0].getOrigin(),
).toContainEntries([['number', 7789]]);
expect(
dataSet.getMultiData({
province: '浙江省',
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
dataSet.getCellMultiData({
query: {
province: '浙江省',
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
},
}),
).toHaveLength(5);

expect(
dataSet.getMultiData({
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
dataSet.getCellMultiData({
query: {
type: '家具',
sub_type: '桌子',
[EXTRA_FIELD]: 'number',
},
}),
).toHaveLength(11);

expect(
dataSet.getMultiData({
type: '家具',
[EXTRA_FIELD]: 'number',
dataSet.getCellMultiData({
query: {
type: '家具',
[EXTRA_FIELD]: 'number',
},
}),
).toHaveLength(33);

expect(
dataSet.getMultiData({
[EXTRA_FIELD]: 'number',
dataSet.getCellMultiData({
query: {
[EXTRA_FIELD]: 'number',
},
}),
).toHaveLength(77);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/__tests__/unit/facet/pivot-facet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jest.mock('@/data-set/pivot-data-set', () => {
getFieldMeta: (field: string, meta: ViewMeta) => find(meta, { field }),
getFieldName: actualPivotDataSet.prototype.getFieldName,
getCellData: actualPivotDataSet.prototype.getCellData,
getMultiData: jest.fn(),
getCellMultiData: jest.fn(),
getDimensionValues: actualPivotDataSet.prototype.getDimensionValues,
};
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/__tests__/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const createFakeSpreadSheet = () => {
});
s2.dataSet = {
...s2.dataCfg,
getMultiData() {
getCellMultiData() {
return [];
},
} as unknown as any;
Expand Down Expand Up @@ -128,7 +128,7 @@ export const createFakeSpreadSheet = () => {
s2.dataSet = {
getFieldDescription: jest.fn(),
getCustomFieldDescription: jest.fn(),
getMultiData: jest.fn(() => []),
getCellMultiData: jest.fn(() => []),
} as unknown as BaseDataSet;

s2.getCellType = jest.fn();
Expand Down
9 changes: 3 additions & 6 deletions packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,16 @@ export interface Total {
subTotalsLabel?: string;
}

/**
* tableau的英文是这个,这里有个绕的概念
* 如,某行维度需要展示小计,实际上是将对应的一列数据进行聚合,所以文案上显示的应该是“展示列小计”
* 但是内部配置我倾向于仍然按照字段所属维度区,即配置的row,代表的是行维度而不是行小计
*/
export interface Totals {
row?: Total;
col?: Total;
}

export interface Sort {
/** 字段id,业务中一般是displayId */
/** 字段id */
sortFieldId: string;

/** 排序方式 */
sortMethod?: SortMethod;

/** 自定义排序 */
Expand Down
Loading

0 comments on commit 16e05e2

Please sign in to comment.