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