Skip to content

Commit b5434a1

Browse files
committed
test(pivot): add test for single and multi dimensions and fix error thrown
1 parent 02075d9 commit b5434a1

File tree

2 files changed

+144
-24
lines changed

2 files changed

+144
-24
lines changed

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.pipes.spec.ts

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,124 @@ describe('Pivot pipes', () => {
4848
filters: null
4949
};
5050

51-
fit('transforms flat data to pivot data', () => {
51+
it('transforms flat data to pivot data', () => {
5252
const rowPipeResult = rowPipe.transform(data, pivotConfigHierarchy.rows, pivotConfigHierarchy.values);
5353
const columnPipeResult = columnPipe.transform(rowPipeResult, pivotConfigHierarchy.columns, pivotConfigHierarchy.values);
5454
expect(columnPipeResult).toEqual([
55-
{ field1: 'All', All: 2127, Bulgaria: 774, USA: 829, Uruguay: 524, level: 0, records: [
56-
{ field1: 'Clothing', All: 1526, Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
57-
{ field1: 'Bikes', All: 68, Uruguay: 68, level: 1 },
58-
{ field1: 'Accessories', All: 293, USA: 293, level: 1 },
59-
{ field1: 'Components', All: 240, USA: 240, level: 1 }
60-
] },
61-
{ field1: 'Clothing', All: 1526, Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
62-
{ field1: 'Bikes', All: 68, Uruguay: 68, level: 1 },
63-
{ field1: 'Accessories', All: 293, USA: 293, level: 1 },
64-
{ field1: 'Components', All: 240, USA: 240, level: 1 }
55+
{
56+
field1: 'All', All: 2127, Bulgaria: 774, USA: 829, Uruguay: 524, level: 0, records: [
57+
{ field1: 'Clothing', All: 1526, Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
58+
{ field1: 'Bikes', All: 68, Uruguay: 68, level: 1 },
59+
{ field1: 'Accessories', All: 293, USA: 293, level: 1 },
60+
{ field1: 'Components', All: 240, USA: 240, level: 1 }
61+
]
62+
},
63+
{ field1: 'Clothing', All: 1526, Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
64+
{ field1: 'Bikes', All: 68, Uruguay: 68, level: 1 },
65+
{ field1: 'Accessories', All: 293, USA: 293, level: 1 },
66+
{ field1: 'Components', All: 240, USA: 240, level: 1 }
67+
]);
68+
});
69+
70+
it('transforms flat data to pivot data single row dimension and no children are defined', () => {
71+
const rowPipeResult = rowPipe.transform(data, [{
72+
member: 'ProductCategory',
73+
enabled: true,
74+
childLevels: []
75+
}], pivotConfigHierarchy.values);
76+
expect(rowPipeResult).toEqual([
77+
{
78+
ProductCategory: 'Clothing', level: 0, records: [
79+
{ ProductCategory: 'Clothing', UnitPrice: 12.81, SellerName: 'Stanley', Country: 'Bulgaria', Date: '01/01/2021', UnitsSold: 282 },
80+
{ ProductCategory: 'Clothing', UnitPrice: 49.57, SellerName: 'Elisa', Country: 'USA', Date: '01/05/2019', UnitsSold: 296 },
81+
{ ProductCategory: 'Clothing', UnitPrice: 68.33, SellerName: 'Larry', Country: 'Uruguay', Date: '05/12/2020', UnitsSold: 456 },
82+
{ ProductCategory: 'Clothing', UnitPrice: 16.05, SellerName: 'Walter', Country: 'Bulgaria', Date: '02/19/2020', UnitsSold: 492 }]
83+
},
84+
{
85+
ProductCategory: 'Bikes', level: 0, records: [
86+
{ ProductCategory: 'Bikes', UnitPrice: 3.56, SellerName: 'Lydia', Country: 'Uruguay', Date: '01/06/2020', UnitsSold: 68 }
87+
]
88+
},
89+
{
90+
ProductCategory: 'Accessories', level: 0, records: [
91+
{ ProductCategory: 'Accessories', UnitPrice: 85.58, SellerName: 'David', Country: 'USA', Date: '04/07/2021', UnitsSold: 293 }
92+
]
93+
},
94+
{
95+
ProductCategory: 'Components', level: 0, records: [
96+
{ ProductCategory: 'Components', UnitPrice: 18.13, SellerName: 'John', Country: 'USA', Date: '12/08/2021', UnitsSold: 240 }
97+
]
98+
}
99+
]);
100+
const columnPipeResult = columnPipe.transform(rowPipeResult, pivotConfigHierarchy.columns, pivotConfigHierarchy.values);
101+
expect(columnPipeResult).toEqual([
102+
{ ProductCategory: 'Clothing', All: 1526, Bulgaria: 774, USA: 296, Uruguay: 456, level: 0 },
103+
{ ProductCategory: 'Bikes', All: 68, Uruguay: 68, level: 0 },
104+
{ ProductCategory: 'Accessories', All: 293, USA: 293, level: 0 },
105+
{ ProductCategory: 'Components', All: 240, USA: 240, level: 0 }
106+
]);
107+
});
108+
109+
it('transforms flat data to pivot data multiple row dimensions', () => {
110+
const rowPipeResult = rowPipe.transform(data, [{
111+
member: 'ProductCategory',
112+
enabled: true,
113+
childLevels: []
114+
},
115+
{
116+
member: 'Date',
117+
enabled: true,
118+
childLevels: []
119+
}], pivotConfigHierarchy.values);
120+
121+
expect(rowPipeResult).toEqual([
122+
{
123+
ProductCategory: 'Clothing', level: 0, children: [
124+
{ Date: '01/01/2021', records: [
125+
{ ProductCategory: 'Clothing', UnitPrice: 12.81, SellerName: 'Stanley', Country: 'Bulgaria', Date: '01/01/2021', UnitsSold: 282 }
126+
]},
127+
{ Date: '01/05/2019', records: [
128+
{ ProductCategory: 'Clothing', UnitPrice: 49.57, SellerName: 'Elisa', Country: 'USA', Date: '01/05/2019', UnitsSold: 296 }
129+
]},
130+
{ Date: '05/12/2020', records: [
131+
{ ProductCategory: 'Clothing', UnitPrice: 68.33, SellerName: 'Larry', Country: 'Uruguay', Date: '05/12/2020', UnitsSold: 456 }
132+
]},
133+
{ Date: '02/19/2020', records: [
134+
{ ProductCategory: 'Clothing', UnitPrice: 16.05, SellerName: 'Walter', Country: 'Bulgaria', Date: '02/19/2020', UnitsSold: 492 }
135+
]},
136+
], records: [
137+
{ ProductCategory: 'Clothing', UnitPrice: 12.81, SellerName: 'Stanley', Country: 'Bulgaria', Date: '01/01/2021', UnitsSold: 282 },
138+
{ ProductCategory: 'Clothing', UnitPrice: 49.57, SellerName: 'Elisa', Country: 'USA', Date: '01/05/2019', UnitsSold: 296 },
139+
{ ProductCategory: 'Clothing', UnitPrice: 68.33, SellerName: 'Larry', Country: 'Uruguay', Date: '05/12/2020', UnitsSold: 456 },
140+
{ ProductCategory: 'Clothing', UnitPrice: 16.05, SellerName: 'Walter', Country: 'Bulgaria', Date: '02/19/2020', UnitsSold: 492 }]
141+
},
142+
{
143+
ProductCategory: 'Bikes', level: 0, children: [
144+
{Date: '01/06/2020', records: [
145+
{ ProductCategory: 'Bikes', UnitPrice: 3.56, SellerName: 'Lydia', Country: 'Uruguay', Date: '01/06/2020', UnitsSold: 68 }
146+
]}
147+
], records: [
148+
{ ProductCategory: 'Bikes', UnitPrice: 3.56, SellerName: 'Lydia', Country: 'Uruguay', Date: '01/06/2020', UnitsSold: 68 }
149+
]
150+
},
151+
{
152+
ProductCategory: 'Accessories', level: 0, children: [
153+
{ Date: '04/07/2021', records: [
154+
{ ProductCategory: 'Accessories', UnitPrice: 85.58, SellerName: 'David', Country: 'USA', Date: '04/07/2021', UnitsSold: 293 }
155+
]}
156+
], records: [
157+
{ ProductCategory: 'Accessories', UnitPrice: 85.58, SellerName: 'David', Country: 'USA', Date: '04/07/2021', UnitsSold: 293 }
158+
]
159+
},
160+
{
161+
ProductCategory: 'Components', level: 0, children: [
162+
{ Date: '12/08/2021', records: [
163+
{ ProductCategory: 'Components', UnitPrice: 18.13, SellerName: 'John', Country: 'USA', Date: '12/08/2021', UnitsSold: 240 }
164+
]}
165+
], records: [
166+
{ ProductCategory: 'Components', UnitPrice: 18.13, SellerName: 'John', Country: 'USA', Date: '12/08/2021', UnitsSold: 240 }
167+
]
168+
}
65169
]);
66170
});
67171

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-util.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class PivotUtil {
88
for (const rec of data) {
99
const vals = this.extractValuesFromDimension(columns, rec);
1010
for (const val of vals) { // this should go in depth also vals.children
11-
if (hierarchy.get(val.value) != null && val.children) {
11+
if (hierarchy.get(val.value) != null) {
1212
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
1313
} else {
1414
hierarchy.set(val.value, cloneValue(val));
@@ -24,7 +24,7 @@ export class PivotUtil {
2424
return typeof dim.member === 'string' ? recData[dim.member] : dim.member.call(null, recData);
2525
}
2626

27-
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any){
27+
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any) {
2828
const vals = [];
2929
let i = 0;
3030
for (const col of dims) {
@@ -65,14 +65,14 @@ export class PivotUtil {
6565
const flatData = [];
6666
for (const dim of dims) {
6767
hierarchies.forEach((h, key) => {
68-
const field = (dim && dim.fieldName) ?? this.generateFieldValue(rec);
68+
const field = this.resolveFieldName(dim, rec);
6969
let obj = {};
7070
obj[field] = key;
7171
obj[pivotKeys.records] = h[pivotKeys.records];
72-
obj = {...obj, ...h[pivotKeys.aggregations]};
72+
obj = { ...obj, ...h[pivotKeys.aggregations] };
7373
obj[pivotKeys.level] = level;
7474
flatData.push(obj);
75-
if (h[pivotKeys.children]) {
75+
if (h[pivotKeys.children] && h[pivotKeys.children].size > 0) {
7676
obj[pivotKeys.records] = this.flattenHierarchy(h[pivotKeys.children], rec, dim.childLevels, pivotKeys, level + 1);
7777
for (const record of obj[pivotKeys.records]) {
7878
flatData.push(record);
@@ -104,9 +104,17 @@ export class PivotUtil {
104104
return flatData;
105105
}
106106

107+
private static resolveFieldName(dimension, record) {
108+
if (typeof dimension.member === 'string') {
109+
return dimension.member;
110+
} else {
111+
return (dimension && dimension.fieldName) ?? this.generateFieldValue(record);
112+
}
113+
}
114+
107115
private static generateFieldValue(rec) {
108116
let i = 0;
109-
while (Object.keys(rec).indexOf('field' + ++i) !== -1) {}
117+
while (Object.keys(rec).indexOf('field' + ++i) !== -1) { }
110118
return 'field' + i;
111119
}
112120

@@ -118,15 +126,23 @@ export class PivotUtil {
118126
}
119127

120128
private static applyHierarchyChildren(hierarchy, val, rec, recordsKey) {
121-
for (const child of val.children) {
122-
if (!hierarchy.get(val.value).children.get(child.value)) {
123-
hierarchy.get(val.value).children.set(child.value, child);
129+
if (!val.children) {
130+
if (hierarchy.get(val.value)[recordsKey]) {
131+
hierarchy.get(val.value)[recordsKey].push(rec);
132+
} else {
133+
hierarchy.get(val.value)[recordsKey] = [rec];
124134
}
135+
} else {
136+
for (const child of val.children) {
137+
if (!hierarchy.get(val.value).children.get(child.value)) {
138+
hierarchy.get(val.value).children.set(child.value, child);
139+
}
125140

126-
if (hierarchy.get(val.value).children.get(child.value)[recordsKey]) {
127-
hierarchy.get(val.value).children.get(child.value)[recordsKey].push(rec);
128-
} else {
129-
hierarchy.get(val.value).children.get(child.value)[recordsKey] = [rec];
141+
if (hierarchy.get(val.value).children.get(child.value)[recordsKey]) {
142+
hierarchy.get(val.value).children.get(child.value)[recordsKey].push(rec);
143+
} else {
144+
hierarchy.get(val.value).children.get(child.value)[recordsKey] = [rec];
145+
}
130146
}
131147
}
132148
}

0 commit comments

Comments
 (0)