Skip to content

Commit 3f5dff4

Browse files
committed
TST: use IPython testing module to execute cells
1 parent 01cd66d commit 3f5dff4

File tree

4 files changed

+123
-229
lines changed

4 files changed

+123
-229
lines changed

dev-requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
ipython>=6.2.1
2-
nbconvert==5.3.1
3-
nbformat==4.4.0
4-
nbval==0.9.0
52
pytest==3.5.0
63
tox==3.0.0

tests/fixtures.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from IPython.testing.globalipapp import start_ipython
2+
import pytest
3+
4+
5+
@pytest.fixture(scope='session')
6+
def global_ip():
7+
return start_ipython()
8+
9+
10+
@pytest.fixture(scope='function')
11+
def ip(global_ip):
12+
global_ip.run_line_magic(magic_name='reset', line='-f')
13+
global_ip.run_line_magic(magic_name='load_ext', line='jupyter_spaces')
14+
yield global_ip
15+
global_ip.run_line_magic(magic_name='unload_ext', line='jupyter_spaces')

tests/test_magics.py

Lines changed: 108 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,108 @@
1-
from nbconvert.preprocessors import ExecutePreprocessor
2-
3-
from tests.utils import NotebookBuilder
4-
5-
6-
notebook_builder = NotebookBuilder(nbformat_major=4, nbformat_minor=2)
7-
notebook_processor = ExecutePreprocessor(timeout=20)
8-
notebook_resources = {}
9-
10-
11-
def test_space_can_access_user_namespace_references():
12-
cells = [
13-
['%load_ext jupyter_spaces'],
14-
['reference = 100'],
15-
['%%space tomato',
16-
'assert reference == 100']
17-
]
18-
notebook = notebook_builder.build(cells=cells)
19-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
20-
21-
22-
def test_space_references_prioritised_over_user_namespace_references():
23-
cells = [
24-
['%load_ext jupyter_spaces'],
25-
['reference = 100'],
26-
['%%space tomato',
27-
'reference = 99',
28-
'assert reference == 99']
29-
]
30-
notebook = notebook_builder.build(cells=cells)
31-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
32-
33-
34-
def test_space_cannot_alter_user_namespace_references():
35-
cells = [
36-
['%load_ext jupyter_spaces'],
37-
['reference = 100'],
38-
['%%space tomato',
39-
'reference = 99'],
40-
['assert reference == 100']
41-
]
42-
notebook = notebook_builder.build(cells=cells)
43-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
44-
45-
46-
def test_space_can_alter_user_namespace_references_using_global():
47-
cells = [
48-
['%load_ext jupyter_spaces'],
49-
['reference = 100'],
50-
['%%space tomato',
51-
'global reference',
52-
'reference = 99'],
53-
['assert reference == 99']
54-
]
55-
notebook = notebook_builder.build(cells=cells)
56-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
57-
58-
59-
def test_space_cannot_remove_user_namespace_references():
60-
cells = [
61-
['import pytest'],
62-
['%load_ext jupyter_spaces'],
63-
['reference = 100'],
64-
['%%space tomato',
65-
'with pytest.raises(NameError):',
66-
' del reference'],
67-
['assert reference == 100']
68-
]
69-
notebook = notebook_builder.build(cells=cells)
70-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
71-
72-
73-
def test_space_cannot_add_user_namespace_references():
74-
cells = [
75-
['import pytest'],
76-
['%load_ext jupyter_spaces'],
77-
['%%space tomato',
78-
'reference = 99'],
79-
['with pytest.raises(NameError):',
80-
' reference']
81-
]
82-
notebook = notebook_builder.build(cells=cells)
83-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
84-
85-
86-
def test_get_spaces_can_access_space_references():
87-
cells = [
88-
['from jupyter_spaces import get_spaces'],
89-
['%load_ext jupyter_spaces'],
90-
['%%space tomato',
91-
'reference = 99'],
92-
['spaces = get_spaces()'],
93-
['assert spaces["tomato"].namespace["reference"] == 99']
94-
]
95-
notebook = notebook_builder.build(cells=cells)
96-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
97-
98-
99-
def test_get_spaces_can_alter_space_references():
100-
cells = [
101-
['from jupyter_spaces import get_spaces'],
102-
['%load_ext jupyter_spaces'],
103-
['%%space tomato',
104-
'reference = 99'],
105-
['spaces = get_spaces()'],
106-
['spaces["tomato"].namespace["reference"] = 101'],
107-
['%%space tomato',
108-
'assert reference == 101']
109-
]
110-
notebook = notebook_builder.build(cells=cells)
111-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
112-
113-
114-
def test_get_spaces_can_remove_space_references():
115-
cells = [
116-
['import pytest'],
117-
['from jupyter_spaces import get_spaces'],
118-
['%load_ext jupyter_spaces'],
119-
['%%space tomato', 'reference = 99'],
120-
['spaces = get_spaces()'],
121-
['del spaces["tomato"].namespace["reference"]'],
122-
['%%space tomato',
123-
'with pytest.raises(NameError):',
124-
' reference']
125-
]
126-
notebook = notebook_builder.build(cells=cells)
127-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
128-
129-
130-
def test_get_spaces_reflects_space_references_changes():
131-
cells = [
132-
['from jupyter_spaces import get_spaces'],
133-
['%load_ext jupyter_spaces'],
134-
['%%space tomato',
135-
'reference = 99'],
136-
['spaces = get_spaces()'],
137-
['assert spaces["tomato"].namespace["reference"] == 99'],
138-
['%%space tomato',
139-
'reference = 101'],
140-
['assert spaces["tomato"].namespace["reference"] == 101']
141-
]
142-
notebook = notebook_builder.build(cells=cells)
143-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
144-
145-
146-
def test_get_spaces_reflects_space_removal():
147-
cells = [
148-
['from jupyter_spaces import get_spaces'],
149-
['%load_ext jupyter_spaces'],
150-
['%%space tomato',
151-
'reference = 99'],
152-
['spaces = get_spaces()'],
153-
['%remove_space tomato'],
154-
['assert "tomato" not in spaces']
155-
]
156-
notebook = notebook_builder.build(cells=cells)
157-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
158-
159-
160-
def test_get_spaces_reflects_extension_reload():
161-
cells = [
162-
['from jupyter_spaces import get_spaces'],
163-
['%load_ext jupyter_spaces'],
164-
['%%space tomato',
165-
'reference = 99'],
166-
['spaces = get_spaces()'],
167-
['%reload_ext jupyter_spaces'],
168-
['assert not spaces']
169-
]
170-
notebook = notebook_builder.build(cells=cells)
171-
notebook_processor.preprocess(nb=notebook, resources=notebook_resources)
1+
from IPython.utils.io import capture_output
2+
import pytest
3+
4+
from tests.fixtures import ip, global_ip
5+
6+
7+
def test_space_can_access_user_namespace_references(ip):
8+
ip.run_cell(raw_cell='reference = 100')
9+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference')
10+
11+
12+
def test_space_references_prioritised_over_user_namespace_references(ip):
13+
ip.run_cell(raw_cell='reference = 100')
14+
ip.run_cell_magic(magic_name='space', line='tomato',
15+
cell='reference = 99; assert reference == 99')
16+
17+
18+
def test_space_cannot_alter_user_namespace_references(ip):
19+
ip.run_cell(raw_cell='reference = 100')
20+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
21+
assert ip.user_global_ns['reference'] == 100
22+
23+
24+
def test_space_can_alter_user_namespace_references_using_global(ip):
25+
ip.run_cell(raw_cell='reference = 100')
26+
ip.run_cell_magic(magic_name='space', line='tomato',
27+
cell='global reference; reference = 99')
28+
assert ip.user_global_ns['reference'] == 99
29+
30+
31+
def test_space_cannot_remove_user_namespace_references(ip):
32+
ip.run_cell(raw_cell='reference = 100')
33+
with pytest.raises(NameError):
34+
ip.run_cell_magic(magic_name='space', line='tomato',
35+
cell='del reference')
36+
assert ip.user_global_ns['reference'] == 100
37+
38+
39+
def test_space_can_remove_user_namespace_references_using_global(ip):
40+
ip.run_cell(raw_cell='reference = 100')
41+
ip.run_cell_magic(magic_name='space', line='tomato',
42+
cell='global reference; del reference')
43+
assert 'reference' not in ip.user_global_ns
44+
45+
46+
def test_space_cannot_add_user_namespace_references(ip):
47+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
48+
assert 'reference' not in ip.user_global_ns
49+
50+
51+
def test_space_can_add_user_namespace_references_using_global(ip):
52+
ip.run_cell_magic(magic_name='space', line='tomato',
53+
cell='global reference; reference = 99')
54+
assert ip.user_global_ns['reference'] == 99
55+
56+
57+
def test_get_spaces_can_access_space_references(ip):
58+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
59+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
60+
ip.run_cell(raw_cell='spaces = get_spaces()')
61+
assert ip.user_global_ns['spaces']['tomato'].namespace['reference'] == 99
62+
63+
64+
def test_get_spaces_can_alter_space_references(ip):
65+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
66+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
67+
ip.run_cell(raw_cell='spaces = get_spaces()')
68+
ip.run_cell(raw_cell='spaces["tomato"].namespace["reference"] = 101')
69+
assert ip.user_global_ns['spaces']['tomato'].namespace['reference'] == 101
70+
71+
72+
def test_get_spaces_can_remove_space_references(ip):
73+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
74+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
75+
ip.run_cell(raw_cell='spaces = get_spaces()')
76+
ip.run_cell(raw_cell='del spaces["tomato"].namespace["reference"]')
77+
assert 'reference' not in ip.user_global_ns['spaces']['tomato'].namespace
78+
79+
80+
def test_get_spaces_reflects_space_references_changes(ip):
81+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
82+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
83+
ip.run_cell(raw_cell='spaces = get_spaces()')
84+
ip.run_cell(raw_cell='spaces["tomato"].namespace["reference"] = 101')
85+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 11')
86+
assert ip.user_global_ns['spaces']['tomato'].namespace['reference'] == 11
87+
88+
89+
def test_get_spaces_reflects_space_removal(ip):
90+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
91+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
92+
ip.run_cell(raw_cell='spaces = get_spaces()')
93+
ip.run_line_magic(magic_name='remove_space', line='tomato')
94+
assert 'tomato' not in ip.user_global_ns['spaces']
95+
96+
97+
def test_get_spaces_reflects_extension_reload(ip):
98+
ip.run_cell(raw_cell='from jupyter_spaces import get_spaces')
99+
ip.run_cell_magic(magic_name='space', line='tomato', cell='reference = 99')
100+
ip.run_cell(raw_cell='spaces = get_spaces()')
101+
ip.run_line_magic(magic_name='reload_ext', line='jupyter_spaces')
102+
assert not ip.user_global_ns['spaces']
103+
104+
105+
def test_space_can_print_to_console(ip):
106+
with capture_output() as captured:
107+
ip.run_cell_magic(magic_name='space', line='tomato', cell='print(100)')
108+
assert captured.stdout == '100\n'

tests/utils.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)