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