Debian Codegraph @

envisage/tests/test_service_registry.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ Tests for the service registry. """
11
12
13 # Standard library imports.
14 import sys
15 import unittest
16
17 from traits.api import HasTraits, Int, Interface, provides
18
19 # Enthought library imports.
20 from envisage.api import Application, NoSuchServiceError, ServiceRegistry
21
22 # This module's package.
23 PKG = "envisage.tests"
24
25
26 def service_factory(**properties):
27 """A factory for foos."""
28
29 return HasTraits(**properties)
30
31
32 class ServiceRegistryTestCase(unittest.TestCase):
33 """Tests for the service registry."""
34
35 ###########################################################################
36 # 'TestCase' interface.
37 ###########################################################################
38
39 def setUp(self):
40 """Prepares the test fixture before each test method is called."""
41
42 # We do all of the testing via the application to make sure it offers
43 # the same interface!
44 self.service_registry = Application(service_registry=ServiceRegistry())
45
46 # module 'foo' need to be cleared out when this test is run,
47 # because other tests also import foo.
48 if PKG + ".foo" in sys.modules:
49 del sys.modules[PKG + ".foo"]
50
51 ###########################################################################
52 # Tests.
53 ###########################################################################
54
55 def test_should_get_required_service(self):
56 class Foo(HasTraits):
57 price = Int
58
59 foo = Foo()
60
61 # Register a service factory.
62 self.service_registry.register_service(Foo, foo)
63
64 service = self.service_registry.get_required_service(Foo)
65 self.assertIs(foo, service)
66
67 def test_should_get_exception_if_required_service_is_missing(self):
68 class IFoo(Interface):
69 price = Int
70
71 with self.assertRaises(NoSuchServiceError):
72 self.service_registry.get_required_service(IFoo)
73
74 def test_imported_service_factory(self):
75 """imported service factory"""
76
77 class IFoo(Interface):
78 price = Int
79
80 # Register a service factory.
81 self.service_registry.register_service(
82 HasTraits,
83 PKG + ".test_service_registry.service_factory",
84 {"price": 100},
85 )
86
87 # Create a query that matches the registered object.
88 service = self.service_registry.get_service(HasTraits, "price <= 100")
89 self.assertNotEqual(None, service)
90 self.assertEqual(HasTraits, type(service))
91
92 # This shows that the properties were passed in to the factory.
93 self.assertEqual(100, service.price)
94
95 # Make sure that the object created by the factory is cached (i.e. we
96 # get the same object back from now on!).
97 service2 = self.service_registry.get_service(HasTraits, "price <= 100")
98 self.assertTrue(service is service2)
99
100 def test_function_service_factory(self):
101 """function service factory"""
102
103 class IFoo(Interface):
104 price = Int
105
106 @provides(IFoo)
107 class Foo(HasTraits):
108 price = Int
109
110 def foo_factory(**properties):
111 """A factory for foos."""
112
113 return Foo(**properties)
114
115 # Register a service factory.
116 self.service_registry.register_service(
117 IFoo, foo_factory, {"price": 100}
118 )
119
120 # Create a query that matches the registered object.
121 service = self.service_registry.get_service(IFoo, "price <= 100")
122 self.assertNotEqual(None, service)
123 self.assertEqual(Foo, type(service))
124
125 # Make sure that the object created by the factory is cached (i.e. we
126 # get the same object back from now on!).
127 service2 = self.service_registry.get_service(IFoo, "price <= 100")
128 self.assertTrue(service is service2)
129
130 def test_lazy_function_service_factory(self):
131 """lazy function service factory"""
132
133 # Register a service factory by name.
134 def foo_factory(**properties):
135 """A factory for foos."""
136
137 from envisage.tests.foo import Foo
138
139 foo_factory.foo = Foo()
140
141 return foo_factory.foo
142
143 i_foo = PKG + ".i_foo.IFoo"
144 foo = PKG + ".foo"
145
146 self.service_registry.register_service(i_foo, foo_factory)
147
148 # Get rid of the 'foo' module (used in other tests).
149 if foo in sys.modules:
150 del sys.modules[foo]
151
152 # Make sure that we haven't imported the 'foo' module.
153 self.assertTrue(foo not in sys.modules)
154
155 # Look up a non-existent service.
156 services = self.service_registry.get_services("bogus.IBogus")
157
158 # Make sure that we *still* haven't imported the 'foo' module.
159 self.assertTrue(foo not in sys.modules)
160
161 # Look it up again.
162 services = self.service_registry.get_services(i_foo)
163 self.assertEqual([foo_factory.foo], services)
164 self.assertTrue(foo in sys.modules)
165
166 # Clean up!
167 del sys.modules[foo]
168
169 def test_lazy_bound_method_service_factory(self):
170 """lazy bound method service factory"""
171
172 i_foo = PKG + ".i_foo.IFoo"
173 foo = PKG + ".foo"
174
175 class ServiceProvider(HasTraits):
176 """A class that provides a service.
177
178 This is used to make sure a bound method can be used as a service
179 factory.
180
181 """
182
183 # Register a service factory by name.
184 def foo_factory(self, **properties):
185 """A factory for foos."""
186
187 from envisage.tests.foo import Foo
188
189 self.foo = Foo()
190
191 return self.foo
192
193 sp = ServiceProvider()
194 self.service_registry.register_service(i_foo, sp.foo_factory)
195
196 # Get rid of the 'foo' module (used in other tests).
197 if foo in sys.modules:
198 del sys.modules[foo]
199
200 # Make sure that we haven't imported the 'foo' module.
201 self.assertTrue(foo not in sys.modules)
202
203 # Look up a non-existent service.
204 services = self.service_registry.get_services("bogus.IBogus")
205
206 # Make sure that we *still* haven't imported the 'foo' module.
207 self.assertTrue(foo not in sys.modules)
208
209 # Look up the service.
210 services = self.service_registry.get_services(i_foo)
211 self.assertEqual([sp.foo], services)
212 self.assertTrue(foo in sys.modules)
213
214 # Clean up!
215 del sys.modules[foo]
216
217 def test_get_services(self):
218 """get services"""
219
220 class IFoo(Interface):
221 pass
222
223 @provides(IFoo)
224 class Foo(HasTraits):
225 pass
226
227 # Register two services.
228 foo = Foo()
229 self.service_registry.register_service(IFoo, foo)
230
231 foo = Foo()
232 self.service_registry.register_service(IFoo, foo)
233
234 # Look it up again.
235 services = self.service_registry.get_services(IFoo)
236 self.assertEqual(2, len(services))
237
238 class IBar(Interface):
239 pass
240
241 # Lookup a non-existent service.
242 services = self.service_registry.get_services(IBar)
243 self.assertEqual([], services)
244
245 def test_get_services_with_strings(self):
246 """get services with strings"""
247
248 from envisage.tests.foo import Foo
249
250 # Register a couple of services using a string protocol name.
251 protocol_name = "envisage.tests.foo.IFoo"
252
253 self.service_registry.register_service(protocol_name, Foo())
254 self.service_registry.register_service(protocol_name, Foo())
255
256 # Look them up using the same string!
257 services = self.service_registry.get_services(protocol_name)
258 self.assertEqual(2, len(services))
259
260 def test_get_services_with_query(self):
261 """get services with query"""
262
263 class IFoo(Interface):
264 price = Int
265
266 @provides(IFoo)
267 class Foo(HasTraits):
268 price = Int
269
270 # Register two services.
271 #
272 # This one shows how the object's attributes are used when evaluating
273 # a query.
274 foo = Foo(price=100)
275 self.service_registry.register_service(IFoo, foo)
276
277 # This one shows how properties can be specified that *take precedence*
278 # over the object's attributes when evaluating a query.
279 goo = Foo(price=10)
280 self.service_registry.register_service(IFoo, goo, {"price": 200})
281
282 # Create a query that doesn't match any registered object.
283 services = self.service_registry.get_services(IFoo, 'color == "red"')
284 self.assertEqual([], services)
285
286 # Create a query that matches one of the registered objects.
287 services = self.service_registry.get_services(IFoo, "price <= 100")
288 self.assertEqual([foo], services)
289
290 # Create a query that matches both registered objects.
291 services = self.service_registry.get_services(IFoo, "price >= 100")
292 self.assertTrue(foo in services)
293 self.assertTrue(goo in services)
294 self.assertEqual(2, len(services))
295
296 class IBar(Interface):
297 pass
298
299 # Lookup a non-existent service.
300 services = self.service_registry.get_services(IBar, "price <= 100")
301 self.assertEqual([], services)
302
303 def test_get_service(self):
304 """get service"""
305
306 class IFoo(Interface):
307 pass
308
309 @provides(IFoo)
310 class Foo(HasTraits):
311 pass
312
313 # Register a couple of services.
314 foo = Foo()
315 self.service_registry.register_service(IFoo, foo)
316
317 goo = Foo()
318 self.service_registry.register_service(IFoo, goo)
319
320 # Look up one of them!
321 service = self.service_registry.get_service(IFoo)
322 self.assertTrue(foo is service or goo is service)
323
324 class IBar(Interface):
325 pass
326
327 # Lookup a non-existent service.
328 service = self.service_registry.get_service(IBar)
329 self.assertEqual(None, service)
330
331 def test_get_service_with_query(self):
332 """get service with query"""
333
334 class IFoo(Interface):
335 price = Int
336
337 @provides(IFoo)
338 class Foo(HasTraits):
339 price = Int
340
341 # Register two services.
342 #
343 # This one shows how the object's attributes are used when evaluating
344 # a query.
345 foo = Foo(price=100)
346 self.service_registry.register_service(IFoo, foo)
347
348 # This one shows how properties can be specified that *take precedence*
349 # over the object's attributes when evaluating a query.
350 goo = Foo(price=10)
351 self.service_registry.register_service(IFoo, goo, {"price": 200})
352
353 # Create a query that doesn't match any registered object.
354 service = self.service_registry.get_service(IFoo, "price < 100")
355 self.assertEqual(None, service)
356
357 # Create a query that matches one of the registered objects.
358 service = self.service_registry.get_service(IFoo, "price <= 100")
359 self.assertEqual(foo, service)
360
361 # Create a query that matches both registered objects.
362 service = self.service_registry.get_service(IFoo, "price >= 100")
363 self.assertTrue(foo is service or goo is service)
364
365 class IBar(Interface):
366 pass
367
368 # Lookup a non-existent service.
369 service = self.service_registry.get_service(IBar, "price <= 100")
370 self.assertEqual(None, service)
371
372 def test_get_and_set_service_properties(self):
373 """get and set service properties"""
374
375 class IFoo(Interface):
376 price = Int
377
378 @provides(IFoo)
379 class Foo(HasTraits):
380 price = Int
381
382 # Register two services.
383 #
384 # This one has no properties.
385 foo = Foo(price=100)
386 foo_id = self.service_registry.register_service(IFoo, foo)
387
388 # This one has properties.
389 goo = Foo(price=10)
390 goo_id = self.service_registry.register_service(
391 IFoo, goo, {"price": 200}
392 )
393
394 # Get the properties.
395 foo_properties = self.service_registry.get_service_properties(foo_id)
396 self.assertEqual({}, foo_properties)
397
398 goo_properties = self.service_registry.get_service_properties(goo_id)
399 self.assertEqual(200, goo_properties["price"])
400
401 # Update the properties.
402 foo_properties["price"] = 300
403 goo_properties["price"] = 500
404
405 # Set the properties.
406 self.service_registry.set_service_properties(foo_id, foo_properties)
407 self.service_registry.set_service_properties(goo_id, goo_properties)
408
409 # Get the properties again.
410 foo_properties = self.service_registry.get_service_properties(foo_id)
411 self.assertEqual(300, foo_properties["price"])
412
413 goo_properties = self.service_registry.get_service_properties(goo_id)
414 self.assertEqual(500, goo_properties["price"])
415
416 # Try to get the properties of a non-existent service.
417 with self.assertRaises(ValueError):
418 self.service_registry.get_service_properties(-1)
419
420 # Try to set the properties of a non-existent service.
421 with self.assertRaises(ValueError):
422 self.service_registry.set_service_properties(-1, {})
423
424 def test_unregister_service(self):
425 """unregister service"""
426
427 class IFoo(Interface):
428 price = Int
429
430 @provides(IFoo)
431 class Foo(HasTraits):
432 price = Int
433
434 # Register two services.
435 #
436 # This one shows how the object's attributes are used when evaluating
437 # a query.
438 foo = Foo(price=100)
439 foo_id = self.service_registry.register_service(IFoo, foo)
440
441 # This one shows how properties can be specified that *take precedence*
442 # over the object's attributes when evaluating a query.
443 goo = Foo(price=10)
444 goo_id = self.service_registry.register_service(
445 IFoo, goo, {"price": 200}
446 )
447
448 # Create a query that doesn't match any registered object.
449 service = self.service_registry.get_service(IFoo, "price < 100")
450 self.assertEqual(None, service)
451
452 # Create a query that matches one of the registered objects.
453 service = self.service_registry.get_service(IFoo, "price <= 100")
454 self.assertEqual(foo, service)
455
456 # Create a query that matches both registered objects.
457 service = self.service_registry.get_service(IFoo, "price >= 100")
458 self.assertTrue(foo is service or goo is service)
459
460 #### Now do some unregistering! ####
461
462 # Unregister 'foo'.
463 self.service_registry.unregister_service(foo_id)
464
465 # This query should no longer match any of the registered objects.
466 service = self.service_registry.get_service(IFoo, "price <= 100")
467 self.assertEqual(None, service)
468
469 # Unregister 'goo'.
470 self.service_registry.unregister_service(goo_id)
471
472 # This query should no longer match any of the registered objects.
473 service = self.service_registry.get_service(IFoo, "price >= 100")
474 self.assertEqual(None, service)
475
476 # Try to unregister a non-existent service.
477 with self.assertRaises(ValueError):
478 self.service_registry.unregister_service(-1)
479
480 def test_minimize_and_maximize(self):
481 """minimize and maximize"""
482
483 class IFoo(Interface):
484 price = Int
485
486 @provides(IFoo)
487 class Foo(HasTraits):
488 price = Int
489
490 # Register some objects with various prices.
491 x = Foo(price=10)
492 y = Foo(price=5)
493 z = Foo(price=100)
494
495 for foo in [x, y, z]:
496 self.service_registry.register_service(IFoo, foo)
497
498 # Find the service with the lowest price.
499 service = self.service_registry.get_service(IFoo, minimize="price")
500 self.assertNotEqual(None, service)
501 self.assertEqual(Foo, type(service))
502 self.assertEqual(y, service)
503
504 # Find the service with the highest price.
505 service = self.service_registry.get_service(IFoo, maximize="price")
506 self.assertNotEqual(None, service)
507 self.assertEqual(Foo, type(service))
508 self.assertEqual(z, service)