##// END OF EJS Templates
Initial fix for magic %rep function (was broken by new history system). Needs a bit of further thought.
Initial fix for magic %rep function (was broken by new history system). Needs a bit of further thought.

File last commit:

r3415:498d9f55
r3416:5fd0b6bb
Show More
test_history.py
94 lines | 3.9 KiB | text/x-python | PythonLexer
Satrajit Ghosh
History refactored and saved to json file...
r3240 """Tests for the IPython tab-completion machinery.
"""
#-----------------------------------------------------------------------------
# Module imports
#-----------------------------------------------------------------------------
# stdlib
import os
import sys
import unittest
# third party
import nose.tools as nt
# our own packages
from IPython.utils.tempdir import TemporaryDirectory
Thomas Kluyver
Passing IPython.core tests.
r3396 from IPython.core.history import HistoryManager, extract_hist_ranges
Satrajit Ghosh
History refactored and saved to json file...
r3240
def test_history():
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
#tmpdir = '/software/temp'
Thomas Kluyver
Passing IPython.core tests.
r3396 histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
Satrajit Ghosh
History refactored and saved to json file...
r3240 # Ensure that we restore the history management that we mess with in
# this test doesn't affect the IPython instance used by the test suite
# beyond this test.
hist_manager_ori = ip.history_manager
try:
Thomas Kluyver
Passing IPython.core tests.
r3396 ip.history_manager = HistoryManager(shell=ip)
Satrajit Ghosh
History refactored and saved to json file...
r3240 ip.history_manager.hist_file = histfile
Thomas Kluyver
Passing IPython.core tests.
r3396 ip.history_manager.init_db() # Has to be called after changing file
Thomas Kluyver
Store history sessions in table with start and end time, number of commands, and name/remark.
r3405 ip.history_manager.reset()
Satrajit Ghosh
History refactored and saved to json file...
r3240 print 'test',histfile
Thomas Kluyver
Add test code for history session offset.
r3383 hist = ['a=1', 'def f():\n test = 1\n return test', 'b=2']
Thomas Kluyver
Passing IPython.core tests.
r3396 for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
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
Allow history to store multiple outputs for a single input line.
r3415 ip.history_manager.output_hist[3].append("spam")
ip.history_manager.store_output(3)
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400
Thomas Kluyver
Passing IPython.core tests.
r3396 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
# Check lines were written to DB
c = ip.history_manager.db.execute("SELECT source_raw FROM history")
nt.assert_equal([x for x, in c], hist)
Thomas Kluyver
Add test code for history session offset.
r3383
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)
gothist = ip.history_manager.get_history(start=1, stop=4)
nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
# Previous session:
gothist = ip.history_manager.get_history(-1, 1, 4)
nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
Thomas Kluyver
Add test code for history session offset.
r3383
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
gothist = ip.history_manager.get_hist_tail(4, output=True)
Thomas Kluyver
Allow history to store multiple outputs for a single input line.
r3415 expected = [(1, 3, (hist[-1], [repr("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)
# Check get_hist_search
gothist = ip.history_manager.get_hist_search("*test*")
nt.assert_equal(list(gothist), [(1,2,hist[1])] )
gothist = ip.history_manager.get_hist_search("b*", output=True)
Thomas Kluyver
Allow history to store multiple outputs for a single input line.
r3415 nt.assert_equal(list(gothist), [(1,3,(hist[2],[repr("spam")]))] )
Thomas Kluyver
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
r3400
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
Add test code for history session offset.
r3383 testfile = open(testfilename, "r")
Thomas Kluyver
Passing IPython.core tests.
r3396 nt.assert_equal(testfile.read(), "\n".join(hist))
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
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)