Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from __future__ import division
__copyright__ = "Copyright (C) 2017 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.test import TestCase
from django.test.utils import override_settings
import json
try:
from mock import patch
except ImportError:
from unittest.mock import patch
from .test_sandbox import SingleCoursePageSandboxTestBaseMixin
# {{{ Test Nbconvert for rendering ipynb notebook
QUESTION_MARKUP_FULL = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb") }}\r
"""
QUESTION_MARKUP_SLICED1 = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb", indices=[0, 1, 2]) }}\r
"""
QUESTION_MARKUP_SLICED2 = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb", indices=[1, 2]) }}\r
"""
QUESTION_MARKUP_CLEAR_MARKDOWN = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb", clear_markdown=True) }}\r
"""
QUESTION_MARKUP_CLEAR_OUTPUT = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb", clear_output=True) }}\r
"""
QUESTION_MARKUP_CLEAR_ALL = """
type: Page\r
id: ipynb\r
content: |\r
# Ipython notebook Examples\r
{{ render_notebook_cells("test.ipynb", clear_markdown=True, clear_output=True) }}\r
"""
MARKDOWN_PLACEHOLDER = "wzxhzdk"
TEST_IPYNB_BYTES = json.dumps({
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# First Title of Test NoteBook"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": True
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is function1\n"
]
}
],
"source": [
"def function1():\n",
" print(\"This is function1\")\n",
"\n",
"function1()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Second Title of Test NoteBook"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": True
},
"outputs": [],
"source": [
"def function2():\n",
" print(\"This is function2\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is function2\n"
]
}
],
"source": [
"function2()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}).encode()
FIRST_TITLE_TEXT = "First Title of Test NoteBook"
SECOND_TITLE_TEXT = "Second Title of Test NoteBook"
TEXT_CELL_HTML_CLASS = "text_cell_render"
CODE_CELL_HTML_CLASS = "code_cell"
CODE_CELL_IN_STR_PATTERN = '<div class="prompt input_prompt">In[%s]:</div>'
CODE_CELL_PRINT_STR1 = "This is function1"
CODE_CELL_PRINT_STR2 = "This is function2"
def strip_nbsp(s):
"""Returns the given HTML with (which is introduced in nbconvert)
stripped."""
from django.utils.encoding import force_text
return force_text(s).replace(' ', '').replace(u'\xa0', '')
def get_nb_html_from_response(response):
from django.utils.safestring import mark_safe
return strip_nbsp(mark_safe(response.context["body"]))
class NbconvertRenderTestMixin(SingleCoursePageSandboxTestBaseMixin):
def assertIsValidNbConversion(self, response): # noqa
self.assertNotContains(response, MARKDOWN_PLACEHOLDER)
self.assertNotContains(response, "```")
self.assertNotContains(response, "# First Title of Test NoteBook")
self.assertNotContains(response, "# Second Title of Test NoteBook")
def setUp(self):
super(NbconvertRenderTestMixin, self).setUp()
patcher = patch("course.content.get_repo_blob_data_cached")
self.mock_func = patcher.start()
self.mock_func.return_value = TEST_IPYNB_BYTES
self.addCleanup(patcher.stop)
@override_settings(RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=True,
CACHE_BACKEND='dummy:///')
class NbconvertRenderTestWithoutCodeHilite(NbconvertRenderTestMixin, TestCase):
@classmethod
def setUpTestData(cls): # noqa
super(NbconvertRenderTestWithoutCodeHilite, cls).setUpTestData()
cls.c.force_login(cls.instructor_participation.user)
def test_full_notebook_render(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_FULL)
self.assertIsValidNbConversion(resp)
self.assertContains(resp, TEXT_CELL_HTML_CLASS, count=2)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=3)
self.assertContains(resp, FIRST_TITLE_TEXT, count=1)
self.assertContains(resp, SECOND_TITLE_TEXT, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR1, count=2)
self.assertContains(resp, CODE_CELL_PRINT_STR2, count=2)
nb_html = get_nb_html_from_response(resp)
for i in range(1, 4):
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % i, nb_html)
def test_notebook_sliced1(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_SLICED1)
self.assertIsValidNbConversion(resp)
self.assertContains(resp, TEXT_CELL_HTML_CLASS, count=2)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=1)
self.assertContains(resp, FIRST_TITLE_TEXT, count=1)
self.assertContains(resp, SECOND_TITLE_TEXT, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR1, count=2)
self.assertNotContains(resp, CODE_CELL_PRINT_STR2)
nb_html = get_nb_html_from_response(resp)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 1, nb_html, count=1)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 2, nb_html, count=0)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 3, nb_html, count=0)
def test_notebook_sliced2(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_SLICED2)
self.assertIsValidNbConversion(resp)
self.assertContains(resp, TEXT_CELL_HTML_CLASS, count=1)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=1)
self.assertNotContains(resp, FIRST_TITLE_TEXT)
self.assertContains(resp, SECOND_TITLE_TEXT, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR1, count=2)
self.assertNotContains(resp, CODE_CELL_PRINT_STR2)
self.assertNotContains(resp, "class=\"codehilite\"")
self.assertContains(resp, "print(")
nb_html = get_nb_html_from_response(resp)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 1, nb_html, count=1)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 2, nb_html, count=0)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % 3, nb_html, count=0)
def test_notebook_clear_markdown(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_CLEAR_MARKDOWN)
self.assertIsValidNbConversion(resp)
self.assertNotContains(resp, TEXT_CELL_HTML_CLASS)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=3)
self.assertNotContains(resp, FIRST_TITLE_TEXT)
self.assertNotContains(resp, SECOND_TITLE_TEXT)
nb_html = get_nb_html_from_response(resp)
for i in range(1, 4):
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % i, nb_html, count=1)
def test_notebook_clear_output(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_CLEAR_OUTPUT)
self.assertIsValidNbConversion(resp)
self.assertContains(resp, TEXT_CELL_HTML_CLASS, count=2)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=3)
self.assertContains(resp, FIRST_TITLE_TEXT, count=1)
self.assertContains(resp, SECOND_TITLE_TEXT, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR1, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR2, count=1)
nb_html = get_nb_html_from_response(resp)
for i in range(1, 4):
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % i, nb_html, count=0)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % "", nb_html, count=3)
def test_notebook_clear_markdown_and_output(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_CLEAR_ALL)
self.assertIsValidNbConversion(resp)
self.assertNotContains(resp, TEXT_CELL_HTML_CLASS)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=3)
self.assertNotContains(resp, FIRST_TITLE_TEXT)
self.assertNotContains(resp, SECOND_TITLE_TEXT)
self.assertContains(resp, CODE_CELL_PRINT_STR1, count=1)
self.assertContains(resp, CODE_CELL_PRINT_STR2, count=1)
nb_html = get_nb_html_from_response(resp)
for i in range(1, 4):
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % i, nb_html, count=0)
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % "", nb_html, count=3)
@override_settings(
RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=False,
CACHE_BACKEND='dummy:///')
class NbconvertRenderTestCodeHilite(NbconvertRenderTestMixin, TestCase):
@classmethod
def setUpTestData(cls): # noqa
super(NbconvertRenderTestCodeHilite, cls).setUpTestData()
cls.c.force_login(cls.instructor_participation.user)
def test_notebook_render_with_codehilite_extension(self):
resp = self.get_page_sandbox_preview_response(QUESTION_MARKUP_FULL)
self.assertIsValidNbConversion(resp)
self.assertContains(resp, TEXT_CELL_HTML_CLASS, count=2)
self.assertContains(resp, CODE_CELL_HTML_CLASS, count=3)
self.assertContains(resp, FIRST_TITLE_TEXT, count=1)
self.assertContains(resp, SECOND_TITLE_TEXT, count=1)
self.assertContains(resp, "class=\"codehilite\"", count=3)
self.assertContains(resp,
'<span class="k">print</span>'
'<span class="p">(</span><span class="s2">',
count=2)
self.assertNotContains(resp, 'print(')
nb_html = get_nb_html_from_response(resp)
for i in range(1, 4):
self.assertInHTML(CODE_CELL_IN_STR_PATTERN % i, nb_html, count=1)
# }}}