Show More
@@ -1,266 +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 | 24 | import nose.tools as nt |
|
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 | 51 | """FileLink: Calling _repr_html_ on non-existent files returns a warning |
|
52 | 52 | """ |
|
53 | 53 | fl = display.FileLink('example.txt') |
|
54 | 54 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
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 | def test_existing_path_FileLink_repr(): |
|
66 | 67 | """FileLink: Calling repr() functions as expected on existing filepath |
|
67 | 68 | """ |
|
68 | 69 | tf = NamedTemporaryFile() |
|
69 | 70 | fl = display.FileLink(tf.name) |
|
70 | 71 | actual = repr(fl) |
|
71 | 72 | expected = tf.name |
|
72 |
|
|
|
73 | assert actual == expected | |
|
74 | ||
|
73 | 75 | |
|
74 | 76 | def test_error_on_directory_to_FileLink(): |
|
75 | 77 | """FileLink: Raises error when passed directory |
|
76 | 78 | """ |
|
77 | 79 | td = mkdtemp() |
|
78 | 80 | nt.assert_raises(ValueError,display.FileLink,td) |
|
79 | 81 | |
|
80 | 82 | #-------------------------- |
|
81 | 83 | # FileLinks tests |
|
82 | 84 | #-------------------------- |
|
83 | 85 | |
|
84 | 86 | def test_instantiation_FileLinks(): |
|
85 | 87 | """FileLinks: Test class can be instantiated |
|
86 | 88 | """ |
|
87 | 89 | fls = display.FileLinks('example') |
|
88 | 90 | |
|
89 | 91 | def test_warning_on_non_existent_path_FileLinks(): |
|
90 | 92 | """FileLinks: Calling _repr_html_ on non-existent files returns a warning |
|
91 | 93 | """ |
|
92 | 94 | fls = display.FileLinks('example') |
|
93 | 95 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
94 | 96 | |
|
95 | 97 | def test_existing_path_FileLinks(): |
|
96 | 98 | """FileLinks: Calling _repr_html_ functions as expected on existing dir |
|
97 | 99 | """ |
|
98 | 100 | td = mkdtemp() |
|
99 | 101 | tf1 = NamedTemporaryFile(dir=td) |
|
100 | 102 | tf2 = NamedTemporaryFile(dir=td) |
|
101 | 103 | fl = display.FileLinks(td) |
|
102 | 104 | actual = fl._repr_html_() |
|
103 | 105 | actual = actual.split('\n') |
|
104 | 106 | actual.sort() |
|
105 | 107 | # the links should always have forward slashes, even on windows, so replace |
|
106 | 108 | # backslashes with forward slashes here |
|
107 | 109 | expected = ["%s/<br>" % td, |
|
108 | 110 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
109 | 111 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), |
|
110 | 112 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
111 | 113 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] |
|
112 | 114 | expected.sort() |
|
113 | 115 | # We compare the sorted list of links here as that's more reliable |
|
114 |
|
|
|
116 | assert actual == expected | |
|
117 | ||
|
115 | 118 | |
|
116 | 119 | def test_existing_path_FileLinks_alt_formatter(): |
|
117 | 120 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter |
|
118 | 121 | """ |
|
119 | 122 | td = mkdtemp() |
|
120 | 123 | tf1 = NamedTemporaryFile(dir=td) |
|
121 | 124 | tf2 = NamedTemporaryFile(dir=td) |
|
122 | 125 | def fake_formatter(dirname,fnames,included_suffixes): |
|
123 | 126 | return ["hello","world"] |
|
124 | 127 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
125 | 128 | actual = fl._repr_html_() |
|
126 | 129 | actual = actual.split('\n') |
|
127 | 130 | actual.sort() |
|
128 | 131 | expected = ["hello","world"] |
|
129 | 132 | expected.sort() |
|
130 | 133 | # We compare the sorted list of links here as that's more reliable |
|
131 |
|
|
|
134 | assert actual == expected | |
|
135 | ||
|
132 | 136 | |
|
133 | 137 | def test_existing_path_FileLinks_repr(): |
|
134 | 138 | """FileLinks: Calling repr() functions as expected on existing directory """ |
|
135 | 139 | td = mkdtemp() |
|
136 | 140 | tf1 = NamedTemporaryFile(dir=td) |
|
137 | 141 | tf2 = NamedTemporaryFile(dir=td) |
|
138 | 142 | fl = display.FileLinks(td) |
|
139 | 143 | actual = repr(fl) |
|
140 | 144 | actual = actual.split('\n') |
|
141 | 145 | actual.sort() |
|
142 | 146 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
143 | 147 | expected.sort() |
|
144 | 148 | # We compare the sorted list of links here as that's more reliable |
|
145 |
|
|
|
149 | assert actual == expected | |
|
150 | ||
|
146 | 151 | |
|
147 | 152 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
148 | 153 | """FileLinks: Calling repr() functions as expected w/ alt formatter |
|
149 | 154 | """ |
|
150 | 155 | td = mkdtemp() |
|
151 | 156 | tf1 = NamedTemporaryFile(dir=td) |
|
152 | 157 | tf2 = NamedTemporaryFile(dir=td) |
|
153 | 158 | def fake_formatter(dirname,fnames,included_suffixes): |
|
154 | 159 | return ["hello","world"] |
|
155 | 160 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
156 | 161 | actual = repr(fl) |
|
157 | 162 | actual = actual.split('\n') |
|
158 | 163 | actual.sort() |
|
159 | 164 | expected = ["hello","world"] |
|
160 | 165 | expected.sort() |
|
161 | 166 | # We compare the sorted list of links here as that's more reliable |
|
162 |
|
|
|
167 | assert actual == expected | |
|
168 | ||
|
163 | 169 | |
|
164 | 170 | def test_error_on_file_to_FileLinks(): |
|
165 | 171 | """FileLinks: Raises error when passed file |
|
166 | 172 | """ |
|
167 | 173 | td = mkdtemp() |
|
168 | 174 | tf1 = NamedTemporaryFile(dir=td) |
|
169 | 175 | nt.assert_raises(ValueError,display.FileLinks,tf1.name) |
|
170 | 176 | |
|
171 | 177 | def test_recursive_FileLinks(): |
|
172 | 178 | """FileLinks: Does not recurse when recursive=False |
|
173 | 179 | """ |
|
174 | 180 | td = mkdtemp() |
|
175 | 181 | tf = NamedTemporaryFile(dir=td) |
|
176 | 182 | subtd = mkdtemp(dir=td) |
|
177 | 183 | subtf = NamedTemporaryFile(dir=subtd) |
|
178 | 184 | fl = display.FileLinks(td) |
|
179 | 185 | actual = str(fl) |
|
180 | 186 | actual = actual.split('\n') |
|
181 |
|
|
|
187 | assert len(actual) == 4, actual | |
|
182 | 188 | fl = display.FileLinks(td, recursive=False) |
|
183 | 189 | actual = str(fl) |
|
184 | 190 | actual = actual.split('\n') |
|
185 |
|
|
|
191 | assert len(actual) == 2, actual | |
|
186 | 192 | |
|
187 | 193 | def test_audio_from_file(): |
|
188 | 194 | path = pjoin(dirname(__file__), 'test.wav') |
|
189 | 195 | display.Audio(filename=path) |
|
190 | 196 | |
|
191 | 197 | class TestAudioDataWithNumpy(TestCase): |
|
192 | 198 | |
|
193 | 199 | @skipif_not_numpy |
|
194 | 200 | def test_audio_from_numpy_array(self): |
|
195 | 201 | test_tone = get_test_tone() |
|
196 | 202 | audio = display.Audio(test_tone, rate=44100) |
|
197 |
|
|
|
203 | assert len(read_wav(audio.data)) == len(test_tone) | |
|
198 | 204 | |
|
199 | 205 | @skipif_not_numpy |
|
200 | 206 | def test_audio_from_list(self): |
|
201 | 207 | test_tone = get_test_tone() |
|
202 | 208 | audio = display.Audio(list(test_tone), rate=44100) |
|
203 |
|
|
|
209 | assert len(read_wav(audio.data)) == len(test_tone) | |
|
204 | 210 | |
|
205 | 211 | @skipif_not_numpy |
|
206 | 212 | def test_audio_from_numpy_array_without_rate_raises(self): |
|
207 | 213 | nt.assert_raises(ValueError, display.Audio, get_test_tone()) |
|
208 | 214 | |
|
209 | 215 | @skipif_not_numpy |
|
210 | 216 | def test_audio_data_normalization(self): |
|
211 | 217 | expected_max_value = numpy.iinfo(numpy.int16).max |
|
212 | 218 | for scale in [1, 0.5, 2]: |
|
213 | 219 | audio = display.Audio(get_test_tone(scale), rate=44100) |
|
214 | 220 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
215 |
|
|
|
221 | assert actual_max_value == expected_max_value | |
|
216 | 222 | |
|
217 | 223 | @skipif_not_numpy |
|
218 | 224 | def test_audio_data_without_normalization(self): |
|
219 | 225 | max_int16 = numpy.iinfo(numpy.int16).max |
|
220 | 226 | for scale in [1, 0.5, 0.2]: |
|
221 | 227 | test_tone = get_test_tone(scale) |
|
222 | 228 | test_tone_max_abs = numpy.max(numpy.abs(test_tone)) |
|
223 | 229 | expected_max_value = int(max_int16 * test_tone_max_abs) |
|
224 | 230 | audio = display.Audio(test_tone, rate=44100, normalize=False) |
|
225 | 231 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
226 |
|
|
|
232 | assert actual_max_value == expected_max_value | |
|
227 | 233 | |
|
228 | 234 | def test_audio_data_without_normalization_raises_for_invalid_data(self): |
|
229 | 235 | nt.assert_raises( |
|
230 | 236 | ValueError, |
|
231 | 237 | lambda: display.Audio([1.001], rate=44100, normalize=False)) |
|
232 | 238 | nt.assert_raises( |
|
233 | 239 | ValueError, |
|
234 | 240 | lambda: display.Audio([-1.001], rate=44100, normalize=False)) |
|
235 | 241 | |
|
236 | 242 | def simulate_numpy_not_installed(): |
|
237 | 243 | try: |
|
238 | 244 | import numpy |
|
239 | 245 | return mock.patch('numpy.array', mock.MagicMock(side_effect=ImportError)) |
|
240 | 246 | except ModuleNotFoundError: |
|
241 | 247 | return lambda x:x |
|
242 | 248 | |
|
243 | 249 | @simulate_numpy_not_installed() |
|
244 | 250 | class TestAudioDataWithoutNumpy(TestAudioDataWithNumpy): |
|
245 | 251 | # All tests from `TestAudioDataWithNumpy` are inherited. |
|
246 | 252 | |
|
247 | 253 | @skipif_not_numpy |
|
248 | 254 | def test_audio_raises_for_nested_list(self): |
|
249 | 255 | stereo_signal = [list(get_test_tone())] * 2 |
|
250 | 256 | nt.assert_raises( |
|
251 | 257 | TypeError, |
|
252 | 258 | lambda: display.Audio(stereo_signal, rate=44100)) |
|
253 | 259 | |
|
254 | 260 | @skipif_not_numpy |
|
255 | 261 | def get_test_tone(scale=1): |
|
256 | 262 | return numpy.sin(2 * numpy.pi * 440 * numpy.linspace(0, 1, 44100)) * scale |
|
257 | 263 | |
|
258 | 264 | def read_wav(data): |
|
259 | 265 | with wave.open(BytesIO(data)) as wave_file: |
|
260 | 266 | wave_data = wave_file.readframes(wave_file.getnframes()) |
|
261 | 267 | num_samples = wave_file.getnframes() * wave_file.getnchannels() |
|
262 | 268 | return struct.unpack('<%sh' % num_samples, wave_data) |
|
263 | 269 | |
|
264 | 270 | def test_code_from_file(): |
|
265 | 271 | c = display.Code(filename=__file__) |
|
266 | 272 | assert c._repr_html_().startswith('<style>') |
General Comments 0
You need to be logged in to leave comments.
Login now