def test_get(self): s = ServiceContainer() service_def = Service("ServiceContainerTestCase.TestClass1", ["param1", "param2"]) s.set_service("test", service_def) i = s.get_service("test") self.assertTrue(isinstance(i, getattr(sys.modules["ServiceContainerTestCase"], "TestClass1")))
def test_set(self): s = ServiceContainer() service_def = Service('ServiceContainerTestCase.TestClass1', ['param1', 'param2']) s.set_service('test', service_def) self.assertEquals({'test': service_def}, s._ServiceContainer__container)
def test_circular_dependencies(self): s = ServiceContainer() service_def1 = Service("ServiceContainerTestCase.TestClass1", ["@test2", ""]) service_def2 = Service("ServiceContainerTestCase.TestClass1", ["@test1", ""]) s.set_service("test1", service_def1) s.set_service("test2", service_def2) self.assertRaises(ValueError, s.get_service, "test1")
def test_circular_dependencies(self): s = ServiceContainer() service_def1 = Service('ServiceContainerTestCase.TestClass1', ['@test2', '']) service_def2 = Service('ServiceContainerTestCase.TestClass1', ['@test1', '']) s.set_service('test1', service_def1) s.set_service('test2', service_def2) self.assertRaises(ValueError, s.get_service, 'test1')
def test_get(self): s = ServiceContainer() service_def = Service('ServiceContainerTestCase.TestClass1', ['param1', 'param2']) s.set_service('test', service_def) i = s.get_service('test') self.assertTrue( isinstance( i, getattr(sys.modules['ServiceContainerTestCase'], 'TestClass1')))
def test_get_dependencies(self): s = ServiceContainer() service_def1 = Service('ServiceContainerTestCase.TestClass1', ['@test2', '@test3']) service_def2 = Service('ServiceContainerTestCase.TestClass2', []) service_def3 = Service('ServiceContainerTestCase.TestClass2', []) s.set_service('test1', service_def1) s.set_service('test2', service_def2) s.set_service('test3', service_def3) i1 = s.get_service('test1') i2 = s.get_service('test2') i3 = s.get_service('test3') self.assertTrue( isinstance( i1, getattr(sys.modules['ServiceContainerTestCase'], 'TestClass1'))) self.assertTrue( isinstance( i2, getattr(sys.modules['ServiceContainerTestCase'], 'TestClass2'))) self.assertTrue( isinstance( i3, getattr(sys.modules['ServiceContainerTestCase'], 'TestClass2'))) self.assertFalse(i2 == i3) self.assertFalse(i1.param1 == i2) self.assertFalse(i1.param2 == i3)
def test_get_persistent_dependencies(self): s = ServiceContainer() service_def1 = Service("ServiceContainerTestCase.TestClass1", ["@test2", "@test3"]) service_def2 = Service("ServiceContainerTestCase.TestClass2", []) service_def3 = Service("ServiceContainerTestCase.TestClass2", []) service_def2.setPersistent(True) service_def3.setPersistent(True) s.set_service("test1", service_def1) s.set_service("test2", service_def2) s.set_service("test3", service_def3) i1 = s.get_service("test1") i2 = s.get_service("test2") i3 = s.get_service("test3") self.assertTrue(isinstance(i1, getattr(sys.modules["ServiceContainerTestCase"], "TestClass1"))) self.assertTrue(isinstance(i2, getattr(sys.modules["ServiceContainerTestCase"], "TestClass2"))) self.assertTrue(isinstance(i3, getattr(sys.modules["ServiceContainerTestCase"], "TestClass2"))) self.assertFalse(i2 == i3) self.assertTrue(i1.param1 == i2) self.assertTrue(i1.param2 == i3)
def test_expand_parameters(self): s = ServiceContainer() service_def = Service("ServiceContainerTestCase.TestClass1", ["%param1%", "param2"]) s.set_service("test", service_def) s.set_param("param1", "my_param_test") i = s.get_service("test") self.assertTrue(isinstance(i, getattr(sys.modules["ServiceContainerTestCase"], "TestClass1"))) self.assertEquals("my_param_test", i.param1)
def test_expand_parameters(self): s = ServiceContainer() service_def = Service('ServiceContainerTestCase.TestClass1', ['%param1%', 'param2']) s.set_service('test', service_def) s.set_param('param1', 'my_param_test') i = s.get_service('test') self.assertTrue( isinstance( i, getattr(sys.modules['ServiceContainerTestCase'], 'TestClass1'))) self.assertEquals('my_param_test', i.param1)
def test_set(self): s = ServiceContainer() service_def = Service("ServiceContainerTestCase.TestClass1", ["param1", "param2"]) s.set_service("test", service_def) self.assertEquals({"test": service_def}, s._ServiceContainer__container)
def test_load(self): container = ServiceContainer() loader = ServiceContainerLoader() loader.register_section_loader(DatabaseSectionLoader()) loader.register_section_loader(ServicesSectionLoader()) loader.load(container, sys.path[0] + '/fixtures/config.yml') #container.dump() # Test parameters self.assertEquals('bar', container.get_param('foo')) self.assertEquals(['one', 'two', 'three'], container.get_param('list')) self.assertEquals( { 'something': 'something else', 'whatever': ['list 1', 'list 2'] }, container.get_param('dict')) self.assertEquals('bar', container.get_param('param1')) self.assertRaises(ValueError, container.get_param, 'param2') self.assertEquals(['one', 'two', 'three'], container.get_param('param3')) self.assertEquals(['bar', 'bar'], container.get_param('param4')) self.assertEquals([['one', 'two', 'three'], ['bar', 'bar']], container.get_param('param5')) self.assertEquals({ 'value2': ['one', 'two', 'three'], 'value1': 'bar' }, container.get_param('param6')) self.assertEquals([[['one', 'two', 'three'], ['bar', 'bar']], { 'value2': ['one', 'two', 'three'], 'value1': 'bar' }], container.get_param('param7')) # Test services i1 = container.get_service('test1') i2 = container.get_service('test2') i3 = container.get_service('test3') self.assertTrue( isinstance( i1, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass1'))) self.assertTrue( isinstance( i2, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass2'))) self.assertTrue( isinstance( i3, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass3'))) self.assertEquals('my_name', i2.name) self.assertTrue( isinstance( i3.class1, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass1'))) self.assertTrue( isinstance( i3.class2, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass2'))) # Service named params i4 = container.get_service('test4') self.assertTrue( isinstance( i4, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass4'))) self.assertEquals(0, i4.a) self.assertEquals(1, i4.b) self.assertEquals(3, i4.c) # Complex service log1 = container.get_service('log.handler') log2 = container.get_service('log.null_handler') log3 = container.get_service('log.file_handler') self.assertTrue(isinstance(log1, logbook.NestedSetup)) self.assertTrue(isinstance(log2, logbook.NullHandler)) self.assertTrue(isinstance(log3, logbook.FileHandler)) self.assertEquals(log1.objects[0], log2) self.assertEquals(log1.objects[1], log3) self.assertEquals(log3._filename, '/tmp/testlog.log') self.assertEquals(log3.level, logbook.WARNING)
def test_load(self): container = ServiceContainer() loader = ServiceContainerLoader() loader.register_section_loader(DatabaseSectionLoader()) loader.register_section_loader(ServicesSectionLoader()) loader.load(container, sys.path[0] + '/fixtures/config.yml') #container.dump() # Test parameters self.assertEquals('bar', container.get_param('foo')) self.assertEquals(['one', 'two', 'three'], container.get_param('list')) self.assertEquals({'something': 'something else', 'whatever': ['list 1', 'list 2']}, container.get_param('dict')) self.assertEquals('bar', container.get_param('param1')) self.assertRaises(ValueError, container.get_param, 'param2') self.assertEquals(['one', 'two', 'three'], container.get_param('param3')) self.assertEquals(['bar', 'bar'], container.get_param('param4')) self.assertEquals([['one', 'two', 'three'], ['bar', 'bar']], container.get_param('param5')) self.assertEquals({'value2': ['one', 'two', 'three'], 'value1': 'bar'}, container.get_param('param6')) self.assertEquals([[['one', 'two', 'three'], ['bar', 'bar']], {'value2': ['one', 'two', 'three'], 'value1': 'bar'}], container.get_param('param7')) # Test services i1 = container.get_service('test1') i2 = container.get_service('test2') i3 = container.get_service('test3') self.assertTrue(isinstance(i1, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass1'))) self.assertTrue(isinstance(i2, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass2'))) self.assertTrue(isinstance(i3, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass3'))) self.assertEquals('my_name', i2.name) self.assertTrue(isinstance(i3.class1, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass1'))) self.assertTrue(isinstance(i3.class2, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass2'))) # Service named params i4 = container.get_service('test4') self.assertTrue(isinstance(i4, getattr(sys.modules['ServiceContainerLoaderTestCase'], 'TestClass4'))) self.assertEquals(0, i4.a) self.assertEquals(1, i4.b) self.assertEquals(3, i4.c) # Complex service log1 = container.get_service('log.handler') log2 = container.get_service('log.null_handler') log3 = container.get_service('log.file_handler') self.assertTrue(isinstance(log1, logbook.NestedSetup)) self.assertTrue(isinstance(log2, logbook.NullHandler)) self.assertTrue(isinstance(log3, logbook.FileHandler)) self.assertEquals(log1.objects[0], log2) self.assertEquals(log1.objects[1], log3) self.assertEquals(log3._filename, '/tmp/testlog.log') self.assertEquals(log3.level, logbook.WARNING)
def test_init(self): s = ServiceContainer() self.assertTrue(isinstance(s, ServiceContainer)) self.assertEquals({}, s._ServiceContainer__container) self.assertRaises(ValueError, s.set_service, 'test', 'TestClass1')