##// END OF EJS Templates
Backport PR #14010: try to fix tbcode
Backport PR #14010: try to fix tbcode

File last commit:

r27764:aefe51c6
r28236:32388851
Show More
test_history.py
306 lines | 11.4 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
Thomas Kluyver
Remove py3compat.open in favour of io.open
r17299 import io
Satrajit Ghosh
History refactored and saved to json file...
r3240 import sys
MinRK
allow setting HistoryManager.hist_file with config...
r5233 import tempfile
MinRK
add get_session_info to HistoryManager for querying session table...
r4487 from datetime import datetime
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509 from pathlib import Path
MinRK
allow setting HistoryManager.hist_file with config...
r5233
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509 from tempfile import TemporaryDirectory
Satrajit Ghosh
History refactored and saved to json file...
r3240 # our own packages
Min RK
update dependency imports...
r21253 from traitlets.config.loader import Config
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509
Aleksey Bogdanov
Fix HistoryAccessor.get_tail bug (#13666)...
r27707 from IPython.core.history import HistoryAccessor, HistoryManager, extract_hist_ranges
Satrajit Ghosh
History refactored and saved to json file...
r3240
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509
Matthias Bussonnier
Use module level setup and teardown compatible both nose and pytest....
r25082 def test_proper_default_encoding():
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert sys.getdefaultencoding() == "utf-8"
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:
Jakub Klus
Update history.py to use pathlib...
r26129 tmp_path = Path(tmpdir)
Satrajit Ghosh
History refactored and saved to json file...
r3240 hist_manager_ori = ip.history_manager
Jakub Klus
Apply darker formatting
r26131 hist_file = tmp_path / "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)
Samuel Gaist
[core][tests][history] Remove nose
r26896 hist = ["a=1", "def f():\n test = 1\n return test", "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
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert ip.history_manager.input_hist_raw == [""] + hist
luciana
Added skipif for sqlite3 version > 3.24.0
r24717
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
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(grs(start=2, stop=-1)) == list(zip([0], [2], hist[1:-1]))
assert list(grs(start=-2)) == list(zip([0, 0], [2, 3], hist[-2:]))
assert list(grs(output=True)) == list(
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)
Matthias Bussonnier
Update some deprecated ip.magic() to run_line-magic in tests
r27549 ip.run_line_magic("hist", "2-500")
luciana
Added skipif for sqlite3 version > 3.24.0
r24717
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
Matthias Bussonnier
Update some deprecated ip.magic() to run_line-magic in tests
r27549 ip.run_line_magic("hist", "-f %s" % (tmp_path / "test1"))
ip.run_line_magic("hist", "-pf %s" % (tmp_path / "test2"))
ip.run_line_magic("hist", "-nf %s" % (tmp_path / "test3"))
ip.run_line_magic("save", "%s 1-10" % (tmp_path / "test4"))
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Passing IPython.core tests.
r3396 # New session
ip.history_manager.reset()
Samuel Gaist
[core][tests][history] Remove nose
r26896 newcmds = ["z=5", "class X(object):\n pass", "k='p'", "z=5"]
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)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == list(zip([0, 0, 0], [1, 2, 3], newcmds))
Thomas Kluyver
Passing IPython.core tests.
r3396 # Previous session:
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.get_range(-1, 1, 4)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == list(zip([1, 1, 1], [1, 2, 3], hist))
Bernardo B. Marques
remove all trailling spaces
r4872
Takafumi Arakaki
Use start=1 for enumerate rather than doing i+1
r8785 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
Takafumi Arakaki
Add duplicated entry in test_history
r8779
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
Takafumi Arakaki
Add duplicated entry in test_history
r8779 gothist = ip.history_manager.get_tail(5, output=True,
Thomas Kluyver
Further refinements to history interfaces.
r3420 include_latest=True)
Takafumi Arakaki
Add duplicated entry in test_history
r8779 expected = [(1, 3, (hist[-1], "spam"))] \
+ [(s, n, (c, None)) for (s, n, c) in newhist]
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert 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)
Takafumi Arakaki
Add duplicated entry in test_history
r8779 expected = newhist[-3:-1]
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert 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
luciana
Added skipif for sqlite3 version > 3.24.0
r24717
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.search("*test*")
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == [(1, 2, hist[1])]
Takafumi Arakaki
Add a test for history search with unique=True
r8781
Takafumi Arakaki
Add tests for the new history seach behavior
r8400 gothist = ip.history_manager.search("*=*")
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == [
(1, 1, hist[0]),
(1, 2, hist[1]),
(1, 3, hist[2]),
newhist[0],
newhist[2],
newhist[3],
]
Takafumi Arakaki
Add a test for history search with unique=True
r8781
Takafumi Arakaki
Add duplicated entry in test_history
r8779 gothist = ip.history_manager.search("*=*", n=4)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == [
(1, 3, hist[2]),
newhist[0],
newhist[2],
newhist[3],
]
Takafumi Arakaki
Add a test for history search with unique=True
r8781
gothist = ip.history_manager.search("*=*", unique=True)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == [
(1, 1, hist[0]),
(1, 2, hist[1]),
(1, 3, hist[2]),
newhist[2],
newhist[3],
]
Takafumi Arakaki
Add a test for history search with unique=True
r8781
Takafumi Arakaki
Add a test for combination of n/unique arguments
r8782 gothist = ip.history_manager.search("*=*", unique=True, n=3)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert list(gothist) == [(1, 3, hist[2]), newhist[2], newhist[3]]
Takafumi Arakaki
Add a test for combination of n/unique arguments
r8782
Thomas Kluyver
Rename history retrieval methods, and improve docstrings.
r3435 gothist = ip.history_manager.search("b*", output=True)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert 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.
Jakub Klus
Apply darker formatting
r26131 testfilename = (tmp_path / "test.py").resolve()
Matthias Bussonnier
Update some deprecated ip.magic() to run_line-magic in tests
r27549 ip.run_line_magic("save", str(testfilename) + " ~1/1-3")
Samuel Gaist
[core][tests][history] Remove nose
r26896 with io.open(testfilename, encoding="utf-8") as testfile:
assert testfile.read() == "# coding: utf-8\n" + "\n".join(hist) + "\n"
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()
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert ip.history_manager.session_number == 3
Nikita Kniazev
Fix `history_manager.search(?, unique=True)` returning old entries
r27045
# Check that session and line values are not just max values
sessid, lineno, entry = newhist[-1]
assert lineno > 1
ip.history_manager.reset()
lineno = 1
ip.history_manager.store_inputs(lineno, entry)
gothist = ip.history_manager.search("*=*", unique=True)
hist = list(gothist)[-1]
assert sessid < hist[0]
assert hist[1:] == (lineno, entry)
Satrajit Ghosh
History refactored and saved to json file...
r3240 finally:
Thomas Kluyver
Ensure history tests release database before cleanup
r16768 # Ensure saving thread is shut down before we try to clean up the files
ip.history_manager.save_thread.stop()
Thomas Kluyver
Ensure history database is closed in test...
r16771 # Forcibly close database rather than relying on garbage collection
ip.history_manager.db.close()
Satrajit Ghosh
History refactored and saved to json file...
r3240 # 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():
Joon Ro
added ~session/noline case to test_extract_hist_ranges()...
r9797 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
Thomas Kluyver
Passing IPython.core tests.
r3396 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),
Joon Ro
added a missing comma
r9798 (-7, 1, 6),
Joon Ro
added ~session/noline case to test_extract_hist_ranges()...
r9797 (-10, 1, None)]
Thomas Kluyver
Passing IPython.core tests.
r3396 actual = list(extract_hist_ranges(instr))
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert actual == expected
Bernardo B. Marques
remove all trailling spaces
r4872
Blazej Michalik
Empty histrange means history of current session...
r26630
def test_extract_hist_ranges_empty_str():
Blazej Michalik
Darker
r26641 instr = ""
Blazej Michalik
Empty histrange means history of current session...
r26630 expected = [(0, 1, None)] # 0 == current session, None == to end
actual = list(extract_hist_ranges(instr))
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert actual == expected
Blazej Michalik
Empty histrange means history of current session...
r26630
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)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert ip.user_ns["a"] == 11
Thomas Kluyver
Change run_cell to not store history by default.
r4995 ip.run_cell("%rerun", store_history=True)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert 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()
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert 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)
Jakub Klus
Update history.py to use pathlib...
r26129 cfg.HistoryManager.hist_file = Path(tfile.name)
MinRK
use NamedTemporaryFile in hist_file_config test
r5242 try:
hm = HistoryManager(shell=get_ipython(), config=cfg)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert hm.hist_file == cfg.HistoryManager.hist_file
MinRK
use NamedTemporaryFile in hist_file_config test
r5242 finally:
MinRK
minor test fixes for win32
r5297 try:
Jakub Klus
Update history.py to use pathlib...
r26129 Path(tfile.name).unlink()
MinRK
minor test fixes for win32
r5297 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
Trevor Bekolay
Add test for disabling history manager
r22185 def test_histmanager_disabled():
"""Ensure that disabling the history manager doesn't create a database."""
cfg = Config()
cfg.HistoryAccessor.enabled = False
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
Jakub Klus
Apply darker formatting
r26131 hist_file = Path(tmpdir) / "history.sqlite"
Trevor Bekolay
Add test for disabling history manager
r22185 cfg.HistoryManager.hist_file = hist_file
try:
ip.history_manager = HistoryManager(shell=ip, config=cfg)
Samuel Gaist
[core][tests][history] Remove nose
r26896 hist = ["a=1", "def f():\n test = 1\n return test", "b='€Æ¾÷ß'"]
Trevor Bekolay
Add test for disabling history manager
r22185 for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert ip.history_manager.input_hist_raw == [""] + hist
Trevor Bekolay
Add test for disabling history manager
r22185 ip.history_manager.reset()
ip.history_manager.end_session()
finally:
ip.history_manager = hist_manager_ori
# hist_file should not be created
Samuel Gaist
[core][tests][history] Remove nose
r26896 assert hist_file.exists() is False
Aleksey Bogdanov
Fix HistoryAccessor.get_tail bug (#13666)...
r27707
Aleksey Bogdanov
Fix formatting issues
r27708
Aleksey Bogdanov
Fix HistoryAccessor.get_tail bug (#13666)...
r27707 def test_get_tail_session_awareness():
"""Test .get_tail() is:
Aleksey Bogdanov
Fix formatting issues
r27708 - session specific in HistoryManager
- session agnostic in HistoryAccessor
same for .get_last_session_id()
Aleksey Bogdanov
Fix HistoryAccessor.get_tail bug (#13666)...
r27707 """
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
hist_file = tmp_path / "history.sqlite"
get_source = lambda x: x[2]
Aleksey Bogdanov
wrap db before host dir is gone in new test
r27709 hm1 = None
hm2 = None
Aleksey Bogdanov
wrap db before host dir is gone in new test (v2)
r27710 ha = None
Aleksey Bogdanov
wrap db before host dir is gone in new test
r27709 try:
# hm1 creates a new session and adds history entries,
# ha catches up
hm1 = HistoryManager(shell=ip, hist_file=hist_file)
hm1_last_sid = hm1.get_last_session_id
ha = HistoryAccessor(hist_file=hist_file)
ha_last_sid = ha.get_last_session_id
hist1 = ["a=1", "b=1", "c=1"]
for i, h in enumerate(hist1 + [""], start=1):
hm1.store_inputs(i, h)
assert list(map(get_source, hm1.get_tail())) == hist1
assert list(map(get_source, ha.get_tail())) == hist1
sid1 = hm1_last_sid()
assert sid1 is not None
assert ha_last_sid() == sid1
# hm2 creates a new session and adds entries,
# ha catches up
hm2 = HistoryManager(shell=ip, hist_file=hist_file)
hm2_last_sid = hm2.get_last_session_id
hist2 = ["a=2", "b=2", "c=2"]
for i, h in enumerate(hist2 + [""], start=1):
hm2.store_inputs(i, h)
tail = hm2.get_tail(n=3)
assert list(map(get_source, tail)) == hist2
tail = ha.get_tail(n=3)
assert list(map(get_source, tail)) == hist2
sid2 = hm2_last_sid()
assert sid2 is not None
assert ha_last_sid() == sid2
assert sid2 != sid1
# but hm1 still maintains its point of reference
# and adding more entries to it doesn't change others
# immediate perspective
assert hm1_last_sid() == sid1
tail = hm1.get_tail(n=3)
assert list(map(get_source, tail)) == hist1
hist3 = ["a=3", "b=3", "c=3"]
for i, h in enumerate(hist3 + [""], start=5):
hm1.store_inputs(i, h)
tail = hm1.get_tail(n=7)
assert list(map(get_source, tail)) == hist1 + [""] + hist3
tail = hm2.get_tail(n=3)
assert list(map(get_source, tail)) == hist2
tail = ha.get_tail(n=3)
assert list(map(get_source, tail)) == hist2
assert hm1_last_sid() == sid1
assert hm2_last_sid() == sid2
assert ha_last_sid() == sid2
finally:
if hm1:
hm1.save_thread.stop()
Aleksey Bogdanov
wrap db before host dir is gone in new test (v2)
r27710 hm1.db.close()
Aleksey Bogdanov
wrap db before host dir is gone in new test
r27709 if hm2:
hm2.save_thread.stop()
Aleksey Bogdanov
wrap db before host dir is gone in new test (v2)
r27710 hm2.db.close()
if ha:
ha.db.close()