test_history.py
94 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
Satrajit Ghosh
|
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
|
r3396 | from IPython.core.history import HistoryManager, extract_hist_ranges | ||
Satrajit Ghosh
|
r3240 | |||
def test_history(): | ||||
ip = get_ipython() | ||||
with TemporaryDirectory() as tmpdir: | ||||
#tmpdir = '/software/temp' | ||||
Thomas Kluyver
|
r3396 | histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite')) | ||
Satrajit Ghosh
|
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
|
r3396 | ip.history_manager = HistoryManager(shell=ip) | ||
Satrajit Ghosh
|
r3240 | ip.history_manager.hist_file = histfile | ||
Thomas Kluyver
|
r3396 | ip.history_manager.init_db() # Has to be called after changing file | ||
Thomas Kluyver
|
r3405 | ip.history_manager.reset() | ||
Satrajit Ghosh
|
r3240 | print 'test',histfile | ||
Thomas Kluyver
|
r3383 | hist = ['a=1', 'def f():\n test = 1\n return test', 'b=2'] | ||
Thomas Kluyver
|
r3396 | for i, h in enumerate(hist, start=1): | ||
ip.history_manager.store_inputs(i, h) | ||||
Thomas Kluyver
|
r3400 | ip.history_manager.db_log_output = True | ||
# Doesn't match the input, but we'll just check it's stored. | ||||
Thomas Kluyver
|
r3415 | ip.history_manager.output_hist[3].append("spam") | ||
ip.history_manager.store_output(3) | ||||
Thomas Kluyver
|
r3400 | |||
Thomas Kluyver
|
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
|
r3383 | |||
Thomas Kluyver
|
r3396 | # New session | ||
ip.history_manager.reset() | ||||
Thomas Kluyver
|
r3383 | newcmds = ["z=5","class X(object):\n pass", "k='p'"] | ||
Thomas Kluyver
|
r3400 | for i, cmd in enumerate(newcmds, start=1): | ||
Thomas Kluyver
|
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
|
r3383 | |||
Thomas Kluyver
|
r3400 | # Check get_hist_tail | ||
gothist = ip.history_manager.get_hist_tail(4, output=True) | ||||
Thomas Kluyver
|
r3415 | expected = [(1, 3, (hist[-1], [repr("spam")])), | ||
Thomas Kluyver
|
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
|
r3415 | nt.assert_equal(list(gothist), [(1,3,(hist[2],[repr("spam")]))] ) | ||
Thomas Kluyver
|
r3400 | |||
Thomas Kluyver
|
r3396 | # Cross testing: check that magic %save can get previous session. | ||
Thomas Kluyver
|
r3383 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) | ||
Thomas Kluyver
|
r3396 | ip.magic_save(testfilename + " ~1/1-3") | ||
Thomas Kluyver
|
r3383 | testfile = open(testfilename, "r") | ||
Thomas Kluyver
|
r3396 | nt.assert_equal(testfile.read(), "\n".join(hist)) | ||
Satrajit Ghosh
|
r3240 | finally: | ||
# Restore history manager | ||||
ip.history_manager = hist_manager_ori | ||||
Thomas Kluyver
|
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) | ||||