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