Skip to content

Commit c77edb7

Browse files
cleanup and functional test
1 parent 557f3e0 commit c77edb7

File tree

2 files changed

+79
-28
lines changed

2 files changed

+79
-28
lines changed

src/datascience-ui/history-react/variableExplorer.tsx

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class VariableExplorer extends React.Component<IVariableExplorerProps, IV
195195
this.props.variableExplorerToggled(!this.state.open);
196196
}
197197

198-
private sortRows = (sortColumn: string | number, sortDirection: string) => {
198+
public sortRows = (sortColumn: string | number, sortDirection: string) => {
199199
this.setState({
200200
sortColumn,
201201
sortDirection,
@@ -204,20 +204,20 @@ export class VariableExplorer extends React.Component<IVariableExplorerProps, IV
204204
}
205205

206206
private getColumnType(key: string | number) : string | undefined {
207-
// Special case our "size" column. It's displayed as a string but
208-
// we will compare it as a number
209-
if (key === 'size') {
210-
return 'number';
211-
}
212-
213207
let column;
214208
if (typeof key === 'string') {
215209
//tslint:disable-next-line:no-any
216210
column = this.state.gridColumns.find(c => c.key === key) as any;
217211
} else {
212+
// This is the index lookup
218213
column = this.state.gridColumns[key];
219214
}
220-
if (column && column.type) {
215+
216+
// Special case our size column, it's displayed as a string
217+
// but we will sort it like a number
218+
if (column && column.key === 'size') {
219+
return 'number';
220+
} else if (column && column.type) {
221221
return column.type;
222222
}
223223
}
@@ -256,27 +256,31 @@ export class VariableExplorer extends React.Component<IVariableExplorerProps, IV
256256
return gridRows.sort(comparer);
257257
}
258258

259+
// Get the numerical comparison value for a column
259260
private getComparisonValue(gridRow: IGridRow, sortColumn: string | number): number {
260-
// We need a special case here for the size column
261-
if (sortColumn === 'size') {
262-
const sizeStr: string = gridRow.size as string;
263-
if (!sizeStr || sizeStr.length === 0) {
264-
return 0;
265-
} else if (sizeStr[0] === '(') {
266-
const commaIndex = sizeStr.indexOf(',');
267-
if (commaIndex > 0) {
268-
return parseInt(sizeStr.substring(1, commaIndex), 10);
269-
}
270-
} else {
271-
// In this case we expect a straight i to a conversion
272-
return parseInt(sizeStr, 10);
273-
}
261+
return (sortColumn === 'size') ? this.sizeColumnComparisonValue(gridRow) : gridRow[sortColumn];
262+
}
263+
264+
// The size column needs special casing
265+
private sizeColumnComparisonValue(gridRow: IGridRow): number {
266+
const sizeStr: string = gridRow.size as string;
267+
268+
if (!sizeStr) {
269+
return -1;
270+
}
271+
272+
let sizeNumber = -1;
273+
const commaIndex = sizeStr.indexOf(',');
274+
// First check the shape case like so (5000,1000) in this case we want the 5000 to compare with
275+
if (sizeStr[0] === '(' && commaIndex > 0) {
276+
sizeNumber = parseInt(sizeStr.substring(1, commaIndex), 10);
274277
} else {
275-
// For none size column just assume a standard numeric column
276-
return gridRow[sortColumn];
278+
// If not in the shape format, assume a to i conversion
279+
sizeNumber = parseInt(sizeStr, 10);
277280
}
278281

279-
return 0;
282+
// If our parse fails we get NaN for any case that like return -1
283+
return isNaN(sizeNumber) ? -1 : sizeNumber;
280284
}
281285

282286
private rowDoubleClick = (_rowIndex: number, row: IGridRow) => {

src/test/datascience/variableexplorer.functional.test.tsx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ myTuple = 1,2,3,4,5,6,7,8,9
220220
{name: 'myDataframe', value: ' 0\n0 0.00000\n1 2.00004\n2 4.00008\n3 6.00012\n4 8.00016\n5 10.00020\n6 12.00024\n7 14.00028\n8 16.00032\n', supportsDataExplorer: true, type: 'DataFrame', size: 54, shape: '', count: 0, truncated: false},
221221
{name: 'myFloat', value: '9999.9999', supportsDataExplorer: false, type: 'float', size: 58, shape: '', count: 0, truncated: false},
222222
{name: 'myInt', value: '99999999', supportsDataExplorer: false, type: 'int', size: 56, shape: '', count: 0, truncated: false},
223+
{name: 'mynpArray', value: `[0.00000000e+00 2.00004000e+00 4.00008000e+00 ... 9.99959999e+04
224+
9.99980000e+04 1.00000000e+05]`, supportsDataExplorer: true, type: 'ndarray', size: 54, shape: '', count: 0, truncated: false},
223225
{name: 'mySeries', value: `0 0.00000
224226
1 2.00004
225227
2 4.00008
@@ -230,9 +232,44 @@ myTuple = 1,2,3,4,5,6,7,8,9
230232
7 14.00028
231233
8 16.00032
232234
9 `, supportsDataExplorer: true, type: 'Series', size: 54, shape: '', count: 0, truncated: false},
233-
{name: 'myTuple', value: '(1, 2, 3, 4, 5, 6, 7, 8, 9)', supportsDataExplorer: false, type: 'tuple', size: 54, shape: '', count: 0, truncated: false},
234-
{name: 'mynpArray', value: `[0.00000000e+00 2.00004000e+00 4.00008000e+00 ... 9.99959999e+04
235-
9.99980000e+04 1.00000000e+05]`, supportsDataExplorer: true, type: 'ndarray', size: 54, shape: '', count: 0, truncated: false}
235+
{name: 'myTuple', value: '(1, 2, 3, 4, 5, 6, 7, 8, 9)', supportsDataExplorer: false, type: 'tuple', size: 54, shape: '', count: 0, truncated: false}
236+
];
237+
verifyVariables(wrapper, targetVariables);
238+
}, () => { return ioc; });
239+
240+
runMountedTest('Variable explorer - Sorting', async (wrapper) => {
241+
const basicCode: string = `b = 2
242+
c = 3
243+
stra = 'a'
244+
strb = 'b'
245+
strc = 'c'`;
246+
247+
openVariableExplorer(wrapper);
248+
249+
await addCode(getOrCreateHistory, wrapper, 'a=1\na');
250+
await addCode(getOrCreateHistory, wrapper, basicCode, 4);
251+
252+
await waitForUpdate(wrapper, VariableExplorer, 7);
253+
254+
let targetVariables: IJupyterVariable[] = [
255+
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
256+
{name: 'b', value: '2', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
257+
{name: 'c', value: '3', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
258+
{name: 'stra', value: 'a', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
259+
{name: 'strb', value: 'b', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
260+
{name: 'strc', value: 'c', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
261+
];
262+
verifyVariables(wrapper, targetVariables);
263+
264+
sortVariableExplorer(wrapper, 'value', 'DESC');
265+
266+
targetVariables = [
267+
{name: 'strc', value: 'c', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
268+
{name: 'strb', value: 'b', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
269+
{name: 'stra', value: 'a', supportsDataExplorer: false, type: 'str', size: 54, shape: '', count: 0, truncated: false},
270+
{name: 'c', value: '3', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
271+
{name: 'b', value: '2', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
272+
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
236273
];
237274
verifyVariables(wrapper, targetVariables);
238275
}, () => { return ioc; });
@@ -249,6 +286,16 @@ function openVariableExplorer(wrapper: ReactWrapper<any, Readonly<{}>, React.Com
249286
}
250287
}
251288

289+
function sortVariableExplorer(wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, sortColumn: string, sortDirection: string) {
290+
const varExp: VariableExplorer = wrapper.find('VariableExplorer').instance() as VariableExplorer;
291+
292+
assert(varExp);
293+
294+
if (varExp) {
295+
varExp.sortRows(sortColumn, sortDirection);
296+
}
297+
}
298+
252299
// Verify a set of rows versus a set of expected variables
253300
function verifyVariables(wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, targetVariables: IJupyterVariable[]) {
254301
const foundRows = wrapper.find('div.react-grid-Row');

0 commit comments

Comments
 (0)