Skip to content

Commit

Permalink
Populate empty notebook with trusted cell, test trust serialization (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski authored Feb 24, 2023
1 parent 772684e commit d133062
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/notebook/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ close the notebook without saving it.`,

// Ensure there is at least one cell
if ((copy.cells?.length ?? 0) === 0) {
copy['cells'] = [{ cell_type: 'code', source: '', metadata: {} }];
copy['cells'] = [
{ cell_type: 'code', source: '', metadata: { trusted: true } }
];
}
this.sharedModel.fromJSON(copy);

Expand Down
25 changes: 25 additions & 0 deletions packages/notebook/test/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cells": [],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
24 changes: 24 additions & 0 deletions packages/notebook/test/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ describe('@jupyterlab/notebook', () => {
expect(data.cells.length).toBe(7);
expect(data.cells[0].id).toBe('cell_1');
});
it('should only include `trusted` metadata in code cells', () => {
const model = new NotebookModel();
model.fromJSON(utils.DEFAULT_CONTENT_45);

[...model.cells].map(cell => (cell.trusted = true));
expect(model.cells.get(0).type).toBe('code');
expect(model.cells.get(1).type).toBe('markdown');
expect(model.cells.get(2).type).toBe('raw');

const data = model.toJSON();
// code cell trust should be preserved
expect(data.cells[0].metadata.trusted).toBe(true);
// markdown cell should have no trusted entry
expect(data.cells[1].metadata.trusted).toBeUndefined();
// raw cell should have no trusted entry
expect(data.cells[2].metadata.trusted).toBeUndefined();
});
});

describe('#fromJSON()', () => {
Expand Down Expand Up @@ -302,6 +319,13 @@ describe('@jupyterlab/notebook', () => {
model.fromJSON(utils.DEFAULT_CONTENT);
expect(model.dirty).toBe(true);
});

it('should populate empty notebook with empty trusted code cell', () => {
const model = new NotebookModel();
model.fromJSON(utils.EMPTY_CONTENT);
const cell = model.cells.get(0);
expect(cell.trusted).toBe(true);
});
});

describe('#metadata', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/notebook/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
} from '@jupyterlab/notebook';
import { NBTestUtils } from '@jupyterlab/notebook/lib/testutils';
import * as defaultContent45 from './default-45.json';
import * as emptyContent from './empty.json';

export { DEFAULT_CONTENT } from '@jupyterlab/notebook/lib/testutils';
export const DEFAULT_CONTENT_45: INotebookContent = defaultContent45;
export const EMPTY_CONTENT: INotebookContent = emptyContent;

/**
* Local versions of the NBTestUtils that import from `src` instead of `lib`.
Expand Down

0 comments on commit d133062

Please sign in to comment.