Example #1
0
    def test_call_undefined_entity(self):
        test_filepaths = [
            'test/data/locator-factorization.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UndefinedContainerIDError, assembler.core.get, 'panda')
Example #2
0
class Core(object):
    """ The Core of the Framework

        This relies on Imagination Framework.
    """
    def __init__(self, locator=None):
        self.assembler = Assembler()

    @property
    def _core(self):
        return self.container

    @property
    def container(self):
        return self.assembler.core

    def get(self, id):
        """ Get the service container. """
        return self._core.get(id)

    def load(self, *paths):
        """ Load service containers from multiple configuration files. """
        self.assembler.load(*paths)

    def set_entity(self, entity_id, fqcn):
        """ Register a new entity. """
        container = Entity(identifier = entity_id, fqcn = fqcn)

        self._core.update_metadata({entity_id: container})

    def set_factorization(self, entity_id, factory_id, factory_method_name):
        """ Register a new entity with factorization. """
        container = Factorization(entity_id, factory_id, factory_method_name)

        self._core.update_metadata({entity_id: container})

    def set_lambda(self, entity_id, import_path):
        container = Lambda(entity_id, import_path)

        self._core.update_metadata({entity_id: container})

    def set_entity_param(self, entity_id, kind, value,
                         transformation_required = False, name = None):
        """ Define a constructor parameter for a particular entity. """
        definition = DataDefinition(value, name, kind, transformation_required)
        metadata   = self._core.get_metadata(entity_id)

        metadata.params.add(definition, name)

    def inject_entity_dependency(self, entity_id, dependency_id, name = None):
        """ Inject an entity as a dependency to the specified entity. """
        self.set_entity_param(entity_id, 'entity', dependency_id, True, name)

    def inject_entity_classinfo(self, entity_id, fqcn, name = None):
        """ Inject a class/type as a dependency to the specified entity. """
        self.set_entity_param(entity_id, 'class', fqcn, True, name)
Example #3
0
    def test_call_entity_with_unexpected_parameters(self):
        """ The constructor receives unexpected parameters. """
        test_filepaths = [
            'test/data/locator-with-undefined-entity.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UndefinedContainerIDError, assembler.core.get, 'dioe')
Example #4
0
    def test_call_entity_with_unexpected_parameters(self):
        """ The constructor receives unexpected parameters. """
        test_filepaths = [
            'test/data/locator-with-unexpected-parameters.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UnexpectedParameterException, assembler.core.get, 'dioe')
Example #5
0
    def test_call_entity_with_missing_required_parameters(self):
        """ The constructor does not have all required parameters. """
        test_filepaths = [
            'test/data/locator-instantiation-error.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UndefinedDefaultValueException, assembler.core.get, 'poow-1')
    def test_call_undefined_entity(self):
        test_filepaths = [
            'test/data/locator-factorization.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UndefinedContainerIDError, assembler.core.get,
                          'panda')
    def test_call_entity_with_unexpected_parameters(self):
        """ The constructor receives unexpected parameters. """
        test_filepaths = [
            'test/data/locator-with-undefined-entity.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UndefinedContainerIDError, assembler.core.get,
                          'dioe')
    def test_call_entity_with_unexpected_parameters(self):
        """ The constructor receives unexpected parameters. """
        test_filepaths = [
            'test/data/locator-with-unexpected-parameters.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(UnexpectedParameterException, assembler.core.get,
                          'dioe')
    def test_call_entity_with_missing_required_parameters(self):
        """ The constructor does not have all required parameters. """
        test_filepaths = [
            'test/data/locator-instantiation-error.xml',
        ]

        assembler = Assembler()
        assembler.load(*test_filepaths)

        self.assertRaises(MissingParameterException, assembler.core.get,
                          'poow-1')
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest(
                'The tested feature is not supported in Python {}.'.format(
                    sys.version))

        test_filepaths = [
            'test/data/locator-aop-error-event.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_no_params(self):
        alpha = self.core.get('alpha')

        try:
            alpha.init_self_destruction_1()
        except DummyException as e:
            self.assertIsInstance(e.previous_error, RuntimeError)
            self.assertFalse(e.positional_parameters)
            self.assertFalse(e.keyword_parameters)
        else:
            self.assertTrue(False,
                            "This is supposed to be raised an exception.")

    def test_with_params(self):
        alpha = self.core.get('alpha')

        positional_parameters = (1, 2, 3)
        keyword_parameters = {'d': 4, 'e': 5}

        try:
            alpha.init_self_destruction_2(*positional_parameters,
                                          **keyword_parameters)
        except DummyException as e:
            self.assertIsInstance(e.previous_error, RuntimeError)
            self.assertEqual(positional_parameters, e.positional_parameters)
            self.assertEqual(keyword_parameters, e.keyword_parameters)
        else:
            self.assertTrue(False,
                            "This is supposed to be raised an exception.")
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest('The tested feature is not supported in Python {}.'.format(sys.version))

        test_filepaths = [
            'test/data/locator-aop-error-event.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_simple(self):
        alpha = self.core.get('alpha')

        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha, Alpha)
        self.assertTrue(issubclass(type(alpha), (Wrapper, Alpha)))
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest('The tested feature is not supported in Python {}.'.format(sys.version))

        self.test_filepaths = [
            'test/data/locator-strategy.xml',
        ]

        self.assembler = Assembler()

    def test_simple(self):
        self.assembler.load(*self.test_filepaths)

        container = self.assembler.core

        ctrl = container.get('strategy.controller')

        self.assertEqual(2, len(ctrl.strategy_mapping))
        self.assertEqual(2, len(ctrl.strategy_order))
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest('The tested feature is not supported in Python {}.'.format(sys.version))

        test_filepaths = [
            'test/data/locator-aop-error-event.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_no_params(self):
        alpha = self.core.get('alpha')

        try:
            alpha.init_self_destruction_1()
        except DummyException as e:
            self.assertIsInstance(e.previous_error, RuntimeError)
            self.assertFalse(e.positional_parameters)
            self.assertFalse(e.keyword_parameters)
        else:
            self.assertTrue(False, "This is supposed to be raised an exception.")

    def test_with_params(self):
        alpha = self.core.get('alpha')

        positional_parameters = (1, 2, 3)
        keyword_parameters    = {'d': 4, 'e': 5}

        try:
            alpha.init_self_destruction_2(*positional_parameters, **keyword_parameters)
        except DummyException as e:
            self.assertIsInstance(e.previous_error, RuntimeError)
            self.assertEqual(positional_parameters, e.positional_parameters)
            self.assertEqual(keyword_parameters,    e.keyword_parameters)
        else:
            self.assertTrue(False, "This is supposed to be raised an exception.")
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest(
                'The tested feature is not supported in Python {}.'.format(
                    sys.version))

        test_filepaths = [
            'test/data/locator-aop-error-event.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_simple(self):
        alpha = self.core.get('alpha')

        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha, Alpha)
        self.assertTrue(issubclass(type(alpha), (Wrapper, Alpha)))
class FunctionalTest(unittest.TestCase):
    """ This test is done via the assembler core. """
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest('The tested feature is not supported in Python {}.'.format(sys.version))

        test_filepaths = [
            'test/data/locator.xml',
            'test/data/locator-factorization.xml',
            'test/data/locator-lazy-action.xml',
            'test/data/container-callable.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_calculate_activation_sequence(self):
        alpha_metadata      = self.core.get_metadata('alpha')
        activation_sequence = self.core._calculate_activation_sequence('alpha')

        minimum_dependencies = alpha_metadata.dependencies
        common_dependencies  = set(activation_sequence).intersection(minimum_dependencies)

        self.assertEqual(len(minimum_dependencies), len(common_dependencies))

    def test_get_entity(self):
        alpha = self.core.get('alpha')

        self.assertIsNotNone(alpha)
        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha._internal_instance, Alpha)
        self.assertIsInstance(alpha.accompany, Wrapper)
        self.assertIsInstance(alpha.accompany._internal_instance, Beta)

        self.assertTrue(self.core.get_info('alpha').activated())
        self.assertTrue(self.core.get_info('beta').activated())
        self.assertFalse(self.core.get_info('charlie').activated())
        self.assertFalse(self.core.get_info('poow-1').activated())

    def test_get_factorization(self):
        alpha = self.core.get('worker.alpha')

        self.assertIsNotNone(alpha)
        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha._internal_instance, Worker)

        self.assertTrue(self.core.get_info('worker.alpha').activated())
        self.assertTrue(self.core.get_info('manager').activated())
        self.assertFalse(self.core.get_info('worker.bravo').activated())
        self.assertFalse(self.core.get_info('something').activated())

    def test_get_lambda(self):
        func_foo = self.core.get('func_foo')

        self.assertTrue(callable(func_foo))

        self.assertTrue(self.core.get_info('func_foo').activated())
        self.assertFalse(self.core.get_info('alpha').activated())
        self.assertFalse(self.core.get_info('beta').activated())
        self.assertFalse(self.core.get_info('charlie').activated())
        self.assertFalse(self.core.get_info('poow-1').activated())

        # Check if everything is still working.
        self.assertEqual(func_foo(1, 2, 3), 1)
        self.assertEqual(func_foo(1, 2, 3, 4), 2)
class FunctionalTest(unittest.TestCase):
    """ This test is done via the assembler core. """
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest(
                'The tested feature is not supported in Python {}.'.format(
                    sys.version))

        test_filepaths = [
            'test/data/locator.xml',
            'test/data/locator-factorization.xml',
            'test/data/locator-lazy-action.xml',
            'test/data/container-callable.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_calculate_activation_sequence(self):
        alpha_metadata = self.core.get_metadata('alpha')
        activation_sequence = self.core._calculate_activation_sequence('alpha')

        minimum_dependencies = alpha_metadata.dependencies
        common_dependencies = set(activation_sequence).intersection(
            minimum_dependencies)

        self.assertEqual(len(minimum_dependencies), len(common_dependencies))

    def test_get_entity(self):
        alpha = self.core.get('alpha')

        self.assertIsNotNone(alpha)
        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha._internal_instance, Alpha)
        self.assertIsInstance(alpha.accompany, Wrapper)
        self.assertIsInstance(alpha.accompany._internal_instance, Beta)

        self.assertTrue(self.core.get_info('alpha').activated())
        self.assertTrue(self.core.get_info('beta').activated())
        self.assertFalse(self.core.get_info('charlie').activated())
        self.assertFalse(self.core.get_info('poow-1').activated())

    def test_get_factorization(self):
        alpha = self.core.get('worker.alpha')

        self.assertIsNotNone(alpha)
        self.assertIsInstance(alpha, Wrapper)
        self.assertIsInstance(alpha._internal_instance, Worker)

        self.assertTrue(self.core.get_info('worker.alpha').activated())
        self.assertTrue(self.core.get_info('manager').activated())
        self.assertFalse(self.core.get_info('worker.bravo').activated())
        self.assertFalse(self.core.get_info('something').activated())

    def test_get_lambda(self):
        func_foo = self.core.get('func_foo')

        self.assertTrue(isinstance(func_foo, collections.Callable))

        self.assertTrue(self.core.get_info('func_foo').activated())
        self.assertFalse(self.core.get_info('alpha').activated())
        self.assertFalse(self.core.get_info('beta').activated())
        self.assertFalse(self.core.get_info('charlie').activated())
        self.assertFalse(self.core.get_info('poow-1').activated())

        # Check if everything is still working.
        self.assertEqual(func_foo(1, 2, 3), 1)
        self.assertEqual(func_foo(1, 2, 3, 4), 2)
class FunctionalTest(unittest.TestCase):
    """ This test is done via the assembler core. """
    def setUp(self):
        if sys.version_info < (3, 3):
            self.skipTest('The tested feature is not supported in Python {}.'.format(sys.version))

        test_filepaths = [
            'test/data/locator-aop.xml',
        ]

        self.assembler = Assembler()
        self.assembler.load(*test_filepaths)

        self.core = self.assembler.core

    def test_calculate_activation_sequence(self):
        alpha_metadata      = self.core.get_metadata('alpha')
        activation_sequence = self.core._calculate_activation_sequence('alpha')

        minimum_dependencies = alpha_metadata.dependencies
        common_dependencies  = set(activation_sequence).intersection(minimum_dependencies)

        self.assertEqual(len(minimum_dependencies), len(common_dependencies))

    def test_aop_positive(self):
        alpha = self.core.get('alpha')

        # expected_log_sequence = [
        #     # sequence as charlie cooks
        #     'Charlie: introduce itself as "Charlie"',
        #     'Alpha: order "egg and becon"',
        #     'Charlie: repeat "egg and becon"',
        #     'Alpha: confirm for egg and becon',
        #     'Charlie: respond "wilco"',
        #     'Charlie: cook',
        #
        #     # sequence as charlie serves
        #     'Alpha: speak to Beta, "watch your hand"',
        #     'Beta: acknowledge',
        #     'Alpha: wash hands',
        #     'Charlie: serve'
        # ]

        expected_log_sequence = [
            'Alpha: orders "egg"',
            'Beta: acknowledge "egg"',
            'Charlie: cook',
            'Charlie: serve',
            'Beta: says "Merci" to Charlie',
            'Alpha: says "Thank you" to Charlie',
        ]

        conversation = self.core.get('conversation')

        alpha   = self.core.get('alpha')
        charlie = self.core.get('charlie')

        charlie.cook()
        charlie.serve()

        self.assertEqual(
            len(expected_log_sequence),
            len(conversation.logs),
            'The number of sequences in the mock scenario must be the same.'
        )

        self.assertEqual(expected_log_sequence[:4], conversation.logs[:4])