##// END OF EJS Templates
[core][tests][history] Remove nose
Samuel Gaist -
Show More
@@ -1,223 +1,218 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for the IPython tab-completion machinery.
2 """Tests for the IPython tab-completion machinery.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Module imports
5 # Module imports
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 # stdlib
8 # stdlib
9 import io
9 import io
10 from pathlib import Path
10 from pathlib import Path
11 import sys
11 import sys
12 import tempfile
12 import tempfile
13 from datetime import datetime
13 from datetime import datetime
14 import sqlite3
14 import sqlite3
15
15
16 # third party
17 import nose.tools as nt
18
19 # our own packages
16 # our own packages
20 from traitlets.config.loader import Config
17 from traitlets.config.loader import Config
21 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.utils.tempdir import TemporaryDirectory
22 from IPython.core.history import HistoryManager, extract_hist_ranges
19 from IPython.core.history import HistoryManager, extract_hist_ranges
23 from IPython.testing.decorators import skipif
20 from IPython.testing.decorators import skipif
24
21
25 def test_proper_default_encoding():
22 def test_proper_default_encoding():
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
23 assert sys.getdefaultencoding() == "utf-8"
27
24
28 @skipif(sqlite3.sqlite_version_info > (3,24,0))
25 @skipif(sqlite3.sqlite_version_info > (3,24,0))
29 def test_history():
26 def test_history():
30 ip = get_ipython()
27 ip = get_ipython()
31 with TemporaryDirectory() as tmpdir:
28 with TemporaryDirectory() as tmpdir:
32 tmp_path = Path(tmpdir)
29 tmp_path = Path(tmpdir)
33 hist_manager_ori = ip.history_manager
30 hist_manager_ori = ip.history_manager
34 hist_file = tmp_path / "history.sqlite"
31 hist_file = tmp_path / "history.sqlite"
35 try:
32 try:
36 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
33 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
37 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
34 hist = ["a=1", "def f():\n test = 1\n return test", "b='€Æ¾÷ß'"]
38 for i, h in enumerate(hist, start=1):
35 for i, h in enumerate(hist, start=1):
39 ip.history_manager.store_inputs(i, h)
36 ip.history_manager.store_inputs(i, h)
40
37
41 ip.history_manager.db_log_output = True
38 ip.history_manager.db_log_output = True
42 # Doesn't match the input, but we'll just check it's stored.
39 # Doesn't match the input, but we'll just check it's stored.
43 ip.history_manager.output_hist_reprs[3] = "spam"
40 ip.history_manager.output_hist_reprs[3] = "spam"
44 ip.history_manager.store_output(3)
41 ip.history_manager.store_output(3)
45
42
46 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
43 assert ip.history_manager.input_hist_raw == [""] + hist
47
44
48 # Detailed tests for _get_range_session
45 # Detailed tests for _get_range_session
49 grs = ip.history_manager._get_range_session
46 grs = ip.history_manager._get_range_session
50 nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1])))
47 assert list(grs(start=2, stop=-1)) == list(zip([0], [2], hist[1:-1]))
51 nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:])))
48 assert list(grs(start=-2)) == list(zip([0, 0], [2, 3], hist[-2:]))
52 nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))))
49 assert list(grs(output=True)) == list(
50 zip([0, 0, 0], [1, 2, 3], zip(hist, [None, None, "spam"]))
51 )
53
52
54 # Check whether specifying a range beyond the end of the current
53 # Check whether specifying a range beyond the end of the current
55 # session results in an error (gh-804)
54 # session results in an error (gh-804)
56 ip.magic('%hist 2-500')
55 ip.magic('%hist 2-500')
57
56
58 # Check that we can write non-ascii characters to a file
57 # Check that we can write non-ascii characters to a file
59 ip.magic("%%hist -f %s" % (tmp_path / "test1"))
58 ip.magic("%%hist -f %s" % (tmp_path / "test1"))
60 ip.magic("%%hist -pf %s" % (tmp_path / "test2"))
59 ip.magic("%%hist -pf %s" % (tmp_path / "test2"))
61 ip.magic("%%hist -nf %s" % (tmp_path / "test3"))
60 ip.magic("%%hist -nf %s" % (tmp_path / "test3"))
62 ip.magic("%%save %s 1-10" % (tmp_path / "test4"))
61 ip.magic("%%save %s 1-10" % (tmp_path / "test4"))
63
62
64 # New session
63 # New session
65 ip.history_manager.reset()
64 ip.history_manager.reset()
66 newcmds = [u"z=5",
65 newcmds = ["z=5", "class X(object):\n pass", "k='p'", "z=5"]
67 u"class X(object):\n pass",
68 u"k='p'",
69 u"z=5"]
70 for i, cmd in enumerate(newcmds, start=1):
66 for i, cmd in enumerate(newcmds, start=1):
71 ip.history_manager.store_inputs(i, cmd)
67 ip.history_manager.store_inputs(i, cmd)
72 gothist = ip.history_manager.get_range(start=1, stop=4)
68 gothist = ip.history_manager.get_range(start=1, stop=4)
73 nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds)))
69 assert list(gothist) == list(zip([0, 0, 0], [1, 2, 3], newcmds))
74 # Previous session:
70 # Previous session:
75 gothist = ip.history_manager.get_range(-1, 1, 4)
71 gothist = ip.history_manager.get_range(-1, 1, 4)
76 nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist)))
72 assert list(gothist) == list(zip([1, 1, 1], [1, 2, 3], hist))
77
73
78 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
74 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
79
75
80 # Check get_hist_tail
76 # Check get_hist_tail
81 gothist = ip.history_manager.get_tail(5, output=True,
77 gothist = ip.history_manager.get_tail(5, output=True,
82 include_latest=True)
78 include_latest=True)
83 expected = [(1, 3, (hist[-1], "spam"))] \
79 expected = [(1, 3, (hist[-1], "spam"))] \
84 + [(s, n, (c, None)) for (s, n, c) in newhist]
80 + [(s, n, (c, None)) for (s, n, c) in newhist]
85 nt.assert_equal(list(gothist), expected)
81 assert list(gothist) == expected
86
82
87 gothist = ip.history_manager.get_tail(2)
83 gothist = ip.history_manager.get_tail(2)
88 expected = newhist[-3:-1]
84 expected = newhist[-3:-1]
89 nt.assert_equal(list(gothist), expected)
85 assert list(gothist) == expected
90
86
91 # Check get_hist_search
87 # Check get_hist_search
92
88
93 gothist = ip.history_manager.search("*test*")
89 gothist = ip.history_manager.search("*test*")
94 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
90 assert list(gothist) == [(1, 2, hist[1])]
95
91
96 gothist = ip.history_manager.search("*=*")
92 gothist = ip.history_manager.search("*=*")
97 nt.assert_equal(list(gothist),
93 assert list(gothist) == [
98 [(1, 1, hist[0]),
94 (1, 1, hist[0]),
99 (1, 2, hist[1]),
95 (1, 2, hist[1]),
100 (1, 3, hist[2]),
96 (1, 3, hist[2]),
101 newhist[0],
97 newhist[0],
102 newhist[2],
98 newhist[2],
103 newhist[3]])
99 newhist[3],
100 ]
104
101
105 gothist = ip.history_manager.search("*=*", n=4)
102 gothist = ip.history_manager.search("*=*", n=4)
106 nt.assert_equal(list(gothist),
103 assert list(gothist) == [
107 [(1, 3, hist[2]),
104 (1, 3, hist[2]),
108 newhist[0],
105 newhist[0],
109 newhist[2],
106 newhist[2],
110 newhist[3]])
107 newhist[3],
108 ]
111
109
112 gothist = ip.history_manager.search("*=*", unique=True)
110 gothist = ip.history_manager.search("*=*", unique=True)
113 nt.assert_equal(list(gothist),
111 assert list(gothist) == [
114 [(1, 1, hist[0]),
112 (1, 1, hist[0]),
115 (1, 2, hist[1]),
113 (1, 2, hist[1]),
116 (1, 3, hist[2]),
114 (1, 3, hist[2]),
117 newhist[2],
115 newhist[2],
118 newhist[3]])
116 newhist[3],
117 ]
119
118
120 gothist = ip.history_manager.search("*=*", unique=True, n=3)
119 gothist = ip.history_manager.search("*=*", unique=True, n=3)
121 nt.assert_equal(list(gothist),
120 assert list(gothist) == [(1, 3, hist[2]), newhist[2], newhist[3]]
122 [(1, 3, hist[2]),
123 newhist[2],
124 newhist[3]])
125
121
126 gothist = ip.history_manager.search("b*", output=True)
122 gothist = ip.history_manager.search("b*", output=True)
127 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
123 assert list(gothist) == [(1, 3, (hist[2], "spam"))]
128
124
129 # Cross testing: check that magic %save can get previous session.
125 # Cross testing: check that magic %save can get previous session.
130 testfilename = (tmp_path / "test.py").resolve()
126 testfilename = (tmp_path / "test.py").resolve()
131 ip.magic("save " + str(testfilename) + " ~1/1-3")
127 ip.magic("save " + str(testfilename) + " ~1/1-3")
132 with io.open(testfilename, encoding='utf-8') as testfile:
128 with io.open(testfilename, encoding="utf-8") as testfile:
133 nt.assert_equal(testfile.read(),
129 assert testfile.read() == "# coding: utf-8\n" + "\n".join(hist) + "\n"
134 u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")
135
130
136 # Duplicate line numbers - check that it doesn't crash, and
131 # Duplicate line numbers - check that it doesn't crash, and
137 # gets a new session
132 # gets a new session
138 ip.history_manager.store_inputs(1, "rogue")
133 ip.history_manager.store_inputs(1, "rogue")
139 ip.history_manager.writeout_cache()
134 ip.history_manager.writeout_cache()
140 nt.assert_equal(ip.history_manager.session_number, 3)
135 assert ip.history_manager.session_number == 3
141 finally:
136 finally:
142 # Ensure saving thread is shut down before we try to clean up the files
137 # Ensure saving thread is shut down before we try to clean up the files
143 ip.history_manager.save_thread.stop()
138 ip.history_manager.save_thread.stop()
144 # Forcibly close database rather than relying on garbage collection
139 # Forcibly close database rather than relying on garbage collection
145 ip.history_manager.db.close()
140 ip.history_manager.db.close()
146 # Restore history manager
141 # Restore history manager
147 ip.history_manager = hist_manager_ori
142 ip.history_manager = hist_manager_ori
148
143
149
144
150 def test_extract_hist_ranges():
145 def test_extract_hist_ranges():
151 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
146 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
152 expected = [(0, 1, 2), # 0 == current session
147 expected = [(0, 1, 2), # 0 == current session
153 (2, 3, 4),
148 (2, 3, 4),
154 (-4, 5, 7),
149 (-4, 5, 7),
155 (-4, 7, 10),
150 (-4, 7, 10),
156 (-9, 2, None), # None == to end
151 (-9, 2, None), # None == to end
157 (-8, 1, None),
152 (-8, 1, None),
158 (-7, 1, 6),
153 (-7, 1, 6),
159 (-10, 1, None)]
154 (-10, 1, None)]
160 actual = list(extract_hist_ranges(instr))
155 actual = list(extract_hist_ranges(instr))
161 nt.assert_equal(actual, expected)
156 assert actual == expected
162
157
163
158
164 def test_extract_hist_ranges_empty_str():
159 def test_extract_hist_ranges_empty_str():
165 instr = ""
160 instr = ""
166 expected = [(0, 1, None)] # 0 == current session, None == to end
161 expected = [(0, 1, None)] # 0 == current session, None == to end
167 actual = list(extract_hist_ranges(instr))
162 actual = list(extract_hist_ranges(instr))
168 nt.assert_equal(actual, expected)
163 assert actual == expected
169
164
170
165
171 def test_magic_rerun():
166 def test_magic_rerun():
172 """Simple test for %rerun (no args -> rerun last line)"""
167 """Simple test for %rerun (no args -> rerun last line)"""
173 ip = get_ipython()
168 ip = get_ipython()
174 ip.run_cell("a = 10", store_history=True)
169 ip.run_cell("a = 10", store_history=True)
175 ip.run_cell("a += 1", store_history=True)
170 ip.run_cell("a += 1", store_history=True)
176 nt.assert_equal(ip.user_ns["a"], 11)
171 assert ip.user_ns["a"] == 11
177 ip.run_cell("%rerun", store_history=True)
172 ip.run_cell("%rerun", store_history=True)
178 nt.assert_equal(ip.user_ns["a"], 12)
173 assert ip.user_ns["a"] == 12
179
174
180 def test_timestamp_type():
175 def test_timestamp_type():
181 ip = get_ipython()
176 ip = get_ipython()
182 info = ip.history_manager.get_session_info()
177 info = ip.history_manager.get_session_info()
183 nt.assert_true(isinstance(info[1], datetime))
178 assert isinstance(info[1], datetime)
184
179
185 def test_hist_file_config():
180 def test_hist_file_config():
186 cfg = Config()
181 cfg = Config()
187 tfile = tempfile.NamedTemporaryFile(delete=False)
182 tfile = tempfile.NamedTemporaryFile(delete=False)
188 cfg.HistoryManager.hist_file = Path(tfile.name)
183 cfg.HistoryManager.hist_file = Path(tfile.name)
189 try:
184 try:
190 hm = HistoryManager(shell=get_ipython(), config=cfg)
185 hm = HistoryManager(shell=get_ipython(), config=cfg)
191 nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file)
186 assert hm.hist_file == cfg.HistoryManager.hist_file
192 finally:
187 finally:
193 try:
188 try:
194 Path(tfile.name).unlink()
189 Path(tfile.name).unlink()
195 except OSError:
190 except OSError:
196 # same catch as in testing.tools.TempFileMixin
191 # same catch as in testing.tools.TempFileMixin
197 # On Windows, even though we close the file, we still can't
192 # On Windows, even though we close the file, we still can't
198 # delete it. I have no clue why
193 # delete it. I have no clue why
199 pass
194 pass
200
195
201 def test_histmanager_disabled():
196 def test_histmanager_disabled():
202 """Ensure that disabling the history manager doesn't create a database."""
197 """Ensure that disabling the history manager doesn't create a database."""
203 cfg = Config()
198 cfg = Config()
204 cfg.HistoryAccessor.enabled = False
199 cfg.HistoryAccessor.enabled = False
205
200
206 ip = get_ipython()
201 ip = get_ipython()
207 with TemporaryDirectory() as tmpdir:
202 with TemporaryDirectory() as tmpdir:
208 hist_manager_ori = ip.history_manager
203 hist_manager_ori = ip.history_manager
209 hist_file = Path(tmpdir) / "history.sqlite"
204 hist_file = Path(tmpdir) / "history.sqlite"
210 cfg.HistoryManager.hist_file = hist_file
205 cfg.HistoryManager.hist_file = hist_file
211 try:
206 try:
212 ip.history_manager = HistoryManager(shell=ip, config=cfg)
207 ip.history_manager = HistoryManager(shell=ip, config=cfg)
213 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
208 hist = ["a=1", "def f():\n test = 1\n return test", "b='€Æ¾÷ß'"]
214 for i, h in enumerate(hist, start=1):
209 for i, h in enumerate(hist, start=1):
215 ip.history_manager.store_inputs(i, h)
210 ip.history_manager.store_inputs(i, h)
216 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
211 assert ip.history_manager.input_hist_raw == [""] + hist
217 ip.history_manager.reset()
212 ip.history_manager.reset()
218 ip.history_manager.end_session()
213 ip.history_manager.end_session()
219 finally:
214 finally:
220 ip.history_manager = hist_manager_ori
215 ip.history_manager = hist_manager_ori
221
216
222 # hist_file should not be created
217 # hist_file should not be created
223 nt.assert_false(hist_file.exists())
218 assert hist_file.exists() is False
General Comments 0
You need to be logged in to leave comments. Login now