##// END OF EJS Templates
Add test for list_profiles_in
Add test for list_profiles_in

File last commit:

r6075:5d306c09
r6204:c67c6c0f
Show More
test_history.py
151 lines | 6.0 KiB | text/x-python | PythonLexer
Thomas Kluyver
Add file encoding declarations to two tests.
r3449 # coding: utf-8
Satrajit Ghosh
History refactored and saved to json file...
r3240 """Tests for the IPython tab-completion machinery.
"""
#-----------------------------------------------------------------------------
# Module imports
#-----------------------------------------------------------------------------
# stdlib
import os
Thomas Kluyver
Add test for writing non-ascii characters to file with %hist.
r6074 import shutil
Satrajit Ghosh
History refactored and saved to json file...
r3240 import sys
MinRK
allow setting HistoryManager.hist_file with config...
r5233 import tempfile
Satrajit Ghosh
History refactored and saved to json file...
r3240 import unittest
MinRK
add get_session_info to HistoryManager for querying session table...
r4487 from datetime import datetime
MinRK
allow setting HistoryManager.hist_file with config...
r5233
Satrajit Ghosh
History refactored and saved to json file...
r3240 # third party
import nose.tools as nt
# our own packages
MinRK
allow setting HistoryManager.hist_file with config...
r5233 from IPython.config.loader import Config
Satrajit Ghosh
History refactored and saved to json file...
r3240 from IPython.utils.tempdir import TemporaryDirectory
Thomas Kluyver
Passing IPython.core tests.
r3396 from IPython.core.history import HistoryManager, extract_hist_ranges
Thomas Kluyver
Start using py3compat module.
r4731 from IPython.utils import py3compat
Satrajit Ghosh
History refactored and saved to json file...
r3240
Thomas Kluyver
Tweak code with suggestions from yesterday.
r3458 def setUp():
Thomas Kluyver
Start using py3compat module.
r4731 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
Thomas Kluyver
Tweak code with suggestions from yesterday.
r3458
def test_history():
Satrajit Ghosh
History refactored and saved to json file...
r3240 ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
Thomas Kluyver
Fix up tests.
r3715 hist_file = os.path.join(tmpdir, 'history.sqlite')
Satrajit Ghosh
History refactored and saved to json file...
r3240 try:
Thomas Kluyver
Fix up tests.
r3715 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
Thomas Kluyver
Fix tests for saving non-ascii history.
r6075 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
Thomas Kluyver
Passing IPython.core tests.
r3396 for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 ip.history_manager.db_log_output = True
# Doesn't match the input, but we'll just check it's stored.
Thomas Kluyver
History expects single output per cell, and doesn't use JSON to store them in the database.
r3741 ip.history_manager.output_hist_reprs[3] = "spam"
Thomas Kluyver
Allow history to store multiple outputs for a single input line.
r3415 ip.history_manager.store_output(3)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Passing IPython.core tests.
r3396 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
Thomas Kluyver
Add detailed tests for history _get_range_session method.
r4889
# Detailed tests for _get_range_session
grs = ip.history_manager._get_range_session
nt.assert_equal(list(grs(start=2,stop=-1)), zip([0], [2], hist[1:-1]))
nt.assert_equal(list(grs(start=-2)), zip([0,0], [2,3], hist[-2:]))
nt.assert_equal(list(grs(output=True)), zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))
Bernardo B. Marques
remove all trailling spaces
r4872
# Check whether specifying a range beyond the end of the current
Thomas Kluyver
Test that specifying a range beyond the end of the current session doesn't raise an error.
r4859 # session results in an error (gh-804)
ip.magic('%hist 2-500')
Thomas Kluyver
Add test for writing non-ascii characters to file with %hist.
r6074
# Check that we can write non-ascii characters to a file
Thomas Kluyver
Fix tests for saving non-ascii history.
r6075 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Passing IPython.core tests.
r3396 # New session
ip.history_manager.reset()
Thomas Kluyver
Add test code for history session offset.
r3383 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 for i, cmd in enumerate(newcmds, start=1):
Thomas Kluyver
Passing IPython.core tests.
r3396 ip.history_manager.store_inputs(i, cmd)
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.get_range(start=1, stop=4)
Thomas Kluyver
Passing IPython.core tests.
r3396 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
# Previous session:
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.get_range(-1, 1, 4)
Thomas Kluyver
Passing IPython.core tests.
r3396 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 # Check get_hist_tail
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.get_tail(4, output=True,
Thomas Kluyver
Further refinements to history interfaces.
r3420 include_latest=True)
Thomas Kluyver
History expects single output per cell, and doesn't use JSON to store them in the database.
r3741 expected = [(1, 3, (hist[-1], "spam")),
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 (2, 1, (newcmds[0], None)),
(2, 2, (newcmds[1], None)),
(2, 3, (newcmds[2], None)),]
nt.assert_equal(list(gothist), expected)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.get_tail(2)
Thomas Kluyver
Further refinements to history interfaces.
r3420 expected = [(2, 1, newcmds[0]),
(2, 2, newcmds[1])]
nt.assert_equal(list(gothist), expected)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 # Check get_hist_search
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.search("*test*")
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.search("b*", output=True)
Thomas Kluyver
History expects single output per cell, and doesn't use JSON to store them in the database.
r3741 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Passing IPython.core tests.
r3396 # Cross testing: check that magic %save can get previous session.
Thomas Kluyver
Add test code for history session offset.
r3383 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
Thomas Kluyver
Passing IPython.core tests.
r3396 ip.magic_save(testfilename + " ~1/1-3")
Thomas Kluyver
Fix almost all IPython.core tests for Python 3.
r4896 with py3compat.open(testfilename) as testfile:
nt.assert_equal(testfile.read(),
u"# coding: utf-8\n" + u"\n".join(hist))
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Add test code to make sure that history database errors caused by duplicate session/line numbers are handled correctly (don't crash, get new session).
r3438 # Duplicate line numbers - check that it doesn't crash, and
# gets a new session
ip.history_manager.store_inputs(1, "rogue")
Thomas Kluyver
Fix up tests.
r3715 ip.history_manager.writeout_cache()
Thomas Kluyver
Add test code to make sure that history database errors caused by duplicate session/line numbers are handled correctly (don't crash, get new session).
r3438 nt.assert_equal(ip.history_manager.session_number, 3)
Satrajit Ghosh
History refactored and saved to json file...
r3240 finally:
# Restore history manager
ip.history_manager = hist_manager_ori
Thomas Kluyver
Passing IPython.core tests.
r3396
Thomas Kluyver
Moved unicode tests into main test suite. Reverified that both fail if the fixes in previous commits are undone.
r3447
Thomas Kluyver
Passing IPython.core tests.
r3396 def test_extract_hist_ranges():
instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
expected = [(0, 1, 2), # 0 == current session
(2, 3, 4),
(-4, 5, 7),
(-4, 7, 10),
(-9, 2, None), # None == to end
(-8, 1, None),
(-7, 1, 6)]
actual = list(extract_hist_ranges(instr))
nt.assert_equal(actual, expected)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Further refinements to history interfaces.
r3420 def test_magic_rerun():
"""Simple test for %rerun (no args -> rerun last line)"""
ip = get_ipython()
Thomas Kluyver
Change run_cell to not store history by default.
r4995 ip.run_cell("a = 10", store_history=True)
ip.run_cell("a += 1", store_history=True)
Thomas Kluyver
Further refinements to history interfaces.
r3420 nt.assert_equal(ip.user_ns["a"], 11)
Thomas Kluyver
Change run_cell to not store history by default.
r4995 ip.run_cell("%rerun", store_history=True)
Thomas Kluyver
Further refinements to history interfaces.
r3420 nt.assert_equal(ip.user_ns["a"], 12)
MinRK
add get_session_info to HistoryManager for querying session table...
r4487
def test_timestamp_type():
ip = get_ipython()
info = ip.history_manager.get_session_info()
nt.assert_true(isinstance(info[1], datetime))
MinRK
allow setting HistoryManager.hist_file with config...
r5233
def test_hist_file_config():
cfg = Config()
MinRK
use NamedTemporaryFile in hist_file_config test
r5242 tfile = tempfile.NamedTemporaryFile(delete=False)
cfg.HistoryManager.hist_file = tfile.name
try:
hm = HistoryManager(shell=get_ipython(), config=cfg)
nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
finally:
MinRK
minor test fixes for win32
r5297 try:
os.remove(tfile.name)
except OSError:
# same catch as in testing.tools.TempFileMixin
# On Windows, even though we close the file, we still can't
# delete it. I have no clue why
pass
MinRK
allow setting HistoryManager.hist_file with config...
r5233