Skip to content

Commit 37da4ac

Browse files
authored
test: add test cases for utils (#845)
1 parent 5b252aa commit 37da4ac

11 files changed

Lines changed: 105 additions & 96 deletions

File tree

__tests__/unit/extract/mock-extract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { requestTboxLLM } from '@ava/utils/llm';
2-
import { getExtractPrompt } from '@ava/extract/extract/prompt';
1+
import { requestTboxLLM } from '../../../src/utils/llm';
2+
import { getExtractPrompt } from '../../../src/extract/extract/prompt';
33

44
export const extract = async (value: string) => {
55
const llmAuth = process.env.LLM_AUTH;

__tests__/unit/utils/aggr.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AggregatorMap, aggregate, aggregateWithMeasures, aggregateWithSeries } from '../../../src/utils/aggregate';
1+
import { AGGREGATOR_MAP, aggregate, aggregateWithMeasures, aggregateWithSeries } from '../../../src/utils/aggregate';
22

33
const data = [
44
{ gender: 'M', count: 40, class: 'class1', grade: 'grade1', score: 7 },
@@ -14,24 +14,25 @@ const data = [
1414
{ gender: 'M', count: 28, class: 'class3', grade: 'grade2', score: 10 },
1515
{ gender: 'F', count: 36, class: 'class3', grade: 'grade2', score: 9 },
1616
];
17+
1718
describe('Aggregation', () => {
1819
test('SUM aggregation', () => {
19-
expect(AggregatorMap.SUM(data, 'count')).toStrictEqual(396);
20+
expect(AGGREGATOR_MAP.SUM(data, 'count')).toStrictEqual(396);
2021
});
2122
test('MEAN aggregation', () => {
22-
expect(AggregatorMap.MEAN(data, 'count')).toStrictEqual(33);
23+
expect(AGGREGATOR_MAP.MEAN(data, 'count')).toStrictEqual(33);
2324
});
2425
test('MAX aggregation', () => {
25-
expect(AggregatorMap.MAX(data, 'count')).toStrictEqual(45);
26+
expect(AGGREGATOR_MAP.MAX(data, 'count')).toStrictEqual(45);
2627
});
2728
test('MIN aggregation', () => {
28-
expect(AggregatorMap.MIN(data, 'count')).toStrictEqual(20);
29+
expect(AGGREGATOR_MAP.MIN(data, 'count')).toStrictEqual(20);
2930
});
3031
test('COUNT aggregation', () => {
31-
expect(AggregatorMap.COUNT(data, 'gender')).toStrictEqual(12);
32+
expect(AGGREGATOR_MAP.COUNT(data, 'gender')).toStrictEqual(12);
3233
});
3334
test('COUNT_DISTINCT aggregation', () => {
34-
expect(AggregatorMap.COUNT_DISTINCT(data, 'gender')).toStrictEqual(2);
35+
expect(AGGREGATOR_MAP.COUNT_DISTINCT(data, 'gender')).toStrictEqual(2);
3536
});
3637

3738
test('aggregation', () => {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { assert, isParentChild, range, sign, unique } from '../../../src/utils/common';
2+
3+
describe('common', () => {
4+
test('sign', () => {
5+
expect(sign(10)).toBe(1);
6+
expect(sign(-5)).toBe(-1);
7+
expect(sign(0)).toBe(0);
8+
});
9+
10+
test('unique', () => {
11+
const data = [1, 2, 3, 3, 2, 1];
12+
expect(unique(data)).toStrictEqual([
13+
[1, 2, 3],
14+
[2, 2, 2],
15+
]);
16+
});
17+
18+
test('range', () => {
19+
const data = 4;
20+
expect(range(data)).toStrictEqual([0, 1, 2, 3]);
21+
});
22+
23+
test('assert', () => {
24+
expect(() => assert(false, 'It is false!')).toThrow('It is false!');
25+
});
26+
27+
test('isParentChild', () => {
28+
expect(isParentChild(['a', 'b', 'c'], [1, 2, 3])).toBe(true);
29+
expect(isParentChild(['a', 'a', 'c'], [1, 1, 3])).toBe(true);
30+
expect(isParentChild(['a', 'a', 'c'], [1, 2, 3])).toBe(true);
31+
expect(isParentChild(['a', 'b', 'c'], [1, 1, 3])).toBe(false);
32+
});
33+
});

__tests__/unit/utils/statictics.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import '@testing-library/jest-dom/extend-expect';
22
import { isEqual } from 'lodash';
3-
43
import { isArray, isNumber, nOnes, range } from '../../../src/utils';
54
import {
65
valid,

src/extract/constants.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
export const SPECIAL_BOOLEANS = [
2-
[true, false],
3-
[0, 1],
4-
['true', 'false'],
5-
['Yes', 'No'],
6-
['True', 'False'],
7-
['0', '1'],
8-
['是', '否'],
9-
];
10-
11-
// For isDateString.ts
12-
export const DELIMITER = '([-_./\\s])';
13-
export const YEAR = '(?<year>(18|19|20)\\d{2})';
14-
export const MONTH = '(?<month>0?[1-9]|1[012])';
15-
export const DAY = '(?<day>0?[1-9]|[12]\\d|3[01])';
16-
export const WEEK = '(?<week>[0-4]\\d|5[0-2])';
17-
export const WEEKDAY = '(?<weekday>[1-7])';
18-
export const BASE_HOUR = '(0?\\d|1\\d|2[0-4])';
19-
export const BASE_MINUTE = '(0?\\d|[012345]\\d)';
20-
export const HOUR = `(?<hour>${BASE_MINUTE})`;
21-
export const MINUTE = `(?<minute>${BASE_MINUTE})`;
22-
export const SECOND = `(?<second>${BASE_MINUTE})`;
23-
export const MILLISECOND = '(?<millisecond>\\d{1,4})';
24-
export const YEARDAY = '(?<yearDay>(([0-2]\\d|3[0-5])\\d)|36[0-6])';
25-
export const OFFSET = `(?<offset>Z|[+-]${BASE_HOUR}(:${BASE_MINUTE})?)`;
26-
271
export enum DATA_SHAPE {
282
PLAIN = 'plain',
293
HIERARCHY = 'hierarchy',

src/utils/aggregate.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,49 @@ import { groupBy, sumBy, minBy, maxBy, meanBy, sortBy, flatten, uniq } from 'lod
22

33
import type { Aggregator, Datum, Measure, MeasureMethod } from '../types';
44

5+
/**
6+
* Aggregate the sum of measure field.
7+
*/
58
const sum = (data: Datum[], measure: string) => {
69
return sumBy(data, measure);
710
};
811

12+
/**
13+
* Aggregate the count of measure field.
14+
*/
915
const count = (data: Datum[], measure: string) => {
1016
return data.filter((item) => measure in item).length;
1117
};
1218

19+
/**
20+
* Aggregate the distinct count of measure field.
21+
*/
1322
const countDistinct = (data: Datum[], measure: string) => {
1423
return uniq(data.filter((item) => measure in item).map((item) => item[measure])).length;
1524
};
1625

26+
/**
27+
* Aggregate the max of measure field.
28+
*/
1729
const max = (data: Datum[], measure: string) => {
1830
return maxBy(data, measure)?.[measure] as number;
1931
};
2032

33+
/**
34+
* Aggregate the min of measure field.
35+
*/
2136
const min = (data: Datum[], measure: string) => {
2237
return minBy(data, measure)?.[measure] as number;
2338
};
2439

40+
/**
41+
* Aggregate the mean of measure field.
42+
*/
2543
const mean = (data: Datum[], measure: string) => {
2644
return meanBy(data, measure);
2745
};
2846

29-
export const AggregatorMap: Record<MeasureMethod, Aggregator> = {
47+
export const AGGREGATOR_MAP: Record<MeasureMethod, Aggregator> = {
3048
SUM: sum,
3149
COUNT: count,
3250
MAX: max,
@@ -35,14 +53,17 @@ export const AggregatorMap: Record<MeasureMethod, Aggregator> = {
3553
COUNT_DISTINCT: countDistinct,
3654
};
3755

56+
/**
57+
* Aggregate data by groupByField and measures.
58+
*/
3859
export function aggregate(data: Datum[], groupByField: string, measures: Measure[], sort?: boolean) {
3960
const grouped = groupBy(data, groupByField);
4061
const entries = sort ? sortBy(Object.entries(grouped), '0') : Object.entries(grouped);
4162
return entries.map(([value, dataGroup]) => {
4263
const datum: Datum = { [groupByField]: value };
4364
measures.forEach((measure) => {
4465
const { fieldName: measureField, method } = measure;
45-
const aggregator = AggregatorMap[method];
66+
const aggregator = AGGREGATOR_MAP[method];
4667
datum[measureField] = aggregator(dataGroup, measureField);
4768
});
4869
return datum;
@@ -56,7 +77,7 @@ export function aggregateWithMeasures(data: Datum[], groupByField: string, measu
5677
measures.forEach((measure) => {
5778
const { fieldName: measureField, method } = measure;
5879
if (measureField in dataGroup[0]) {
59-
const aggregator = AggregatorMap[method];
80+
const aggregator = AGGREGATOR_MAP[method];
6081
const measureValue = aggregator(dataGroup, measureField);
6182
result.push({
6283
[groupByField]: value,
@@ -72,7 +93,7 @@ export function aggregateWithMeasures(data: Datum[], groupByField: string, measu
7293
export function aggregateWithSeries(data: Datum[], groupByField: string, measure: Measure, expandingField: string) {
7394
const grouped = groupBy(data, groupByField);
7495
const { fieldName: measureField, method } = measure;
75-
const aggregator = AggregatorMap[method];
96+
const aggregator = AGGREGATOR_MAP[method];
7697
return flatten(
7798
Object.entries(grouped).map(([value, dataGroup]) => {
7899
const childGrouped = groupBy(dataGroup, expandingField);

src/utils/common.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { Meta } from '../types';
44

55
import { isArray } from './isType';
66

7-
// sign
7+
/**
8+
* Returns the sign of the x, indicating whether x is positive, negative or zero.
9+
*/
810
export function sign(value: number) {
9-
if (value > 0) return 1;
10-
return value < 0 ? -1 : 0;
11+
return Math.sign(value);
1112
}
1213

13-
// unique
14+
/**
15+
* Returns the unique elements in the array and their counts.
16+
*/
1417
export function unique(arr: string[] | number[]): [(string | number)[], number[]] {
1518
const sorted = arr.slice().sort();
1619

@@ -28,7 +31,9 @@ export function unique(arr: string[] | number[]): [(string | number)[], number[]
2831
return [uniqArr, countArr];
2932
}
3033

31-
// rank
34+
/**
35+
* Returns the rank of each element in the array.
36+
*/
3237
export function rank(arr: (string | number)[]): number[] {
3338
const sorted = arr.slice().sort();
3439
const rank: number[] = [];

src/utils/dataFormat.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/utils/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ export * from './arr2map';
44
export * from './aggregate';
55
export * from './statistics';
66
export * from './llm';
7-
8-
export { default as dataFormat } from './dataFormat';

0 commit comments

Comments
 (0)