##// END OF EJS Templates
Added comments to testStrictDict about random test failure on OS X....
Brian Granger -
Show More
@@ -1,374 +1,377 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
2
3 """Test template for complete engine object"""
3 """Test template for complete engine object"""
4
4
5 __docformat__ = "restructuredtext en"
5 __docformat__ = "restructuredtext en"
6
6
7 #-------------------------------------------------------------------------------
7 #-------------------------------------------------------------------------------
8 # Copyright (C) 2008 The IPython Development Team
8 # Copyright (C) 2008 The IPython Development Team
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-------------------------------------------------------------------------------
12 #-------------------------------------------------------------------------------
13
13
14 #-------------------------------------------------------------------------------
14 #-------------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-------------------------------------------------------------------------------
16 #-------------------------------------------------------------------------------
17
17
18 import cPickle as pickle
18 import cPickle as pickle
19
19
20 from twisted.internet import defer, reactor
20 from twisted.internet import defer, reactor
21 from twisted.python import failure
21 from twisted.python import failure
22 from twisted.application import service
22 from twisted.application import service
23 import zope.interface as zi
23 import zope.interface as zi
24
24
25 from IPython.kernel import newserialized
25 from IPython.kernel import newserialized
26 from IPython.kernel import error
26 from IPython.kernel import error
27 from IPython.kernel.pickleutil import can, uncan
27 from IPython.kernel.pickleutil import can, uncan
28 import IPython.kernel.engineservice as es
28 import IPython.kernel.engineservice as es
29 from IPython.kernel.core.interpreter import Interpreter
29 from IPython.kernel.core.interpreter import Interpreter
30 from IPython.testing.parametric import Parametric, parametric
30 from IPython.testing.parametric import Parametric, parametric
31
31
32 #-------------------------------------------------------------------------------
32 #-------------------------------------------------------------------------------
33 # Tests
33 # Tests
34 #-------------------------------------------------------------------------------
34 #-------------------------------------------------------------------------------
35
35
36
36
37 # A sequence of valid commands run through execute
37 # A sequence of valid commands run through execute
38 validCommands = ['a=5',
38 validCommands = ['a=5',
39 'b=10',
39 'b=10',
40 'a=5; b=10; c=a+b',
40 'a=5; b=10; c=a+b',
41 'import math; 2.0*math.pi',
41 'import math; 2.0*math.pi',
42 """def f():
42 """def f():
43 result = 0.0
43 result = 0.0
44 for i in range(10):
44 for i in range(10):
45 result += i
45 result += i
46 """,
46 """,
47 'if 1<2: a=5',
47 'if 1<2: a=5',
48 """import time
48 """import time
49 time.sleep(0.1)""",
49 time.sleep(0.1)""",
50 """from math import cos;
50 """from math import cos;
51 x = 1.0*cos(0.5)""", # Semicolons lead to Discard ast nodes that should be discarded
51 x = 1.0*cos(0.5)""", # Semicolons lead to Discard ast nodes that should be discarded
52 """s = 1
52 """s = 1
53 s = set()
53 s = set()
54 """, # Trailing whitespace should be allowed.
54 """, # Trailing whitespace should be allowed.
55 """import math
55 """import math
56 math.cos(1.0)""", # Test a method call with a discarded return value
56 math.cos(1.0)""", # Test a method call with a discarded return value
57 """x=1.0234
57 """x=1.0234
58 a=5; b=10""", # Test an embedded semicolon
58 a=5; b=10""", # Test an embedded semicolon
59 """x=1.0234
59 """x=1.0234
60 a=5; b=10;""" # Test both an embedded and trailing semicolon
60 a=5; b=10;""" # Test both an embedded and trailing semicolon
61 ]
61 ]
62
62
63 # A sequence of commands that raise various exceptions
63 # A sequence of commands that raise various exceptions
64 invalidCommands = [('a=1/0',ZeroDivisionError),
64 invalidCommands = [('a=1/0',ZeroDivisionError),
65 ('print v',NameError),
65 ('print v',NameError),
66 ('l=[];l[0]',IndexError),
66 ('l=[];l[0]',IndexError),
67 ("d={};d['a']",KeyError),
67 ("d={};d['a']",KeyError),
68 ("assert 1==0",AssertionError),
68 ("assert 1==0",AssertionError),
69 ("import abababsdbfsbaljasdlja",ImportError),
69 ("import abababsdbfsbaljasdlja",ImportError),
70 ("raise Exception()",Exception)]
70 ("raise Exception()",Exception)]
71
71
72 def testf(x):
72 def testf(x):
73 return 2.0*x
73 return 2.0*x
74
74
75 globala = 99
75 globala = 99
76
76
77 def testg(x):
77 def testg(x):
78 return globala*x
78 return globala*x
79
79
80 class IEngineCoreTestCase(object):
80 class IEngineCoreTestCase(object):
81 """Test an IEngineCore implementer."""
81 """Test an IEngineCore implementer."""
82
82
83 def createShell(self):
83 def createShell(self):
84 return Interpreter()
84 return Interpreter()
85
85
86 def catchQueueCleared(self, f):
86 def catchQueueCleared(self, f):
87 try:
87 try:
88 f.raiseException()
88 f.raiseException()
89 except error.QueueCleared:
89 except error.QueueCleared:
90 pass
90 pass
91
91
92 def testIEngineCoreInterface(self):
92 def testIEngineCoreInterface(self):
93 """Does self.engine claim to implement IEngineCore?"""
93 """Does self.engine claim to implement IEngineCore?"""
94 self.assert_(es.IEngineCore.providedBy(self.engine))
94 self.assert_(es.IEngineCore.providedBy(self.engine))
95
95
96 def testIEngineCoreInterfaceMethods(self):
96 def testIEngineCoreInterfaceMethods(self):
97 """Does self.engine have the methods and attributes in IEngineCore."""
97 """Does self.engine have the methods and attributes in IEngineCore."""
98 for m in list(es.IEngineCore):
98 for m in list(es.IEngineCore):
99 self.assert_(hasattr(self.engine, m))
99 self.assert_(hasattr(self.engine, m))
100
100
101 def testIEngineCoreDeferreds(self):
101 def testIEngineCoreDeferreds(self):
102 d = self.engine.execute('a=5')
102 d = self.engine.execute('a=5')
103 d.addCallback(lambda _: self.engine.pull('a'))
103 d.addCallback(lambda _: self.engine.pull('a'))
104 d.addCallback(lambda _: self.engine.get_result())
104 d.addCallback(lambda _: self.engine.get_result())
105 d.addCallback(lambda _: self.engine.keys())
105 d.addCallback(lambda _: self.engine.keys())
106 d.addCallback(lambda _: self.engine.push(dict(a=10)))
106 d.addCallback(lambda _: self.engine.push(dict(a=10)))
107 return d
107 return d
108
108
109 def runTestExecute(self, cmd):
109 def runTestExecute(self, cmd):
110 self.shell = Interpreter()
110 self.shell = Interpreter()
111 actual = self.shell.execute(cmd)
111 actual = self.shell.execute(cmd)
112 def compare(computed):
112 def compare(computed):
113 actual['id'] = computed['id']
113 actual['id'] = computed['id']
114 self.assertEquals(actual, computed)
114 self.assertEquals(actual, computed)
115 d = self.engine.execute(cmd)
115 d = self.engine.execute(cmd)
116 d.addCallback(compare)
116 d.addCallback(compare)
117 return d
117 return d
118
118
119 @parametric
119 @parametric
120 def testExecute(cls):
120 def testExecute(cls):
121 return [(cls.runTestExecute, cmd) for cmd in validCommands]
121 return [(cls.runTestExecute, cmd) for cmd in validCommands]
122
122
123 def runTestExecuteFailures(self, cmd, exc):
123 def runTestExecuteFailures(self, cmd, exc):
124 def compare(f):
124 def compare(f):
125 self.assertRaises(exc, f.raiseException)
125 self.assertRaises(exc, f.raiseException)
126 d = self.engine.execute(cmd)
126 d = self.engine.execute(cmd)
127 d.addErrback(compare)
127 d.addErrback(compare)
128 return d
128 return d
129
129
130 @parametric
130 @parametric
131 def testExecuteFailuresEngineService(cls):
131 def testExecuteFailuresEngineService(cls):
132 return [(cls.runTestExecuteFailures, cmd, exc)
132 return [(cls.runTestExecuteFailures, cmd, exc)
133 for cmd, exc in invalidCommands]
133 for cmd, exc in invalidCommands]
134
134
135 def runTestPushPull(self, o):
135 def runTestPushPull(self, o):
136 d = self.engine.push(dict(a=o))
136 d = self.engine.push(dict(a=o))
137 d.addCallback(lambda r: self.engine.pull('a'))
137 d.addCallback(lambda r: self.engine.pull('a'))
138 d.addCallback(lambda r: self.assertEquals(o,r))
138 d.addCallback(lambda r: self.assertEquals(o,r))
139 return d
139 return d
140
140
141 @parametric
141 @parametric
142 def testPushPull(cls):
142 def testPushPull(cls):
143 objs = [10,"hi there",1.2342354,{"p":(1,2)},None]
143 objs = [10,"hi there",1.2342354,{"p":(1,2)},None]
144 return [(cls.runTestPushPull, o) for o in objs]
144 return [(cls.runTestPushPull, o) for o in objs]
145
145
146 def testPullNameError(self):
146 def testPullNameError(self):
147 d = self.engine.push(dict(a=5))
147 d = self.engine.push(dict(a=5))
148 d.addCallback(lambda _:self.engine.reset())
148 d.addCallback(lambda _:self.engine.reset())
149 d.addCallback(lambda _: self.engine.pull("a"))
149 d.addCallback(lambda _: self.engine.pull("a"))
150 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
150 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
151 return d
151 return d
152
152
153 def testPushPullFailures(self):
153 def testPushPullFailures(self):
154 d = self.engine.pull('a')
154 d = self.engine.pull('a')
155 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
155 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
156 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
156 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
157 d.addCallback(lambda _: self.engine.pull('l'))
157 d.addCallback(lambda _: self.engine.pull('l'))
158 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
158 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
159 d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x)))
159 d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x)))
160 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
160 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
161 return d
161 return d
162
162
163 def testPushPullArray(self):
163 def testPushPullArray(self):
164 try:
164 try:
165 import numpy
165 import numpy
166 except:
166 except:
167 return
167 return
168 a = numpy.random.random(1000)
168 a = numpy.random.random(1000)
169 d = self.engine.push(dict(a=a))
169 d = self.engine.push(dict(a=a))
170 d.addCallback(lambda _: self.engine.pull('a'))
170 d.addCallback(lambda _: self.engine.pull('a'))
171 d.addCallback(lambda b: b==a)
171 d.addCallback(lambda b: b==a)
172 d.addCallback(lambda c: c.all())
172 d.addCallback(lambda c: c.all())
173 return self.assertDeferredEquals(d, True)
173 return self.assertDeferredEquals(d, True)
174
174
175 def testPushFunction(self):
175 def testPushFunction(self):
176
176
177 d = self.engine.push_function(dict(f=testf))
177 d = self.engine.push_function(dict(f=testf))
178 d.addCallback(lambda _: self.engine.execute('result = f(10)'))
178 d.addCallback(lambda _: self.engine.execute('result = f(10)'))
179 d.addCallback(lambda _: self.engine.pull('result'))
179 d.addCallback(lambda _: self.engine.pull('result'))
180 d.addCallback(lambda r: self.assertEquals(r, testf(10)))
180 d.addCallback(lambda r: self.assertEquals(r, testf(10)))
181 return d
181 return d
182
182
183 def testPullFunction(self):
183 def testPullFunction(self):
184 d = self.engine.push_function(dict(f=testf, g=testg))
184 d = self.engine.push_function(dict(f=testf, g=testg))
185 d.addCallback(lambda _: self.engine.pull_function(('f','g')))
185 d.addCallback(lambda _: self.engine.pull_function(('f','g')))
186 d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
186 d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
187 return d
187 return d
188
188
189 def testPushFunctionGlobal(self):
189 def testPushFunctionGlobal(self):
190 """Make sure that pushed functions pick up the user's namespace for globals."""
190 """Make sure that pushed functions pick up the user's namespace for globals."""
191 d = self.engine.push(dict(globala=globala))
191 d = self.engine.push(dict(globala=globala))
192 d.addCallback(lambda _: self.engine.push_function(dict(g=testg)))
192 d.addCallback(lambda _: self.engine.push_function(dict(g=testg)))
193 d.addCallback(lambda _: self.engine.execute('result = g(10)'))
193 d.addCallback(lambda _: self.engine.execute('result = g(10)'))
194 d.addCallback(lambda _: self.engine.pull('result'))
194 d.addCallback(lambda _: self.engine.pull('result'))
195 d.addCallback(lambda r: self.assertEquals(r, testg(10)))
195 d.addCallback(lambda r: self.assertEquals(r, testg(10)))
196 return d
196 return d
197
197
198 def testGetResultFailure(self):
198 def testGetResultFailure(self):
199 d = self.engine.get_result(None)
199 d = self.engine.get_result(None)
200 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
200 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
201 d.addCallback(lambda _: self.engine.get_result(10))
201 d.addCallback(lambda _: self.engine.get_result(10))
202 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
202 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
203 return d
203 return d
204
204
205 def runTestGetResult(self, cmd):
205 def runTestGetResult(self, cmd):
206 self.shell = Interpreter()
206 self.shell = Interpreter()
207 actual = self.shell.execute(cmd)
207 actual = self.shell.execute(cmd)
208 def compare(computed):
208 def compare(computed):
209 actual['id'] = computed['id']
209 actual['id'] = computed['id']
210 self.assertEquals(actual, computed)
210 self.assertEquals(actual, computed)
211 d = self.engine.execute(cmd)
211 d = self.engine.execute(cmd)
212 d.addCallback(lambda r: self.engine.get_result(r['number']))
212 d.addCallback(lambda r: self.engine.get_result(r['number']))
213 d.addCallback(compare)
213 d.addCallback(compare)
214 return d
214 return d
215
215
216 @parametric
216 @parametric
217 def testGetResult(cls):
217 def testGetResult(cls):
218 return [(cls.runTestGetResult, cmd) for cmd in validCommands]
218 return [(cls.runTestGetResult, cmd) for cmd in validCommands]
219
219
220 def testGetResultDefault(self):
220 def testGetResultDefault(self):
221 cmd = 'a=5'
221 cmd = 'a=5'
222 shell = self.createShell()
222 shell = self.createShell()
223 shellResult = shell.execute(cmd)
223 shellResult = shell.execute(cmd)
224 def popit(dikt, key):
224 def popit(dikt, key):
225 dikt.pop(key)
225 dikt.pop(key)
226 return dikt
226 return dikt
227 d = self.engine.execute(cmd)
227 d = self.engine.execute(cmd)
228 d.addCallback(lambda _: self.engine.get_result())
228 d.addCallback(lambda _: self.engine.get_result())
229 d.addCallback(lambda r: self.assertEquals(shellResult, popit(r,'id')))
229 d.addCallback(lambda r: self.assertEquals(shellResult, popit(r,'id')))
230 return d
230 return d
231
231
232 def testKeys(self):
232 def testKeys(self):
233 d = self.engine.keys()
233 d = self.engine.keys()
234 d.addCallback(lambda s: isinstance(s, list))
234 d.addCallback(lambda s: isinstance(s, list))
235 d.addCallback(lambda r: self.assertEquals(r, True))
235 d.addCallback(lambda r: self.assertEquals(r, True))
236 return d
236 return d
237
237
238 Parametric(IEngineCoreTestCase)
238 Parametric(IEngineCoreTestCase)
239
239
240 class IEngineSerializedTestCase(object):
240 class IEngineSerializedTestCase(object):
241 """Test an IEngineCore implementer."""
241 """Test an IEngineCore implementer."""
242
242
243 def testIEngineSerializedInterface(self):
243 def testIEngineSerializedInterface(self):
244 """Does self.engine claim to implement IEngineCore?"""
244 """Does self.engine claim to implement IEngineCore?"""
245 self.assert_(es.IEngineSerialized.providedBy(self.engine))
245 self.assert_(es.IEngineSerialized.providedBy(self.engine))
246
246
247 def testIEngineSerializedInterfaceMethods(self):
247 def testIEngineSerializedInterfaceMethods(self):
248 """Does self.engine have the methods and attributes in IEngineCore."""
248 """Does self.engine have the methods and attributes in IEngineCore."""
249 for m in list(es.IEngineSerialized):
249 for m in list(es.IEngineSerialized):
250 self.assert_(hasattr(self.engine, m))
250 self.assert_(hasattr(self.engine, m))
251
251
252 def testIEngineSerializedDeferreds(self):
252 def testIEngineSerializedDeferreds(self):
253 dList = []
253 dList = []
254 d = self.engine.push_serialized(dict(key=newserialized.serialize(12345)))
254 d = self.engine.push_serialized(dict(key=newserialized.serialize(12345)))
255 self.assert_(isinstance(d, defer.Deferred))
255 self.assert_(isinstance(d, defer.Deferred))
256 dList.append(d)
256 dList.append(d)
257 d = self.engine.pull_serialized('key')
257 d = self.engine.pull_serialized('key')
258 self.assert_(isinstance(d, defer.Deferred))
258 self.assert_(isinstance(d, defer.Deferred))
259 dList.append(d)
259 dList.append(d)
260 D = defer.DeferredList(dList)
260 D = defer.DeferredList(dList)
261 return D
261 return D
262
262
263 def testPushPullSerialized(self):
263 def testPushPullSerialized(self):
264 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
264 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
265 d = defer.succeed(None)
265 d = defer.succeed(None)
266 for o in objs:
266 for o in objs:
267 self.engine.push_serialized(dict(key=newserialized.serialize(o)))
267 self.engine.push_serialized(dict(key=newserialized.serialize(o)))
268 value = self.engine.pull_serialized('key')
268 value = self.engine.pull_serialized('key')
269 value.addCallback(lambda serial: newserialized.IUnSerialized(serial).getObject())
269 value.addCallback(lambda serial: newserialized.IUnSerialized(serial).getObject())
270 d = self.assertDeferredEquals(value,o,d)
270 d = self.assertDeferredEquals(value,o,d)
271 return d
271 return d
272
272
273 def testPullSerializedFailures(self):
273 def testPullSerializedFailures(self):
274 d = self.engine.pull_serialized('a')
274 d = self.engine.pull_serialized('a')
275 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
275 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
276 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
276 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
277 d.addCallback(lambda _: self.engine.pull_serialized('l'))
277 d.addCallback(lambda _: self.engine.pull_serialized('l'))
278 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
278 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
279 return d
279 return d
280
280
281 Parametric(IEngineSerializedTestCase)
281 Parametric(IEngineSerializedTestCase)
282
282
283 class IEngineQueuedTestCase(object):
283 class IEngineQueuedTestCase(object):
284 """Test an IEngineQueued implementer."""
284 """Test an IEngineQueued implementer."""
285
285
286 def testIEngineQueuedInterface(self):
286 def testIEngineQueuedInterface(self):
287 """Does self.engine claim to implement IEngineQueued?"""
287 """Does self.engine claim to implement IEngineQueued?"""
288 self.assert_(es.IEngineQueued.providedBy(self.engine))
288 self.assert_(es.IEngineQueued.providedBy(self.engine))
289
289
290 def testIEngineQueuedInterfaceMethods(self):
290 def testIEngineQueuedInterfaceMethods(self):
291 """Does self.engine have the methods and attributes in IEngineQueued."""
291 """Does self.engine have the methods and attributes in IEngineQueued."""
292 for m in list(es.IEngineQueued):
292 for m in list(es.IEngineQueued):
293 self.assert_(hasattr(self.engine, m))
293 self.assert_(hasattr(self.engine, m))
294
294
295 def testIEngineQueuedDeferreds(self):
295 def testIEngineQueuedDeferreds(self):
296 dList = []
296 dList = []
297 d = self.engine.clear_queue()
297 d = self.engine.clear_queue()
298 self.assert_(isinstance(d, defer.Deferred))
298 self.assert_(isinstance(d, defer.Deferred))
299 dList.append(d)
299 dList.append(d)
300 d = self.engine.queue_status()
300 d = self.engine.queue_status()
301 self.assert_(isinstance(d, defer.Deferred))
301 self.assert_(isinstance(d, defer.Deferred))
302 dList.append(d)
302 dList.append(d)
303 D = defer.DeferredList(dList)
303 D = defer.DeferredList(dList)
304 return D
304 return D
305
305
306 def testClearQueue(self):
306 def testClearQueue(self):
307 result = self.engine.clear_queue()
307 result = self.engine.clear_queue()
308 d1 = self.assertDeferredEquals(result, None)
308 d1 = self.assertDeferredEquals(result, None)
309 d1.addCallback(lambda _: self.engine.queue_status())
309 d1.addCallback(lambda _: self.engine.queue_status())
310 d2 = self.assertDeferredEquals(d1, {'queue':[], 'pending':'None'})
310 d2 = self.assertDeferredEquals(d1, {'queue':[], 'pending':'None'})
311 return d2
311 return d2
312
312
313 def testQueueStatus(self):
313 def testQueueStatus(self):
314 result = self.engine.queue_status()
314 result = self.engine.queue_status()
315 result.addCallback(lambda r: 'queue' in r and 'pending' in r)
315 result.addCallback(lambda r: 'queue' in r and 'pending' in r)
316 d = self.assertDeferredEquals(result, True)
316 d = self.assertDeferredEquals(result, True)
317 return d
317 return d
318
318
319 Parametric(IEngineQueuedTestCase)
319 Parametric(IEngineQueuedTestCase)
320
320
321 class IEnginePropertiesTestCase(object):
321 class IEnginePropertiesTestCase(object):
322 """Test an IEngineProperties implementor."""
322 """Test an IEngineProperties implementor."""
323
323
324 def testIEnginePropertiesInterface(self):
324 def testIEnginePropertiesInterface(self):
325 """Does self.engine claim to implement IEngineProperties?"""
325 """Does self.engine claim to implement IEngineProperties?"""
326 self.assert_(es.IEngineProperties.providedBy(self.engine))
326 self.assert_(es.IEngineProperties.providedBy(self.engine))
327
327
328 def testIEnginePropertiesInterfaceMethods(self):
328 def testIEnginePropertiesInterfaceMethods(self):
329 """Does self.engine have the methods and attributes in IEngineProperties."""
329 """Does self.engine have the methods and attributes in IEngineProperties."""
330 for m in list(es.IEngineProperties):
330 for m in list(es.IEngineProperties):
331 self.assert_(hasattr(self.engine, m))
331 self.assert_(hasattr(self.engine, m))
332
332
333 def testGetSetProperties(self):
333 def testGetSetProperties(self):
334 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
334 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
335 d = self.engine.set_properties(dikt)
335 d = self.engine.set_properties(dikt)
336 d.addCallback(lambda r: self.engine.get_properties())
336 d.addCallback(lambda r: self.engine.get_properties())
337 d = self.assertDeferredEquals(d, dikt)
337 d = self.assertDeferredEquals(d, dikt)
338 d.addCallback(lambda r: self.engine.get_properties(('c',)))
338 d.addCallback(lambda r: self.engine.get_properties(('c',)))
339 d = self.assertDeferredEquals(d, {'c': dikt['c']})
339 d = self.assertDeferredEquals(d, {'c': dikt['c']})
340 d.addCallback(lambda r: self.engine.set_properties(dict(c=False)))
340 d.addCallback(lambda r: self.engine.set_properties(dict(c=False)))
341 d.addCallback(lambda r: self.engine.get_properties(('c', 'd')))
341 d.addCallback(lambda r: self.engine.get_properties(('c', 'd')))
342 d = self.assertDeferredEquals(d, dict(c=False, d=None))
342 d = self.assertDeferredEquals(d, dict(c=False, d=None))
343 return d
343 return d
344
344
345 def testClearProperties(self):
345 def testClearProperties(self):
346 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
346 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
347 d = self.engine.set_properties(dikt)
347 d = self.engine.set_properties(dikt)
348 d.addCallback(lambda r: self.engine.clear_properties())
348 d.addCallback(lambda r: self.engine.clear_properties())
349 d.addCallback(lambda r: self.engine.get_properties())
349 d.addCallback(lambda r: self.engine.get_properties())
350 d = self.assertDeferredEquals(d, {})
350 d = self.assertDeferredEquals(d, {})
351 return d
351 return d
352
352
353 def testDelHasProperties(self):
353 def testDelHasProperties(self):
354 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
354 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
355 d = self.engine.set_properties(dikt)
355 d = self.engine.set_properties(dikt)
356 d.addCallback(lambda r: self.engine.del_properties(('b','e')))
356 d.addCallback(lambda r: self.engine.del_properties(('b','e')))
357 d.addCallback(lambda r: self.engine.has_properties(('a','b','c','d','e')))
357 d.addCallback(lambda r: self.engine.has_properties(('a','b','c','d','e')))
358 d = self.assertDeferredEquals(d, [True, False, True, True, False])
358 d = self.assertDeferredEquals(d, [True, False, True, True, False])
359 return d
359 return d
360
360
361 def testStrictDict(self):
361 def testStrictDict(self):
362 s = """from IPython.kernel.engineservice import get_engine
362 s = """from IPython.kernel.engineservice import get_engine; p = get_engine(%s).properties"""%self.engine.id
363 p = get_engine(%s).properties"""%self.engine.id
364 d = self.engine.execute(s)
363 d = self.engine.execute(s)
364 # These 3 lines cause a weird testing error on some platforms (OS X).
365 # I am leaving them here in case they are masking some really
366 # weird reactor issue. For now I will just keep my eye on this.
365 d.addCallback(lambda r: self.engine.execute("p['a'] = lambda _:None"))
367 d.addCallback(lambda r: self.engine.execute("p['a'] = lambda _:None"))
366 d.addErrback(lambda f: self.assertRaises(error.InvalidProperty,
368 d.addErrback(lambda f: self.assertRaises(error.InvalidProperty,
367 f.raiseException))
369 f.raiseException))
370 # Below here seems to be fine
368 d.addCallback(lambda r: self.engine.execute("p['a'] = range(5)"))
371 d.addCallback(lambda r: self.engine.execute("p['a'] = range(5)"))
369 d.addCallback(lambda r: self.engine.execute("p['a'].append(5)"))
372 d.addCallback(lambda r: self.engine.execute("p['a'].append(5)"))
370 d.addCallback(lambda r: self.engine.get_properties('a'))
373 d.addCallback(lambda r: self.engine.get_properties('a'))
371 d = self.assertDeferredEquals(d, dict(a=range(5)))
374 d = self.assertDeferredEquals(d, dict(a=range(5)))
372 return d
375 return d
373
376
374 Parametric(IEnginePropertiesTestCase)
377 Parametric(IEnginePropertiesTestCase)
General Comments 0
You need to be logged in to leave comments. Login now