##// END OF EJS Templates
added basic test...
sukisuki -
Show More
@@ -1,152 +1,161 b''
1 # Copyright (c) IPython Development Team.
1 # Copyright (c) IPython Development Team.
2 # Distributed under the terms of the Modified BSD License.
2 # Distributed under the terms of the Modified BSD License.
3
3
4 import json
4 import json
5 import tempfile
5 import os
6 import os
6 import warnings
7 import warnings
7
8
8 import nose.tools as nt
9 import nose.tools as nt
9
10
10 from IPython.core import display
11 from IPython.core import display
11 from IPython.core.getipython import get_ipython
12 from IPython.core.getipython import get_ipython
12 from IPython import paths as ipath
13 from IPython import paths as ipath
13
14
14 import IPython.testing.decorators as dec
15 import IPython.testing.decorators as dec
15
16
16 def test_image_size():
17 def test_image_size():
17 """Simple test for display.Image(args, width=x,height=y)"""
18 """Simple test for display.Image(args, width=x,height=y)"""
18 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
19 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
19 img = display.Image(url=thisurl, width=200, height=200)
20 img = display.Image(url=thisurl, width=200, height=200)
20 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
21 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
21 img = display.Image(url=thisurl, width=200)
22 img = display.Image(url=thisurl, width=200)
22 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
23 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
23 img = display.Image(url=thisurl)
24 img = display.Image(url=thisurl)
24 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
25 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
25 img = display.Image(url=thisurl, unconfined=True)
26 img = display.Image(url=thisurl, unconfined=True)
26 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
27 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
27
28
28 def test_retina_png():
29 def test_retina_png():
29 here = os.path.dirname(__file__)
30 here = os.path.dirname(__file__)
30 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
31 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
31 nt.assert_equal(img.height, 1)
32 nt.assert_equal(img.height, 1)
32 nt.assert_equal(img.width, 1)
33 nt.assert_equal(img.width, 1)
33 data, md = img._repr_png_()
34 data, md = img._repr_png_()
34 nt.assert_equal(md['width'], 1)
35 nt.assert_equal(md['width'], 1)
35 nt.assert_equal(md['height'], 1)
36 nt.assert_equal(md['height'], 1)
36
37
37 def test_retina_jpeg():
38 def test_retina_jpeg():
38 here = os.path.dirname(__file__)
39 here = os.path.dirname(__file__)
39 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
40 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
40 nt.assert_equal(img.height, 1)
41 nt.assert_equal(img.height, 1)
41 nt.assert_equal(img.width, 1)
42 nt.assert_equal(img.width, 1)
42 data, md = img._repr_jpeg_()
43 data, md = img._repr_jpeg_()
43 nt.assert_equal(md['width'], 1)
44 nt.assert_equal(md['width'], 1)
44 nt.assert_equal(md['height'], 1)
45 nt.assert_equal(md['height'], 1)
45
46
46 def test_image_filename_defaults():
47 def test_image_filename_defaults():
47 '''test format constraint, and validity of jpeg and png'''
48 '''test format constraint, and validity of jpeg and png'''
48 tpath = ipath.get_ipython_package_dir()
49 tpath = ipath.get_ipython_package_dir()
49 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
50 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
50 embed=True)
51 embed=True)
51 nt.assert_raises(ValueError, display.Image)
52 nt.assert_raises(ValueError, display.Image)
52 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
53 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
53 # check boths paths to allow packages to test at build and install time
54 # check boths paths to allow packages to test at build and install time
54 imgfile = os.path.join(tpath, 'core/tests/2x2.png')
55 imgfile = os.path.join(tpath, 'core/tests/2x2.png')
55 img = display.Image(filename=imgfile)
56 img = display.Image(filename=imgfile)
56 nt.assert_equal('png', img.format)
57 nt.assert_equal('png', img.format)
57 nt.assert_is_not_none(img._repr_png_())
58 nt.assert_is_not_none(img._repr_png_())
58 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
59 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
59 nt.assert_equal('jpeg', img.format)
60 nt.assert_equal('jpeg', img.format)
60 nt.assert_is_none(img._repr_jpeg_())
61 nt.assert_is_none(img._repr_jpeg_())
61
62
62 def _get_inline_config():
63 def _get_inline_config():
63 from ipython_kernel.pylab.config import InlineBackend
64 from ipython_kernel.pylab.config import InlineBackend
64 return InlineBackend.instance()
65 return InlineBackend.instance()
65
66
66 @dec.skip_without('matplotlib')
67 @dec.skip_without('matplotlib')
67 def test_set_matplotlib_close():
68 def test_set_matplotlib_close():
68 cfg = _get_inline_config()
69 cfg = _get_inline_config()
69 cfg.close_figures = False
70 cfg.close_figures = False
70 display.set_matplotlib_close()
71 display.set_matplotlib_close()
71 assert cfg.close_figures
72 assert cfg.close_figures
72 display.set_matplotlib_close(False)
73 display.set_matplotlib_close(False)
73 assert not cfg.close_figures
74 assert not cfg.close_figures
74
75
75 _fmt_mime_map = {
76 _fmt_mime_map = {
76 'png': 'image/png',
77 'png': 'image/png',
77 'jpeg': 'image/jpeg',
78 'jpeg': 'image/jpeg',
78 'pdf': 'application/pdf',
79 'pdf': 'application/pdf',
79 'retina': 'image/png',
80 'retina': 'image/png',
80 'svg': 'image/svg+xml',
81 'svg': 'image/svg+xml',
81 }
82 }
82
83
83 @dec.skip_without('matplotlib')
84 @dec.skip_without('matplotlib')
84 def test_set_matplotlib_formats():
85 def test_set_matplotlib_formats():
85 from matplotlib.figure import Figure
86 from matplotlib.figure import Figure
86 formatters = get_ipython().display_formatter.formatters
87 formatters = get_ipython().display_formatter.formatters
87 for formats in [
88 for formats in [
88 ('png',),
89 ('png',),
89 ('pdf', 'svg'),
90 ('pdf', 'svg'),
90 ('jpeg', 'retina', 'png'),
91 ('jpeg', 'retina', 'png'),
91 (),
92 (),
92 ]:
93 ]:
93 active_mimes = {_fmt_mime_map[fmt] for fmt in formats}
94 active_mimes = {_fmt_mime_map[fmt] for fmt in formats}
94 display.set_matplotlib_formats(*formats)
95 display.set_matplotlib_formats(*formats)
95 for mime, f in formatters.items():
96 for mime, f in formatters.items():
96 if mime in active_mimes:
97 if mime in active_mimes:
97 nt.assert_in(Figure, f)
98 nt.assert_in(Figure, f)
98 else:
99 else:
99 nt.assert_not_in(Figure, f)
100 nt.assert_not_in(Figure, f)
100
101
101 @dec.skip_without('matplotlib')
102 @dec.skip_without('matplotlib')
102 def test_set_matplotlib_formats_kwargs():
103 def test_set_matplotlib_formats_kwargs():
103 from matplotlib.figure import Figure
104 from matplotlib.figure import Figure
104 ip = get_ipython()
105 ip = get_ipython()
105 cfg = _get_inline_config()
106 cfg = _get_inline_config()
106 cfg.print_figure_kwargs.update(dict(foo='bar'))
107 cfg.print_figure_kwargs.update(dict(foo='bar'))
107 kwargs = dict(quality=10)
108 kwargs = dict(quality=10)
108 display.set_matplotlib_formats('png', **kwargs)
109 display.set_matplotlib_formats('png', **kwargs)
109 formatter = ip.display_formatter.formatters['image/png']
110 formatter = ip.display_formatter.formatters['image/png']
110 f = formatter.lookup_by_type(Figure)
111 f = formatter.lookup_by_type(Figure)
111 cell = f.__closure__[0].cell_contents
112 cell = f.__closure__[0].cell_contents
112 expected = kwargs
113 expected = kwargs
113 expected.update(cfg.print_figure_kwargs)
114 expected.update(cfg.print_figure_kwargs)
114 nt.assert_equal(cell, expected)
115 nt.assert_equal(cell, expected)
115
116
116 def test_displayobject_repr():
117 def test_displayobject_repr():
117 h = display.HTML('<br />')
118 h = display.HTML('<br />')
118 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
119 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
119 h._show_mem_addr = True
120 h._show_mem_addr = True
120 nt.assert_equal(repr(h), object.__repr__(h))
121 nt.assert_equal(repr(h), object.__repr__(h))
121 h._show_mem_addr = False
122 h._show_mem_addr = False
122 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
123 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
123
124
124 j = display.Javascript('')
125 j = display.Javascript('')
125 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
126 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
126 j._show_mem_addr = True
127 j._show_mem_addr = True
127 nt.assert_equal(repr(j), object.__repr__(j))
128 nt.assert_equal(repr(j), object.__repr__(j))
128 j._show_mem_addr = False
129 j._show_mem_addr = False
129 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
130 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
130
131
131 def test_json():
132 def test_json():
132 d = {'a': 5}
133 d = {'a': 5}
133 lis = [d]
134 lis = [d]
134 j = display.JSON(d)
135 j = display.JSON(d)
135 nt.assert_equal(j._repr_json_(), d)
136 nt.assert_equal(j._repr_json_(), d)
136
137
137 with warnings.catch_warnings(record=True) as w:
138 with warnings.catch_warnings(record=True) as w:
138 warnings.simplefilter("always")
139 warnings.simplefilter("always")
139 j = display.JSON(json.dumps(d))
140 j = display.JSON(json.dumps(d))
140 nt.assert_equal(len(w), 1)
141 nt.assert_equal(len(w), 1)
141 nt.assert_equal(j._repr_json_(), d)
142 nt.assert_equal(j._repr_json_(), d)
142
143
143 j = display.JSON(lis)
144 j = display.JSON(lis)
144 nt.assert_equal(j._repr_json_(), lis)
145 nt.assert_equal(j._repr_json_(), lis)
145
146
146 with warnings.catch_warnings(record=True) as w:
147 with warnings.catch_warnings(record=True) as w:
147 warnings.simplefilter("always")
148 warnings.simplefilter("always")
148 j = display.JSON(json.dumps(lis))
149 j = display.JSON(json.dumps(lis))
149 nt.assert_equal(len(w), 1)
150 nt.assert_equal(len(w), 1)
150 nt.assert_equal(j._repr_json_(), lis)
151 nt.assert_equal(j._repr_json_(), lis)
151
152
152
153 def test_video_embedding():
154 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
155 with tempfile.NamedTemporaryFile(suffix='.mp4') as f:
156 with open(f.name,'wb') as f:
157 f.write(b'abc')
158
159 v = display.Video(f.name, embed=True)
160 html = v._repr_html_()
161 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
General Comments 0
You need to be logged in to leave comments. Login now