2
2
// Licensed under the MIT License.
3
3
'use strict' ;
4
4
5
+ import { nbformat } from '@jupyterlab/coreutils' ;
5
6
import { assert } from 'chai' ;
6
7
import * as sinon from 'sinon' ;
7
8
import { anything , instance , mock , verify , when } from 'ts-mockito' ;
@@ -13,12 +14,13 @@ import { PythonSettings } from '../../client/common/configSettings';
13
14
import { ConfigurationService } from '../../client/common/configuration/service' ;
14
15
import { IConfigurationService , IPythonSettings } from '../../client/common/types' ;
15
16
import { CommandRegistry } from '../../client/datascience/commands/commandRegistry' ;
17
+ import { pruneCell } from '../../client/datascience/common' ;
16
18
import { DataScience } from '../../client/datascience/datascience' ;
17
19
import { DataScienceCodeLensProvider } from '../../client/datascience/editor-integration/codelensprovider' ;
18
20
import { IDataScienceCodeLensProvider } from '../../client/datascience/types' ;
19
21
20
22
// tslint:disable: max-func-body-length
21
- suite ( 'Data Science Tests' , ( ) => {
23
+ suite ( 'DataScience Tests' , ( ) => {
22
24
let dataScience : DataScience ;
23
25
let cmdManager : CommandManager ;
24
26
let codeLensProvider : IDataScienceCodeLensProvider ;
@@ -75,4 +77,169 @@ suite('Data Science Tests', () => {
75
77
assert . ok ( onDidChangeActiveTextEditor . calledOnce ) ;
76
78
} ) ;
77
79
} ) ;
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
+ } ) ;
78
245
} ) ;
0 commit comments