Show More
@@ -1,36 +1,34 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Test suite for the deepreload module.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | from pathlib import Path |
|
8 | 8 | |
|
9 | import nose.tools as nt | |
|
10 | ||
|
11 | 9 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
12 | 10 | from IPython.utils.tempdir import TemporaryDirectory |
|
13 | 11 | from IPython.lib.deepreload import reload as dreload |
|
14 | 12 | |
|
15 | 13 | |
|
16 | 14 | def test_deepreload(): |
|
17 | 15 | "Test that dreload does deep reloads and skips excluded modules." |
|
18 | 16 | with TemporaryDirectory() as tmpdir: |
|
19 | 17 | with prepended_to_syspath(tmpdir): |
|
20 | 18 | tmpdirpath = Path(tmpdir) |
|
21 | 19 | with open(tmpdirpath / "A.py", "w") as f: |
|
22 | 20 | f.write("class Object(object):\n pass\n") |
|
23 | 21 | with open(tmpdirpath / "B.py", "w") as f: |
|
24 | 22 | f.write("import A\n") |
|
25 | 23 | import A |
|
26 | 24 | import B |
|
27 | 25 | |
|
28 | 26 | # Test that A is not reloaded. |
|
29 | 27 | obj = A.Object() |
|
30 | 28 | dreload(B, exclude=["A"]) |
|
31 |
|
|
|
29 | assert isinstance(obj, A.Object) is True | |
|
32 | 30 | |
|
33 | 31 | # Test that A is reloaded. |
|
34 | 32 | obj = A.Object() |
|
35 | 33 | dreload(B) |
|
36 |
|
|
|
34 | assert isinstance(obj, A.Object) is False |
@@ -1,272 +1,272 b'' | |||
|
1 | 1 | """Tests for IPython.lib.display. |
|
2 | 2 | |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2012, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | from tempfile import NamedTemporaryFile, mkdtemp |
|
16 | 16 | from os.path import split, join as pjoin, dirname |
|
17 | 17 | import pathlib |
|
18 | 18 | from unittest import TestCase, mock |
|
19 | 19 | import struct |
|
20 | 20 | import wave |
|
21 | 21 | from io import BytesIO |
|
22 | 22 | |
|
23 | 23 | # Third-party imports |
|
24 | import nose.tools as nt | |
|
24 | import pytest | |
|
25 | 25 | |
|
26 | 26 | try: |
|
27 | 27 | import numpy |
|
28 | 28 | except ImportError: |
|
29 | 29 | pass |
|
30 | 30 | |
|
31 | 31 | # Our own imports |
|
32 | 32 | from IPython.lib import display |
|
33 | 33 | |
|
34 | 34 | from IPython.testing.decorators import skipif_not_numpy |
|
35 | 35 | |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | # Classes and functions |
|
38 | 38 | #----------------------------------------------------------------------------- |
|
39 | 39 | |
|
40 | 40 | #-------------------------- |
|
41 | 41 | # FileLink tests |
|
42 | 42 | #-------------------------- |
|
43 | 43 | |
|
44 | 44 | def test_instantiation_FileLink(): |
|
45 | 45 | """FileLink: Test class can be instantiated""" |
|
46 | 46 | fl = display.FileLink('example.txt') |
|
47 | 47 | # TODO: remove if when only Python >= 3.6 is supported |
|
48 | 48 | fl = display.FileLink(pathlib.PurePath('example.txt')) |
|
49 | 49 | |
|
50 | 50 | def test_warning_on_non_existent_path_FileLink(): |
|
51 | """FileLink: Calling _repr_html_ on non-existent files returns a warning | |
|
52 | """ | |
|
53 | fl = display.FileLink('example.txt') | |
|
54 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) | |
|
51 | """FileLink: Calling _repr_html_ on non-existent files returns a warning""" | |
|
52 | fl = display.FileLink("example.txt") | |
|
53 | assert fl._repr_html_().startswith("Path (<tt>example.txt</tt>)") | |
|
54 | ||
|
55 | 55 | |
|
56 | 56 | def test_existing_path_FileLink(): |
|
57 | 57 | """FileLink: Calling _repr_html_ functions as expected on existing filepath |
|
58 | 58 | """ |
|
59 | 59 | tf = NamedTemporaryFile() |
|
60 | 60 | fl = display.FileLink(tf.name) |
|
61 | 61 | actual = fl._repr_html_() |
|
62 | 62 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name, tf.name) |
|
63 | 63 | assert actual == expected |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | def test_existing_path_FileLink_repr(): |
|
67 | 67 | """FileLink: Calling repr() functions as expected on existing filepath |
|
68 | 68 | """ |
|
69 | 69 | tf = NamedTemporaryFile() |
|
70 | 70 | fl = display.FileLink(tf.name) |
|
71 | 71 | actual = repr(fl) |
|
72 | 72 | expected = tf.name |
|
73 | 73 | assert actual == expected |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def test_error_on_directory_to_FileLink(): |
|
77 | 77 | """FileLink: Raises error when passed directory |
|
78 | 78 | """ |
|
79 | 79 | td = mkdtemp() |
|
80 |
|
|
|
80 | pytest.raises(ValueError, display.FileLink, td) | |
|
81 | 81 | |
|
82 | 82 | #-------------------------- |
|
83 | 83 | # FileLinks tests |
|
84 | 84 | #-------------------------- |
|
85 | 85 | |
|
86 | 86 | def test_instantiation_FileLinks(): |
|
87 | 87 | """FileLinks: Test class can be instantiated |
|
88 | 88 | """ |
|
89 | 89 | fls = display.FileLinks('example') |
|
90 | 90 | |
|
91 | 91 | def test_warning_on_non_existent_path_FileLinks(): |
|
92 | """FileLinks: Calling _repr_html_ on non-existent files returns a warning | |
|
93 | """ | |
|
94 | fls = display.FileLinks('example') | |
|
95 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) | |
|
92 | """FileLinks: Calling _repr_html_ on non-existent files returns a warning""" | |
|
93 | fls = display.FileLinks("example") | |
|
94 | assert fls._repr_html_().startswith("Path (<tt>example</tt>)") | |
|
95 | ||
|
96 | 96 | |
|
97 | 97 | def test_existing_path_FileLinks(): |
|
98 | 98 | """FileLinks: Calling _repr_html_ functions as expected on existing dir |
|
99 | 99 | """ |
|
100 | 100 | td = mkdtemp() |
|
101 | 101 | tf1 = NamedTemporaryFile(dir=td) |
|
102 | 102 | tf2 = NamedTemporaryFile(dir=td) |
|
103 | 103 | fl = display.FileLinks(td) |
|
104 | 104 | actual = fl._repr_html_() |
|
105 | 105 | actual = actual.split('\n') |
|
106 | 106 | actual.sort() |
|
107 | 107 | # the links should always have forward slashes, even on windows, so replace |
|
108 | 108 | # backslashes with forward slashes here |
|
109 | 109 | expected = ["%s/<br>" % td, |
|
110 | 110 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
111 | 111 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), |
|
112 | 112 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
113 | 113 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] |
|
114 | 114 | expected.sort() |
|
115 | 115 | # We compare the sorted list of links here as that's more reliable |
|
116 | 116 | assert actual == expected |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def test_existing_path_FileLinks_alt_formatter(): |
|
120 | 120 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter |
|
121 | 121 | """ |
|
122 | 122 | td = mkdtemp() |
|
123 | 123 | tf1 = NamedTemporaryFile(dir=td) |
|
124 | 124 | tf2 = NamedTemporaryFile(dir=td) |
|
125 | 125 | def fake_formatter(dirname,fnames,included_suffixes): |
|
126 | 126 | return ["hello","world"] |
|
127 | 127 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
128 | 128 | actual = fl._repr_html_() |
|
129 | 129 | actual = actual.split('\n') |
|
130 | 130 | actual.sort() |
|
131 | 131 | expected = ["hello","world"] |
|
132 | 132 | expected.sort() |
|
133 | 133 | # We compare the sorted list of links here as that's more reliable |
|
134 | 134 | assert actual == expected |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | def test_existing_path_FileLinks_repr(): |
|
138 | 138 | """FileLinks: Calling repr() functions as expected on existing directory """ |
|
139 | 139 | td = mkdtemp() |
|
140 | 140 | tf1 = NamedTemporaryFile(dir=td) |
|
141 | 141 | tf2 = NamedTemporaryFile(dir=td) |
|
142 | 142 | fl = display.FileLinks(td) |
|
143 | 143 | actual = repr(fl) |
|
144 | 144 | actual = actual.split('\n') |
|
145 | 145 | actual.sort() |
|
146 | 146 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
147 | 147 | expected.sort() |
|
148 | 148 | # We compare the sorted list of links here as that's more reliable |
|
149 | 149 | assert actual == expected |
|
150 | 150 | |
|
151 | 151 | |
|
152 | 152 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
153 | 153 | """FileLinks: Calling repr() functions as expected w/ alt formatter |
|
154 | 154 | """ |
|
155 | 155 | td = mkdtemp() |
|
156 | 156 | tf1 = NamedTemporaryFile(dir=td) |
|
157 | 157 | tf2 = NamedTemporaryFile(dir=td) |
|
158 | 158 | def fake_formatter(dirname,fnames,included_suffixes): |
|
159 | 159 | return ["hello","world"] |
|
160 | 160 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
161 | 161 | actual = repr(fl) |
|
162 | 162 | actual = actual.split('\n') |
|
163 | 163 | actual.sort() |
|
164 | 164 | expected = ["hello","world"] |
|
165 | 165 | expected.sort() |
|
166 | 166 | # We compare the sorted list of links here as that's more reliable |
|
167 | 167 | assert actual == expected |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | def test_error_on_file_to_FileLinks(): |
|
171 | 171 | """FileLinks: Raises error when passed file |
|
172 | 172 | """ |
|
173 | 173 | td = mkdtemp() |
|
174 | 174 | tf1 = NamedTemporaryFile(dir=td) |
|
175 |
|
|
|
175 | pytest.raises(ValueError, display.FileLinks, tf1.name) | |
|
176 | ||
|
176 | 177 | |
|
177 | 178 | def test_recursive_FileLinks(): |
|
178 | 179 | """FileLinks: Does not recurse when recursive=False |
|
179 | 180 | """ |
|
180 | 181 | td = mkdtemp() |
|
181 | 182 | tf = NamedTemporaryFile(dir=td) |
|
182 | 183 | subtd = mkdtemp(dir=td) |
|
183 | 184 | subtf = NamedTemporaryFile(dir=subtd) |
|
184 | 185 | fl = display.FileLinks(td) |
|
185 | 186 | actual = str(fl) |
|
186 | 187 | actual = actual.split('\n') |
|
187 | 188 | assert len(actual) == 4, actual |
|
188 | 189 | fl = display.FileLinks(td, recursive=False) |
|
189 | 190 | actual = str(fl) |
|
190 | 191 | actual = actual.split('\n') |
|
191 | 192 | assert len(actual) == 2, actual |
|
192 | 193 | |
|
193 | 194 | def test_audio_from_file(): |
|
194 | 195 | path = pjoin(dirname(__file__), 'test.wav') |
|
195 | 196 | display.Audio(filename=path) |
|
196 | 197 | |
|
197 | 198 | class TestAudioDataWithNumpy(TestCase): |
|
198 | 199 | |
|
199 | 200 | @skipif_not_numpy |
|
200 | 201 | def test_audio_from_numpy_array(self): |
|
201 | 202 | test_tone = get_test_tone() |
|
202 | 203 | audio = display.Audio(test_tone, rate=44100) |
|
203 | 204 | assert len(read_wav(audio.data)) == len(test_tone) |
|
204 | 205 | |
|
205 | 206 | @skipif_not_numpy |
|
206 | 207 | def test_audio_from_list(self): |
|
207 | 208 | test_tone = get_test_tone() |
|
208 | 209 | audio = display.Audio(list(test_tone), rate=44100) |
|
209 | 210 | assert len(read_wav(audio.data)) == len(test_tone) |
|
210 | 211 | |
|
211 | 212 | @skipif_not_numpy |
|
212 | 213 | def test_audio_from_numpy_array_without_rate_raises(self): |
|
213 |
|
|
|
214 | self.assertRaises(ValueError, display.Audio, get_test_tone()) | |
|
214 | 215 | |
|
215 | 216 | @skipif_not_numpy |
|
216 | 217 | def test_audio_data_normalization(self): |
|
217 | 218 | expected_max_value = numpy.iinfo(numpy.int16).max |
|
218 | 219 | for scale in [1, 0.5, 2]: |
|
219 | 220 | audio = display.Audio(get_test_tone(scale), rate=44100) |
|
220 | 221 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
221 | 222 | assert actual_max_value == expected_max_value |
|
222 | 223 | |
|
223 | 224 | @skipif_not_numpy |
|
224 | 225 | def test_audio_data_without_normalization(self): |
|
225 | 226 | max_int16 = numpy.iinfo(numpy.int16).max |
|
226 | 227 | for scale in [1, 0.5, 0.2]: |
|
227 | 228 | test_tone = get_test_tone(scale) |
|
228 | 229 | test_tone_max_abs = numpy.max(numpy.abs(test_tone)) |
|
229 | 230 | expected_max_value = int(max_int16 * test_tone_max_abs) |
|
230 | 231 | audio = display.Audio(test_tone, rate=44100, normalize=False) |
|
231 | 232 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
232 | 233 | assert actual_max_value == expected_max_value |
|
233 | 234 | |
|
234 | 235 | def test_audio_data_without_normalization_raises_for_invalid_data(self): |
|
235 |
|
|
|
236 | self.assertRaises( | |
|
236 | 237 | ValueError, |
|
237 | 238 | lambda: display.Audio([1.001], rate=44100, normalize=False)) |
|
238 |
|
|
|
239 | self.assertRaises( | |
|
239 | 240 | ValueError, |
|
240 | 241 | lambda: display.Audio([-1.001], rate=44100, normalize=False)) |
|
241 | 242 | |
|
242 | 243 | def simulate_numpy_not_installed(): |
|
243 | 244 | try: |
|
244 | 245 | import numpy |
|
245 | 246 | return mock.patch('numpy.array', mock.MagicMock(side_effect=ImportError)) |
|
246 | 247 | except ModuleNotFoundError: |
|
247 | 248 | return lambda x:x |
|
248 | 249 | |
|
249 | 250 | @simulate_numpy_not_installed() |
|
250 | 251 | class TestAudioDataWithoutNumpy(TestAudioDataWithNumpy): |
|
251 | 252 | # All tests from `TestAudioDataWithNumpy` are inherited. |
|
252 | 253 | |
|
253 | 254 | @skipif_not_numpy |
|
254 | 255 | def test_audio_raises_for_nested_list(self): |
|
255 | 256 | stereo_signal = [list(get_test_tone())] * 2 |
|
256 | nt.assert_raises( | |
|
257 | TypeError, | |
|
258 | lambda: display.Audio(stereo_signal, rate=44100)) | |
|
257 | self.assertRaises(TypeError, lambda: display.Audio(stereo_signal, rate=44100)) | |
|
258 | ||
|
259 | 259 | |
|
260 | 260 | @skipif_not_numpy |
|
261 | 261 | def get_test_tone(scale=1): |
|
262 | 262 | return numpy.sin(2 * numpy.pi * 440 * numpy.linspace(0, 1, 44100)) * scale |
|
263 | 263 | |
|
264 | 264 | def read_wav(data): |
|
265 | 265 | with wave.open(BytesIO(data)) as wave_file: |
|
266 | 266 | wave_data = wave_file.readframes(wave_file.getnframes()) |
|
267 | 267 | num_samples = wave_file.getnframes() * wave_file.getnchannels() |
|
268 | 268 | return struct.unpack('<%sh' % num_samples, wave_data) |
|
269 | 269 | |
|
270 | 270 | def test_code_from_file(): |
|
271 | 271 | c = display.Code(filename=__file__) |
|
272 | 272 | assert c._repr_html_().startswith('<style>') |
General Comments 0
You need to be logged in to leave comments.
Login now