##// END OF EJS Templates
added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency.
added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency.

File last commit:

r8836:b5e4338f
r8836:b5e4338f
Show More
test_display.py
153 lines | 5.3 KiB | text/x-python | PythonLexer
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 """Tests for IPython.lib.display.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
Greg Caporaso
added tests of formatting functionality
r8369 from tempfile import NamedTemporaryFile, mkdtemp
Greg Caporaso
fixed function name overlap that was causing a test to not be run
r8445 from os.path import split
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366
# Third-party imports
import nose.tools as nt
# Our own imports
from IPython.lib import display
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
#--------------------------
# FileLink tests
#--------------------------
def test_instantiation_FileLink():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLink: Test class can be instantiated"""
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 fl = display.FileLink('example.txt')
def test_warning_on_non_existant_path_FileLink():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLink: Calling _repr_html_ on non-existant files returns a warning
"""
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 fl = display.FileLink('example.txt')
nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
Greg Caporaso
added tests of formatting functionality
r8369 def test_existing_path_FileLink():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLink: Calling _repr_html_ functions as expected on existing filepath
"""
Greg Caporaso
added tests of formatting functionality
r8369 tf = NamedTemporaryFile()
fl = display.FileLink(tf.name)
actual = fl._repr_html_()
Greg Caporaso
modified to join urls with '/'.join instead of os.path.join. @takluyver pointed out that while os.path.join does work for this on OS X and Linux, it's not meant for constructing URLs, so fails on Windows where paths are constructed using \ rather than /.
r8440 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
Greg Caporaso
added tests of formatting functionality
r8369 nt.assert_equal(actual,expected)
Greg Caporaso
added tests of repr()
r8444 def test_existing_path_FileLink_repr():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLink: Calling repr() functions as expected on existing filepath
"""
Greg Caporaso
added tests of repr()
r8444 tf = NamedTemporaryFile()
fl = display.FileLink(tf.name)
actual = repr(fl)
expected = tf.name
nt.assert_equal(actual,expected)
Greg Caporaso
added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency.
r8836 def test_error_on_directory_to_FileLink():
"""FileLink: Raises error when passed directory
"""
td = mkdtemp()
nt.assert_raises(ValueError,display.FileLink,td)
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 #--------------------------
# FileLinks tests
#--------------------------
def test_instantiation_FileLinks():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Test class can be instantiated
"""
Greg Caporaso
added tests of formatting functionality
r8369 fls = display.FileLinks('example')
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366
def test_warning_on_non_existant_path_FileLinks():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Calling _repr_html_ on non-existant files returns a warning
"""
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 fls = display.FileLinks('example')
nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
Greg Caporaso
added tests of formatting functionality
r8369 def test_existing_path_FileLinks():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Calling _repr_html_ functions as expected on existing dir
"""
Greg Caporaso
added tests of formatting functionality
r8369 td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
tf2 = NamedTemporaryFile(dir=td)
fl = display.FileLinks(td)
actual = fl._repr_html_()
actual = actual.split('\n')
actual.sort()
Greg Caporaso
noew removes trailing slashes if they are provided in input so the output can be formatted consistently by always appending a trailing slash (which previously could result in two trailing slashes if the input path included a trailing slash)
r8631 expected = ["%s/<br>" % td,
Greg Caporaso
improved formatting for FileLinks
r8619 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]),
"&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])]
Greg Caporaso
added tests of formatting functionality
r8369 expected.sort()
# We compare the sorted list of links here as that's more reliable
nt.assert_equal(actual,expected)
Greg Caporaso
added tests of passing alternative formatter functions
r8632 def test_existing_path_FileLinks_alt_formatter():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
Greg Caporaso
added tests of passing alternative formatter functions
r8632 """
td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
tf2 = NamedTemporaryFile(dir=td)
Greg Caporaso
added included_suffixes as an option to display formatters - previously it wasn't possible to pass these in if defining custom display formatters
r8802 def fake_formatter(dirname,fnames,included_suffixes):
Greg Caporaso
updating to make interface more intuitive now that we're not using os.path.walk
r8801 return ["hello","world"]
Greg Caporaso
added tests of passing alternative formatter functions
r8632 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
actual = fl._repr_html_()
actual = actual.split('\n')
actual.sort()
expected = ["hello","world"]
expected.sort()
# We compare the sorted list of links here as that's more reliable
nt.assert_equal(actual,expected)
Greg Caporaso
added tests of repr()
r8444 def test_existing_path_FileLinks_repr():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Calling repr() functions as expected on existing directory """
Greg Caporaso
added tests of repr()
r8444 td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
tf2 = NamedTemporaryFile(dir=td)
fl = display.FileLinks(td)
actual = repr(fl)
actual = actual.split('\n')
actual.sort()
Greg Caporaso
noew removes trailing slashes if they are provided in input so the output can be formatted consistently by always appending a trailing slash (which previously could result in two trailing slashes if the input path included a trailing slash)
r8631 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
Greg Caporaso
added tests of repr()
r8444 expected.sort()
# We compare the sorted list of links here as that's more reliable
nt.assert_equal(actual,expected)
Greg Caporaso
added tests of passing alternative formatter functions
r8632
def test_existing_path_FileLinks_repr_alt_formatter():
Greg Caporaso
removing DirectoryLink class (which was an alias for FileLink) and modifying FileLink to raise an error if a directory is provided. @ellisonbg pointed out that these give a 404. I think removing this for now is the way to go as we don't have an immediate use case for DirectoryLink - FileLinks is what we would want users to call for linking to a directory.
r8835 """FileLinks: Calling repr() functions as expected w/ alt formatter
Greg Caporaso
added tests of passing alternative formatter functions
r8632 """
td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
tf2 = NamedTemporaryFile(dir=td)
Greg Caporaso
added included_suffixes as an option to display formatters - previously it wasn't possible to pass these in if defining custom display formatters
r8802 def fake_formatter(dirname,fnames,included_suffixes):
Greg Caporaso
updating to make interface more intuitive now that we're not using os.path.walk
r8801 return ["hello","world"]
Greg Caporaso
added tests of passing alternative formatter functions
r8632 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
actual = repr(fl)
actual = actual.split('\n')
actual.sort()
expected = ["hello","world"]
expected.sort()
# We compare the sorted list of links here as that's more reliable
nt.assert_equal(actual,expected)
Greg Caporaso
added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency.
r8836
def test_error_on_file_to_FileLinks():
"""FileLinks: Raises error when passed file
"""
td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
nt.assert_raises(ValueError,display.FileLinks,tf1.name)
Greg Caporaso
added tests of repr()
r8444