forked from cyclotruc/gitingest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_notebook_utils.py
294 lines (243 loc) · 10.7 KB
/
test_notebook_utils.py
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
"""
Tests for the `notebook_utils` module.
These tests validate how notebooks are processed into Python-like output, ensuring that markdown/raw cells are
converted to triple-quoted blocks, code cells remain executable code, and various edge cases (multiple worksheets,
empty cells, outputs, etc.) are handled appropriately.
"""
import pytest
from gitingest.utils.notebook_utils import process_notebook
from tests.conftest import WriteNotebookFunc
def test_process_notebook_all_cells(write_notebook: WriteNotebookFunc) -> None:
"""
Test processing a notebook containing markdown, code, and raw cells.
Given a notebook with:
- One markdown cell
- One code cell
- One raw cell
When `process_notebook` is invoked,
Then markdown and raw cells should appear in triple-quoted blocks, and code cells remain as normal code.
"""
notebook_content = {
"cells": [
{"cell_type": "markdown", "source": ["# Markdown cell"]},
{"cell_type": "code", "source": ['print("Hello Code")']},
{"cell_type": "raw", "source": ["<raw content>"]},
]
}
nb_path = write_notebook("all_cells.ipynb", notebook_content)
result = process_notebook(nb_path)
assert result.count('"""') == 4, "Two non-code cells => 2 triple-quoted blocks => 4 total triple quotes."
# Ensure markdown and raw cells are in triple quotes
assert "# Markdown cell" in result
assert "<raw content>" in result
# Ensure code cell is not in triple quotes
assert 'print("Hello Code")' in result
assert '"""\nprint("Hello Code")\n"""' not in result
def test_process_notebook_with_worksheets(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook containing the (as of IPEP-17 deprecated) 'worksheets' key.
Given a notebook that uses the 'worksheets' key with a single worksheet,
When `process_notebook` is called,
Then a `DeprecationWarning` should be raised, and the content should match an equivalent notebook
that has top-level 'cells'.
"""
with_worksheets = {
"worksheets": [
{
"cells": [
{"cell_type": "markdown", "source": ["# Markdown cell"]},
{"cell_type": "code", "source": ['print("Hello Code")']},
{"cell_type": "raw", "source": ["<raw content>"]},
]
}
]
}
without_worksheets = with_worksheets["worksheets"][0] # same, but no 'worksheets' key
nb_with = write_notebook("with_worksheets.ipynb", with_worksheets)
nb_without = write_notebook("without_worksheets.ipynb", without_worksheets)
with pytest.warns(DeprecationWarning, match="Worksheets are deprecated as of IPEP-17."):
result_with = process_notebook(nb_with)
# Should not raise a warning
result_without = process_notebook(nb_without)
assert result_with == result_without, "Content from the single worksheet should match the top-level equivalent."
def test_process_notebook_multiple_worksheets(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook containing multiple 'worksheets'.
Given a notebook with two worksheets:
- First with a markdown cell
- Second with a code cell
When `process_notebook` is called,
Then a warning about multiple worksheets should be raised, and the second worksheet's content should appear
in the final output.
"""
multi_worksheets = {
"worksheets": [
{"cells": [{"cell_type": "markdown", "source": ["# First Worksheet"]}]},
{"cells": [{"cell_type": "code", "source": ["# Second Worksheet"]}]},
]
}
single_worksheet = {
"worksheets": [
{"cells": [{"cell_type": "markdown", "source": ["# First Worksheet"]}]},
]
}
nb_multi = write_notebook("multiple_worksheets.ipynb", multi_worksheets)
nb_single = write_notebook("single_worksheet.ipynb", single_worksheet)
# Expect DeprecationWarning + UserWarning
with pytest.warns(
DeprecationWarning, match="Worksheets are deprecated as of IPEP-17. Consider updating the notebook."
):
with pytest.warns(
UserWarning, match="Multiple worksheets detected. Combining all worksheets into a single script."
):
result_multi = process_notebook(nb_multi)
# Expect DeprecationWarning only
with pytest.warns(
DeprecationWarning, match="Worksheets are deprecated as of IPEP-17. Consider updating the notebook."
):
result_single = process_notebook(nb_single)
assert result_multi != result_single, "Two worksheets should produce more content than one."
assert len(result_multi) > len(result_single), "The multi-worksheet notebook should have extra code content."
assert "# First Worksheet" in result_single
assert "# Second Worksheet" not in result_single
assert "# First Worksheet" in result_multi
assert "# Second Worksheet" in result_multi
def test_process_notebook_code_only(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook containing only code cells.
Given a notebook with code cells only:
When `process_notebook` is called,
Then no triple quotes should appear in the output.
"""
notebook_content = {
"cells": [
{"cell_type": "code", "source": ["print('Code Cell 1')"]},
{"cell_type": "code", "source": ["x = 42"]},
]
}
nb_path = write_notebook("code_only.ipynb", notebook_content)
result = process_notebook(nb_path)
assert '"""' not in result, "No triple quotes expected when there are only code cells."
assert "print('Code Cell 1')" in result
assert "x = 42" in result
def test_process_notebook_markdown_only(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook with only markdown cells.
Given a notebook with two markdown cells:
When `process_notebook` is called,
Then each markdown cell should become a triple-quoted block (2 blocks => 4 triple quotes total).
"""
notebook_content = {
"cells": [
{"cell_type": "markdown", "source": ["# Markdown Header"]},
{"cell_type": "markdown", "source": ["Some more markdown."]},
]
}
nb_path = write_notebook("markdown_only.ipynb", notebook_content)
result = process_notebook(nb_path)
assert result.count('"""') == 4, "Two markdown cells => 2 blocks => 4 triple quotes total."
assert "# Markdown Header" in result
assert "Some more markdown." in result
def test_process_notebook_raw_only(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook with only raw cells.
Given two raw cells:
When `process_notebook` is called,
Then each raw cell should become a triple-quoted block (2 blocks => 4 triple quotes total).
"""
notebook_content = {
"cells": [
{"cell_type": "raw", "source": ["Raw content line 1"]},
{"cell_type": "raw", "source": ["Raw content line 2"]},
]
}
nb_path = write_notebook("raw_only.ipynb", notebook_content)
result = process_notebook(nb_path)
assert result.count('"""') == 4, "Two raw cells => 2 blocks => 4 triple quotes."
assert "Raw content line 1" in result
assert "Raw content line 2" in result
def test_process_notebook_empty_cells(write_notebook: WriteNotebookFunc) -> None:
"""
Test that cells with an empty 'source' are skipped.
Given a notebook with 4 cells, 3 of which have empty `source`:
When `process_notebook` is called,
Then only the non-empty cell should appear in the output (1 block => 2 triple quotes).
"""
notebook_content = {
"cells": [
{"cell_type": "markdown", "source": []},
{"cell_type": "code", "source": []},
{"cell_type": "raw", "source": []},
{"cell_type": "markdown", "source": ["# Non-empty markdown"]},
]
}
nb_path = write_notebook("empty_cells.ipynb", notebook_content)
result = process_notebook(nb_path)
assert result.count('"""') == 2, "Only one non-empty cell => 1 block => 2 triple quotes"
assert "# Non-empty markdown" in result
def test_process_notebook_invalid_cell_type(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook with an unknown cell type.
Given a notebook cell whose `cell_type` is unrecognized:
When `process_notebook` is called,
Then a ValueError should be raised.
"""
notebook_content = {
"cells": [
{"cell_type": "markdown", "source": ["# Valid markdown"]},
{"cell_type": "unknown", "source": ["Unrecognized cell type"]},
]
}
nb_path = write_notebook("invalid_cell_type.ipynb", notebook_content)
with pytest.raises(ValueError, match="Unknown cell type: unknown"):
process_notebook(nb_path)
def test_process_notebook_with_output(write_notebook: WriteNotebookFunc) -> None:
"""
Test a notebook that has code cells with outputs.
Given a code cell and multiple output objects:
When `process_notebook` is called with `include_output=True`,
Then the outputs should be appended as commented lines under the code.
"""
notebook_content = {
"cells": [
{
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"print('my_data')\n",
"my_data = [1, 2, 3, 4, 5]\n",
"plt.plot(my_data)\n",
"my_data",
],
"outputs": [
{"output_type": "stream", "text": ["my_data"]},
{"output_type": "execute_result", "data": {"text/plain": ["[1, 2, 3, 4, 5]"]}},
{"output_type": "display_data", "data": {"text/plain": ["<Figure size 640x480 with 1 Axes>"]}},
],
}
]
}
nb_path = write_notebook("with_output.ipynb", notebook_content)
with_output = process_notebook(nb_path, include_output=True)
without_output = process_notebook(nb_path, include_output=False)
expected_source = "\n".join(
[
"# Jupyter notebook converted to Python script.\n",
"import matplotlib.pyplot as plt",
"print('my_data')",
"my_data = [1, 2, 3, 4, 5]",
"plt.plot(my_data)",
"my_data\n",
]
)
expected_output = "\n".join(
[
"# Output:",
"# my_data",
"# [1, 2, 3, 4, 5]",
"# <Figure size 640x480 with 1 Axes>\n",
]
)
expected_combined = expected_source + expected_output
assert with_output == expected_combined, "Should include source code and comment-ified output."
assert without_output == expected_source, "Should include only the source code without output."