test_history.py
114 lines
| 4.4 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r3449 | # coding: utf-8 | ||
Satrajit Ghosh
|
r3240 | """Tests for the IPython tab-completion machinery. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Module imports | ||||
#----------------------------------------------------------------------------- | ||||
# stdlib | ||||
import os | ||||
import sys | ||||
import unittest | ||||
MinRK
|
r4487 | from datetime import datetime | ||
Satrajit Ghosh
|
r3240 | # 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 | |||
Thomas Kluyver
|
r3458 | def setUp(): | ||
Thomas Kluyver
|
r3447 | nt.assert_equal(sys.getdefaultencoding(), "ascii") | ||
Thomas Kluyver
|
r3458 | |||
def test_history(): | ||||
Satrajit Ghosh
|
r3240 | ip = get_ipython() | ||
with TemporaryDirectory() as tmpdir: | ||||
hist_manager_ori = ip.history_manager | ||||
Thomas Kluyver
|
r3715 | hist_file = os.path.join(tmpdir, 'history.sqlite') | ||
Satrajit Ghosh
|
r3240 | try: | ||
Thomas Kluyver
|
r3715 | ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file) | ||
Thomas Kluyver
|
r3450 | hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"] | ||
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
|
r3741 | ip.history_manager.output_hist_reprs[3] = "spam" | ||
Thomas Kluyver
|
r3415 | ip.history_manager.store_output(3) | ||
Thomas Kluyver
|
r3400 | |||
Thomas Kluyver
|
r3396 | nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist) | ||
Thomas Kluyver
|
r3715 | |||
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) | ||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.get_range(start=1, stop=4) | ||
Thomas Kluyver
|
r3396 | nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds)) | ||
# Previous session: | ||||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.get_range(-1, 1, 4) | ||
Thomas Kluyver
|
r3396 | nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) | ||
Thomas Kluyver
|
r3383 | |||
Thomas Kluyver
|
r3400 | # Check get_hist_tail | ||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.get_tail(4, output=True, | ||
Thomas Kluyver
|
r3420 | include_latest=True) | ||
Thomas Kluyver
|
r3741 | expected = [(1, 3, (hist[-1], "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) | ||||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.get_tail(2) | ||
Thomas Kluyver
|
r3420 | expected = [(2, 1, newcmds[0]), | ||
(2, 2, newcmds[1])] | ||||
nt.assert_equal(list(gothist), expected) | ||||
Thomas Kluyver
|
r3400 | # Check get_hist_search | ||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.search("*test*") | ||
Thomas Kluyver
|
r3400 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) | ||
Thomas Kluyver
|
r3435 | gothist = ip.history_manager.search("b*", output=True) | ||
Thomas Kluyver
|
r3741 | nt.assert_equal(list(gothist), [(1,3,(hist[2],"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
|
r3454 | nt.assert_equal(testfile.read().decode("utf-8"), | ||
"# coding: utf-8\n" + "\n".join(hist)) | ||||
Thomas Kluyver
|
r3438 | |||
# Duplicate line numbers - check that it doesn't crash, and | ||||
# gets a new session | ||||
ip.history_manager.store_inputs(1, "rogue") | ||||
Thomas Kluyver
|
r3715 | ip.history_manager.writeout_cache() | ||
Thomas Kluyver
|
r3438 | nt.assert_equal(ip.history_manager.session_number, 3) | ||
Satrajit Ghosh
|
r3240 | finally: | ||
# Restore history manager | ||||
ip.history_manager = hist_manager_ori | ||||
Thomas Kluyver
|
r3396 | |||
Thomas Kluyver
|
r3447 | |||
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) | ||||
Thomas Kluyver
|
r3420 | |||
def test_magic_rerun(): | ||||
"""Simple test for %rerun (no args -> rerun last line)""" | ||||
ip = get_ipython() | ||||
ip.run_cell("a = 10") | ||||
ip.run_cell("a += 1") | ||||
nt.assert_equal(ip.user_ns["a"], 11) | ||||
ip.run_cell("%rerun") | ||||
nt.assert_equal(ip.user_ns["a"], 12) | ||||
MinRK
|
r4487 | |||
def test_timestamp_type(): | ||||
ip = get_ipython() | ||||
info = ip.history_manager.get_session_info() | ||||
nt.assert_true(isinstance(info[1], datetime)) | ||||