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