MCPcopy Create free account
hub / github.com/ipython/ipython / test_history

Function test_history

IPython/core/tests/test_history.py:29–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27
28@skipif(sqlite3.sqlite_version_info > (3,24,0))
29def test_history():
30 ip = get_ipython()
31 with TemporaryDirectory() as tmpdir:
32 hist_manager_ori = ip.history_manager
33 hist_file = os.path.join(tmpdir, 'history.sqlite')
34 try:
35 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
36 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
37 for i, h in enumerate(hist, start=1):
38 ip.history_manager.store_inputs(i, h)
39
40 ip.history_manager.db_log_output = True
41 # Doesn't match the input, but we'll just check it's stored.
42 ip.history_manager.output_hist_reprs[3] = "spam"
43 ip.history_manager.store_output(3)
44
45 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
46
47 # Detailed tests for _get_range_session
48 grs = ip.history_manager._get_range_session
49 nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1])))
50 nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:])))
51 nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))))
52
53 # Check whether specifying a range beyond the end of the current
54 # session results in an error (gh-804)
55 ip.magic('%hist 2-500')
56
57 # Check that we can write non-ascii characters to a file
58 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
59 ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
60 ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
61 ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
62
63 # New session
64 ip.history_manager.reset()
65 newcmds = [u"z=5",
66 u"class X(object):\n pass",
67 u"k='p'",
68 u"z=5"]
69 for i, cmd in enumerate(newcmds, start=1):
70 ip.history_manager.store_inputs(i, cmd)
71 gothist = ip.history_manager.get_range(start=1, stop=4)
72 nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds)))
73 # Previous session:
74 gothist = ip.history_manager.get_range(-1, 1, 4)
75 nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist)))
76
77 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
78
79 # Check get_hist_tail
80 gothist = ip.history_manager.get_tail(5, output=True,
81 include_latest=True)
82 expected = [(1, 3, (hist[-1], "spam"))] \
83 + [(s, n, (c, None)) for (s, n, c) in newhist]
84 nt.assert_equal(list(gothist), expected)
85
86 gothist = ip.history_manager.get_tail(2)

Callers

nothing calls this directly

Calls 13

HistoryManagerClass · 0.90
store_inputsMethod · 0.80
store_outputMethod · 0.80
readMethod · 0.80
stopMethod · 0.80
get_ipythonFunction · 0.50
magicMethod · 0.45
resetMethod · 0.45
get_rangeMethod · 0.45
get_tailMethod · 0.45
searchMethod · 0.45
writeout_cacheMethod · 0.45

Tested by

no test coverage detected