| 1 |
import { |
| 2 |
IClassicComm, |
| 3 |
ICallbacks, |
| 4 |
WidgetView, |
| 5 |
WidgetModel, |
| 6 |
} from '@jupyter-widgets/base'; |
| 7 |
|
| 8 |
import { JSONObject } from '@lumino/coreutils'; |
| 9 |
import { DataGridModel } from '../../js/datagrid'; |
| 10 |
import { DataSource } from '../../js/datasource'; |
| 11 |
|
| 12 |
/** |
| 13 |
* A namespace for functions that generate tables for testing. |
| 14 |
* |
| 15 |
* #### Notes |
| 16 |
* This is based on the JSON Table Schema specification: |
| 17 |
* https://specs.frictionlessdata.io/table-schema/ |
| 18 |
*/ |
| 19 |
export namespace DataGenerator { |
| 20 |
// Unique primary key identifier |
| 21 |
export const IPYDG_UUID = 'ipydguuid'; |
| 22 |
|
| 23 |
/** |
| 24 |
* A function that returns a table with a single column. |
| 25 |
* |
| 26 |
* @param options - The options for creating a table. |
| 27 |
*/ |
| 28 |
export function singleCol( |
| 29 |
options: ISingleColOptions, |
| 30 |
): DataGridModel.IData { |
| 31 |
const schema = { |
| 32 |
fields: [ |
| 33 |
{ name: 'index', type: 'integer', rows: ['index'] }, |
| 34 |
{ name: options.name, type: options.type, rows: [options.name] }, |
| 35 |
{ name: IPYDG_UUID, type: 'integer', rows: [IPYDG_UUID] }, |
| 36 |
], |
| 37 |
primaryKey: ['index', IPYDG_UUID], |
| 38 |
primaryKeyUuid: IPYDG_UUID, |
| 39 |
}; |
| 40 |
|
| 41 |
const data: {[key: string]: any} = {}; |
| 42 |
data["index"] = [...Array(options.data.length).keys()]; |
| 43 |
data[options.name] = options.data; |
| 44 |
data[IPYDG_UUID] = [...Array(options.data.length).keys()]; |
| 45 |
|
| 46 |
return { |
| 47 |
fields: [], |
| 48 |
schema: schema, |
| 49 |
data: new DataSource(data, [], schema), |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
export function multiCol( |
| 54 |
options: IMultiColOptions, |
| 55 |
): DataGridModel.IData { |
| 56 |
const schema = { |
| 57 |
fields: [ |
| 58 |
...options.data.map((val) => { |
| 59 |
return { name: val.name, type: val.type, rows: [val.name] }; |
| 60 |
}), |
| 61 |
{ name: IPYDG_UUID, type: 'integer', rows: [IPYDG_UUID] }, |
| 62 |
], |
| 63 |
primaryKey: ['index', IPYDG_UUID], |
| 64 |
primaryKeyUuid: IPYDG_UUID, |
| 65 |
}; |
| 66 |
|
| 67 |
const data: {[key: string]: any} = {}; |
| 68 |
options.data.forEach((col) => { |
| 69 |
data[col.name] = col.data; |
| 70 |
}); |
| 71 |
data[IPYDG_UUID] = [...Array(options.length).keys()]; |
| 72 |
|
| 73 |
return { |
| 74 |
fields: [], |
| 75 |
schema: schema, |
| 76 |
data: new DataSource(data, [], schema), |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* A function that returns a table with multiple indices and multiple columns |
| 82 |
* |
| 83 |
* @param options - The options for creating a table. |
| 84 |
*/ |
| 85 |
export function multiIndexCol( |
| 86 |
options: IMultiIndexColOptions, |
| 87 |
primaryKeyUuid: string, |
| 88 |
): DataGridModel.IData { |
| 89 |
const schema = { |
| 90 |
fields: options.data.map((val) => { |
| 91 |
return { |
| 92 |
name: val.name, |
| 93 |
type: val.type, |
| 94 |
rows: |
| 95 |
typeof val.name === 'number' |
| 96 |
? [val.name] |
| 97 |
: val.name.replace(/[^\w\s]/gi, '').split(' '), |
| 98 |
}; |
| 99 |
}), |
| 100 |
primaryKey: options.primaryKeyData, |
| 101 |
primaryKeyUuid: primaryKeyUuid, |
| 102 |
}; |
| 103 |
|
| 104 |
const data: {[key: string]: any} = {}; |
| 105 |
options.data.forEach((col) => { |
| 106 |
data[col.name] = col.data; |
| 107 |
}); |
| 108 |
data[IPYDG_UUID] = [...Array(options.length).keys()]; |
| 109 |
|
| 110 |
return { |
| 111 |
fields: [], |
| 112 |
schema: schema, |
| 113 |
data: new DataSource(data, [], schema), |
| 114 |
}; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* An options object for initializing single column table for testing. |
| 119 |
*/ |
| 120 |
export interface ISingleColOptions { |
| 121 |
/** |
| 122 |
* The name of the column to be created. |
| 123 |
*/ |
| 124 |
name: string; |
| 125 |
|
| 126 |
/** |
| 127 |
* The dtype of the column to be created. |
| 128 |
*/ |
| 129 |
type: string; |
| 130 |
|
| 131 |
/** |
| 132 |
* The data to be added to the created column. |
| 133 |
*/ |
| 134 |
data: any[]; |
| 135 |
} |
| 136 |
|
| 137 |
export interface IMultiColOptions { |
| 138 |
data: DataGenerator.ISingleColOptions[]; |
| 139 |
length: number; |
| 140 |
} |
| 141 |
|
| 142 |
export interface IMultiIndexColOptions { |
| 143 |
data: DataGenerator.ISingleColOptions[]; |
| 144 |
length: number; |
| 145 |
primaryKeyData: any[]; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Mock widget manager for testing. |
| 151 |
*/ |
| 152 |
export class MockWidgetManager { |
| 153 |
create_view(model: any) { |
| 154 |
return new Promise((resolve, reject) => { |
| 155 |
resolve(new MockView()); |
| 156 |
}); |
| 157 |
} |
| 158 |
display_view(model: any) { |
| 159 |
return new Promise((resolve, reject) => { |
| 160 |
resolve(jest.fn()); |
| 161 |
}); |
| 162 |
} |
| 163 |
|
| 164 |
callbacks(view?: WidgetView): ICallbacks { |
| 165 |
return {}; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
class MockView { |
| 170 |
on() { } |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Mock Comm for testing. |
| 175 |
*/ |
| 176 |
export class MockComm implements IClassicComm { |
| 177 |
constructor() { |
| 178 |
this.comm_id = `mock-comm-id-${MockComm.numComms}`; |
| 179 |
MockComm.numComms += 1; |
| 180 |
} |
| 181 |
on_open(fn: Function) { |
| 182 |
this._on_open = fn; |
| 183 |
} |
| 184 |
on_close(fn: Function) { |
| 185 |
this._on_close = fn; |
| 186 |
} |
| 187 |
on_msg(fn: Function) { |
| 188 |
this._on_msg = fn; |
| 189 |
} |
| 190 |
_process_msg(msg: any) { |
| 191 |
if (this._on_msg) { |
| 192 |
return this._on_msg(msg); |
| 193 |
} else { |
| 194 |
return Promise.resolve(); |
| 195 |
} |
| 196 |
} |
| 197 |
open() { |
| 198 |
if (this._on_open) { |
| 199 |
this._on_open(); |
| 200 |
} |
| 201 |
return ''; |
| 202 |
} |
| 203 |
close() { |
| 204 |
if (this._on_close) { |
| 205 |
this._on_close(); |
| 206 |
} |
| 207 |
return ''; |
| 208 |
} |
| 209 |
send() { |
| 210 |
return ''; |
| 211 |
} |
| 212 |
comm_id: string; |
| 213 |
target_name: string; |
| 214 |
_on_msg: Function; |
| 215 |
_on_open: Function; |
| 216 |
_on_close: Function; |
| 217 |
|
| 218 |
private static numComms: number = 0; |
| 219 |
} |
| 220 |
|
| 221 |
export function emulateCustomCommMessage( |
| 222 |
model: WidgetModel, |
| 223 |
channel: 'iopub' | 'shell', |
| 224 |
content: JSONObject, |
| 225 |
) { |
| 226 |
// @ts-ignore |
| 227 |
model._handle_comm_msg({ |
| 228 |
channel: channel, |
| 229 |
content: { |
| 230 |
comm_id: '', |
| 231 |
data: { |
| 232 |
method: 'custom', |
| 233 |
content: content, |
| 234 |
}, |
| 235 |
}, |
| 236 |
}); |
| 237 |
} |