Show More
@@ -13,9 +13,6 b' 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 | |
@@ -23,7 +20,7 b' 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 |
|
|
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(): | |
@@ -34,7 +31,7 b' def test_history():' | |||||
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 = [ |
|
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 | |||
@@ -43,13 +40,15 b' def test_history():' | |||||
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 |
|
|
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 |
|
|
47 | assert list(grs(start=2, stop=-1)) == list(zip([0], [2], hist[1:-1])) | |
51 |
|
|
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) | |
@@ -63,17 +62,14 b' def test_history():' | |||||
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 |
|
|
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 |
|
|
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 | |||
@@ -82,62 +78,61 b' def test_history():' | |||||
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 |
|
|
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 |
|
|
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 |
|
|
90 | assert list(gothist) == [(1, 2, hist[1])] | |
95 |
|
91 | |||
96 | gothist = ip.history_manager.search("*=*") |
|
92 | gothist = ip.history_manager.search("*=*") | |
97 |
|
|
93 | assert list(gothist) == [ | |
98 |
|
|
94 | (1, 1, hist[0]), | |
99 |
|
|
95 | (1, 2, hist[1]), | |
100 |
|
|
96 | (1, 3, hist[2]), | |
101 |
|
|
97 | newhist[0], | |
102 |
|
|
98 | newhist[2], | |
103 |
|
|
99 | newhist[3], | |
|
100 | ] | |||
104 |
|
101 | |||
105 | gothist = ip.history_manager.search("*=*", n=4) |
|
102 | gothist = ip.history_manager.search("*=*", n=4) | |
106 |
|
|
103 | assert list(gothist) == [ | |
107 |
|
|
104 | (1, 3, hist[2]), | |
108 |
|
|
105 | newhist[0], | |
109 |
|
|
106 | newhist[2], | |
110 |
|
|
107 | newhist[3], | |
|
108 | ] | |||
111 |
|
109 | |||
112 | gothist = ip.history_manager.search("*=*", unique=True) |
|
110 | gothist = ip.history_manager.search("*=*", unique=True) | |
113 |
|
|
111 | assert list(gothist) == [ | |
114 |
|
|
112 | (1, 1, hist[0]), | |
115 |
|
|
113 | (1, 2, hist[1]), | |
116 |
|
|
114 | (1, 3, hist[2]), | |
117 |
|
|
115 | newhist[2], | |
118 |
|
|
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 |
|
|
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= |
|
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 |
|
|
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() | |
@@ -158,14 +153,14 b' def test_extract_hist_ranges():' | |||||
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 |
|
|
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 |
|
|
163 | assert actual == expected | |
169 |
|
164 | |||
170 |
|
165 | |||
171 | def test_magic_rerun(): |
|
166 | def test_magic_rerun(): | |
@@ -173,14 +168,14 b' def test_magic_rerun():' | |||||
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 |
|
|
171 | assert ip.user_ns["a"] == 11 | |
177 | ip.run_cell("%rerun", store_history=True) |
|
172 | ip.run_cell("%rerun", store_history=True) | |
178 |
|
|
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 |
|
|
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() | |
@@ -188,7 +183,7 b' def test_hist_file_config():' | |||||
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 |
|
|
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() | |
@@ -210,14 +205,14 b' def test_histmanager_disabled():' | |||||
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 = [ |
|
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 |
|
|
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 |
|
|
218 | assert hist_file.exists() is False |
General Comments 0
You need to be logged in to leave comments.
Login now