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