22// Licensed under the MIT License.
33'use strict' ;
44
5+ import { nbformat } from '@jupyterlab/coreutils' ;
56import { assert } from 'chai' ;
67import * as sinon from 'sinon' ;
78import { anything , instance , mock , verify , when } from 'ts-mockito' ;
@@ -13,12 +14,13 @@ import { PythonSettings } from '../../client/common/configSettings';
1314import { ConfigurationService } from '../../client/common/configuration/service' ;
1415import { IConfigurationService , IPythonSettings } from '../../client/common/types' ;
1516import { CommandRegistry } from '../../client/datascience/commands/commandRegistry' ;
17+ import { pruneCell } from '../../client/datascience/common' ;
1618import { DataScience } from '../../client/datascience/datascience' ;
1719import { DataScienceCodeLensProvider } from '../../client/datascience/editor-integration/codelensprovider' ;
1820import { IDataScienceCodeLensProvider } from '../../client/datascience/types' ;
1921
2022// tslint:disable: max-func-body-length
21- suite ( 'Data Science Tests' , ( ) => {
23+ suite ( 'DataScience Tests' , ( ) => {
2224 let dataScience : DataScience ;
2325 let cmdManager : CommandManager ;
2426 let codeLensProvider : IDataScienceCodeLensProvider ;
@@ -75,4 +77,169 @@ suite('Data Science Tests', () => {
7577 assert . ok ( onDidChangeActiveTextEditor . calledOnce ) ;
7678 } ) ;
7779 } ) ;
80+
81+ suite ( 'Cell pruning' , ( ) => {
82+ test ( 'Remove output and execution count from non code' , ( ) => {
83+ const cell : nbformat . ICell = {
84+ cell_type : 'markdown' ,
85+ outputs : [ ] ,
86+ execution_count : '23' ,
87+ source : 'My markdown' ,
88+ metadata : { }
89+ } ;
90+ const result = pruneCell ( cell ) ;
91+ assert . equal ( Object . keys ( result ) . indexOf ( 'outputs' ) , - 1 , 'Outputs inside markdown' ) ;
92+ assert . equal ( Object . keys ( result ) . indexOf ( 'execution_count' ) , - 1 , 'Execution count inside markdown' ) ;
93+ } ) ;
94+ test ( 'Outputs dont contain extra data' , ( ) => {
95+ const cell : nbformat . ICell = {
96+ cell_type : 'code' ,
97+ outputs : [
98+ {
99+ output_type : 'display_data' ,
100+ extra : { }
101+ }
102+ ] ,
103+ execution_count : '23' ,
104+ source : 'My source' ,
105+ metadata : { }
106+ } ;
107+ const result = pruneCell ( cell ) ;
108+ // tslint:disable-next-line: no-any
109+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
110+ assert . equal ( result . execution_count , '23' , 'Output execution count removed' ) ;
111+ const output = ( result . outputs as nbformat . IOutput [ ] ) [ 0 ] ;
112+ assert . equal ( Object . keys ( output ) . indexOf ( 'extra' ) , - 1 , 'Output still has extra data' ) ;
113+ assert . notEqual ( Object . keys ( output ) . indexOf ( 'output_type' ) , - 1 , 'Output is missing output_type' ) ;
114+ } ) ;
115+ test ( 'Display outputs still have their data' , ( ) => {
116+ const cell : nbformat . ICell = {
117+ cell_type : 'code' ,
118+ execution_count : 2 ,
119+ metadata : { } ,
120+ outputs : [
121+ {
122+ output_type : 'display_data' ,
123+ data : {
124+ 'text/plain' : "Box(children=(Label(value='My label'),))" ,
125+ 'application/vnd.jupyter.widget-view+json' : {
126+ version_major : 2 ,
127+ version_minor : 0 ,
128+ model_id : '90c99248d7bb490ca132427de6d1e235'
129+ }
130+ } ,
131+ metadata : { bob : 'youruncle' }
132+ }
133+ ] ,
134+ source : [ "line = widgets.Label('My label')\n" , 'box = widgets.Box([line])\n' , 'box' ]
135+ } ;
136+
137+ const result = pruneCell ( cell ) ;
138+ // tslint:disable-next-line: no-any
139+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
140+ assert . equal ( result . execution_count , 2 , 'Output execution count removed' ) ;
141+ assert . deepEqual ( result . outputs , cell . outputs , 'Outputs were modified' ) ;
142+ } ) ;
143+ test ( 'Stream outputs still have their data' , ( ) => {
144+ const cell : nbformat . ICell = {
145+ cell_type : 'code' ,
146+ execution_count : 2 ,
147+ metadata : { } ,
148+ outputs : [
149+ {
150+ output_type : 'stream' ,
151+ name : 'stdout' ,
152+ text : 'foobar'
153+ }
154+ ] ,
155+ source : [ "line = widgets.Label('My label')\n" , 'box = widgets.Box([line])\n' , 'box' ]
156+ } ;
157+
158+ const result = pruneCell ( cell ) ;
159+ // tslint:disable-next-line: no-any
160+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
161+ assert . equal ( result . execution_count , 2 , 'Output execution count removed' ) ;
162+ assert . deepEqual ( result . outputs , cell . outputs , 'Outputs were modified' ) ;
163+ } ) ;
164+ test ( 'Errors outputs still have their data' , ( ) => {
165+ const cell : nbformat . ICell = {
166+ cell_type : 'code' ,
167+ execution_count : 2 ,
168+ metadata : { } ,
169+ outputs : [
170+ {
171+ output_type : 'error' ,
172+ ename : 'stdout' ,
173+ evalue : 'stdout is a value' ,
174+ traceback : [ 'more' ]
175+ }
176+ ] ,
177+ source : [ "line = widgets.Label('My label')\n" , 'box = widgets.Box([line])\n' , 'box' ]
178+ } ;
179+
180+ const result = pruneCell ( cell ) ;
181+ // tslint:disable-next-line: no-any
182+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
183+ assert . equal ( result . execution_count , 2 , 'Output execution count removed' ) ;
184+ assert . deepEqual ( result . outputs , cell . outputs , 'Outputs were modified' ) ;
185+ } ) ;
186+ test ( 'Execute result outputs still have their data' , ( ) => {
187+ const cell : nbformat . ICell = {
188+ cell_type : 'code' ,
189+ execution_count : 2 ,
190+ metadata : { } ,
191+ outputs : [
192+ {
193+ output_type : 'execute_result' ,
194+ execution_count : '4' ,
195+ data : {
196+ 'text/plain' : "Box(children=(Label(value='My label'),))" ,
197+ 'application/vnd.jupyter.widget-view+json' : {
198+ version_major : 2 ,
199+ version_minor : 0 ,
200+ model_id : '90c99248d7bb490ca132427de6d1e235'
201+ }
202+ } ,
203+ metadata : { foo : 'bar' }
204+ }
205+ ] ,
206+ source : [ "line = widgets.Label('My label')\n" , 'box = widgets.Box([line])\n' , 'box' ]
207+ } ;
208+
209+ const result = pruneCell ( cell ) ;
210+ // tslint:disable-next-line: no-any
211+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
212+ assert . equal ( result . execution_count , 2 , 'Output execution count removed' ) ;
213+ assert . deepEqual ( result . outputs , cell . outputs , 'Outputs were modified' ) ;
214+ } ) ;
215+ test ( 'Unrecognized outputs still have their data' , ( ) => {
216+ const cell : nbformat . ICell = {
217+ cell_type : 'code' ,
218+ execution_count : 2 ,
219+ metadata : { } ,
220+ outputs : [
221+ {
222+ output_type : 'unrecognized' ,
223+ execution_count : '4' ,
224+ data : {
225+ 'text/plain' : "Box(children=(Label(value='My label'),))" ,
226+ 'application/vnd.jupyter.widget-view+json' : {
227+ version_major : 2 ,
228+ version_minor : 0 ,
229+ model_id : '90c99248d7bb490ca132427de6d1e235'
230+ }
231+ } ,
232+ metadata : { }
233+ }
234+ ] ,
235+ source : [ "line = widgets.Label('My label')\n" , 'box = widgets.Box([line])\n' , 'box' ]
236+ } ;
237+
238+ const result = pruneCell ( cell ) ;
239+ // tslint:disable-next-line: no-any
240+ assert . equal ( ( result . outputs as any ) . length , 1 , 'Outputs were removed' ) ;
241+ assert . equal ( result . execution_count , 2 , 'Output execution count removed' ) ;
242+ assert . deepEqual ( result . outputs , cell . outputs , 'Outputs were modified' ) ;
243+ } ) ;
244+ } ) ;
78245} ) ;
0 commit comments