Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Jun 8, 2022
1 parent 91e4096 commit b4b373b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import interact\n",
"import pandas as pd\n",
"import plotly.express as px\n",
"pd.options.plotting.backend = \"plotly\"\n",
"# import pandas as pd\n",
"# import plotly.express as px\n",
"# pd.options.plotting.backend = \"plotly\"\n",
"\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import ipywidgets as widgets\n",
Expand Down Expand Up @@ -49,9 +49,11 @@
" display(out2)\n",
" display(caption)\n",
" display(textbox)\n",
" df = pd.DataFrame({\"x\": list(range(10)), \"y\": list(range(10))})\n",
" fig = df.plot.scatter(x=\"x\", y=\"y\")\n",
" fig.show()\n",
" # Output a custom mime type instead of a plotly plot so that we don't have to install plotly on CI.\n",
" display({\"application/vnd.custom\": f\"Custom Mime Output under the `output widget`\"}, raw=True)\n",
" # df = pd.DataFrame({\"x\": list(range(10)), \"y\": list(range(10))})\n",
" # fig = df.plot.scatter(x=\"x\", y=\"y\")\n",
" # fig.show()\n",
"\n",
"\n",
"function_with_captured_output()"
Expand Down
85 changes: 41 additions & 44 deletions src/test/datascience/widgets/standard.vscode.common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ suite('IPyWisdget Tests', function () {
await executeCellAndWaitForOutput(cell, comms);
await assertOutputContainsHtml(cell, comms, ['66'], '.widget-readout');
});
test.only('Nested Output Widgets', async () => {
test('Nested Output Widgets', async () => {
const comms = await initializeNotebook({ templateFile: 'nested_output_widget.ipynb' });
const [cell1, cell2, cell3, cell4] = vscodeNotebook.activeNotebookEditor!.notebook.getCells();
await executeCellAndWaitForOutput(cell1, comms);
Expand All @@ -423,7 +423,7 @@ suite('IPyWisdget Tests', function () {
await assertOutputContainsHtml(cell1, comms, ['>Widgets are linked an get updated<'], '.widget-output');
assert.strictEqual(cell3.outputs.length, 0, 'Cell 3 should not have any output');
});
test.only('Interactive Button', async () => {
test('Interactive Button', async () => {
const comms = await initializeNotebook({ templateFile: 'interactive_button.ipynb' });
const cell = vscodeNotebook.activeNotebookEditor!.notebook.cellAt(0);

Expand All @@ -432,58 +432,56 @@ suite('IPyWisdget Tests', function () {

// Click the button and verify we have output in other cells
await click(comms, cell, 'button');
await assertOutputContainsHtml(cell, comms, ['Button clicked']);
await waitForCondition(
() => {
assert.strictEqual(getTextOutputValue(cell.outputs[1]).trim(), 'Button clicked');
return true;
},
5_000,
`Expected 'Button clicked' to exist in ${getTextOutputValue(cell.outputs[1])}`
);
});
test.only('Interactive Function', async () => {
test('Interactive Function', async () => {
const comms = await initializeNotebook({ templateFile: 'interactive_function.ipynb' });
const cell = vscodeNotebook.activeNotebookEditor!.notebook.cellAt(0);

await executeCellAndWaitForOutput(cell, comms);
await assertOutputContainsHtml(
cell,
comms,
[
'<input type="text',
">Executing do_something with 'Foo'<",
">'Foo'<",
">Executing do_something with 'Hello World'<",
">'Hello World'<"
],
'.widget-output'
);
await assertOutputContainsHtml(cell, comms, [
'<input type="text',
">Executing do_something with 'Foo'",
">'Foo'"
]);
await waitForCondition(() => cell.outputs.length >= 3, 5_000, 'Cell must have 3 outputs');
assert.strictEqual(getTextOutputValue(cell.outputs[1]).trim(), `Executing do_something with 'Hello World'`);
assert.strictEqual(getTextOutputValue(cell.outputs[2]).trim(), `'Hello World'`);

// Update the textbox and confirm the output is updated accordingly.
await comms.setValue(cell, '.widget-text input', 'Bar');
await assertOutputContainsHtml(
cell,
comms,
[
">Executing do_something with 'Bar'<",
">'Bar'<",
">Executing do_something with 'Hello World'<",
">'Hello World'<"
],
'.widget-output'
);
await assertOutputContainsHtml(cell, comms, [
'<input type="text',
">Executing do_something with 'Bar'",
">'Bar'"
]);
assert.strictEqual(getTextOutputValue(cell.outputs[1]).trim(), `Executing do_something with 'Hello World'`);
assert.strictEqual(getTextOutputValue(cell.outputs[2]).trim(), `'Hello World'`);
});
test.only('Interactive Plot', async () => {
test('Interactive Plot', async () => {
const comms = await initializeNotebook({ templateFile: 'interactive_plot.ipynb' });
const cell = vscodeNotebook.activeNotebookEditor!.notebook.cellAt(0);

await executeCellAndWaitForOutput(cell, comms);
await assertOutputContainsHtml(cell, comms, ['Text Value is Foo'], '.widget-output');
await assertOutputContainsHtml(cell, comms, ['Text Value is Foo']);
assert.strictEqual(cell.outputs.length, 4, 'Cell should have 4 outputs');

const [, output2, output3, output4] = cell.outputs;
// This cannot be displayed by output widget, hence we need to handle this.
assert.strictEqual(output2.items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(output2.items[0].data).toString(), 'Text Value is Foo');
assert.strictEqual(cell.outputs[1].items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(cell.outputs[1].items[0].data).toString(), 'Text Value is Foo');

assert.strictEqual(getTextOutputValue(output3), 'Text Value is Foo');
assert.strictEqual(getTextOutputValue(cell.outputs[2]).trim(), 'Text Value is Hello World');

// This cannot be displayed by output widget, hence we need to handle this.
assert.strictEqual(output4.items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(output4.items[0].data).toString(), 'Text Value is Foo');
assert.strictEqual(cell.outputs[3].items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(cell.outputs[3].items[0].data).toString().trim(), 'Text Value is Hello World');

// Wait for the second output to get updated.
const outputUpdated = new Promise<boolean>((resolve) => {
Expand All @@ -508,15 +506,14 @@ suite('IPyWisdget Tests', function () {
// Wait for the output to get updated.
await waitForCondition(() => outputUpdated, 5_000, 'Second output not updated');

// This should have been updated.
assert.strictEqual(output2.items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(output2.items[0].data).toString(), 'Text Value is Bar');

// This should not have been updated.
assert.strictEqual(getTextOutputValue(output3), 'Text Value is Foo');
// The first & second outputs should have been updated
await assertOutputContainsHtml(cell, comms, ['Text Value is Bar']);
assert.strictEqual(cell.outputs[1].items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(cell.outputs[1].items[0].data).toString().trim(), 'Text Value is Bar');

// This should not have been updated.
assert.strictEqual(output4.items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(output4.items[0].data).toString(), 'Text Value is Foo');
// The last two should not have changed.
assert.strictEqual(getTextOutputValue(cell.outputs[2]).trim(), 'Text Value is Hello World');
assert.strictEqual(cell.outputs[3].items[0].mime, 'application/vnd.custom');
assert.strictEqual(Buffer.from(cell.outputs[3].items[0].data).toString().trim(), 'Text Value is Hello World');
});
});

0 comments on commit b4b373b

Please sign in to comment.