class ParametrizedAnnotationTest(unittest.TestCase): def setUp(self): self.registry = Registry() def test_whenUsingParametrizedAnnotation_thenWrappedObjectsAreSavedInTheRegistry( self): wrapped_function = self.registry.get_annotated_in_package( myAnnotation, "")[0].registered self.assertEqual(some_wrapped_function, wrapped_function) def test_whenUsingParametrizedAnnotation_thenParametersAreStoredInTheRegistration( self): registration = self.registry.get_annotated_in_package( myAnnotation, "")[0] self.assertEqual(A_MESSAGE, registration.arguments['message']) self.assertFalse("value" in registration.arguments) def test_givenImplicitParameterName_whenUsingParametrizedAnnotation_thenParameterIsSavedAsTheDeclaredParameterName( self): registration = self.registry.get_annotated_in_package( implicit_parameter_annotation, "")[0] self.assertEqual(A_MESSAGE, registration.arguments['path']) def test_givenKeywordOnlyArguments_whenUsingParametrizedAnnotation_thenParametersAreSavedInTheRegistration( self): registration = self.registry.get_annotated_in_package( multipleParameterAnnotation, "")[0] self.assertEqual(A_MESSAGE, registration.arguments['message']) self.assertEqual(A_ROUTE, registration.arguments['route']) def test_givenMissingParameters_whenUsingOptionalArguments_thenPassNoneInstead( self): registration = self.registry.get_annotated_in_package( annotationWithOptionalParameters, "")[0] self.assertEqual(A_MESSAGE, registration.arguments['mandatory_parameter']) self.assertIsNone(registration.arguments['optional_parameter'])
def __init__(self, url_pattern: str, registry: Registry, root_package_name: str, regex_pattern: str = None): registrations = registry.get_annotated_in_package( RequestFilter, root_package_name) super().__init__( url_pattern, Stream(registrations).map(lambda r: r.registered).toList(), regex_pattern)
def __init__(self, registry: Registry, resources: List[Registration]): super().__init__() for resource in resources: for primitive in http_methods: routable_functions = registry.get_annotated_in_package(primitive, resource.registered.__module__) sub_paths = registry.get_annotated_in_package(Path, resource.registered.__module__) for route_function in routable_functions: route_sub_path = Stream(sub_paths).firstMatch(lambda r: r.registered == route_function.registered) resource_path = resource.arguments['value'] if not route_sub_path.isPresent(): self.register_route(primitive, resource_path, resource.registered, route_function.registered) else: sub_path = route_sub_path.get().arguments['value'] path = resource_path + sub_path if \ resource_path.endswith('/') or sub_path.startswith('/') \ else resource_path + '/' + sub_path self.register_route(primitive, path, resource.registered, route_function.registered)
def _find_dispatchers( self, registry: Registry, root_package_name: str, service_locator: ServiceLocator) -> List[MessageDispatcher]: dispatchers = [] registrations = registry.get_annotated_in_package( EventHandler, root_package_name) for registration in registrations: if registration.is_function_registration(): dispatchers.append( MessageDispatcherFunction( registration.arguments[EVENT_NAME_PARAMETER], registration.registered)) elif registration.is_class_registration() and issubclass( registration.registered, Runnable): dispatchers.append( MessageDispatcherRunnable( registration.arguments[EVENT_NAME_PARAMETER], registration.registered, self.service_locator)) for registration in registry.get_annotated_in_package( EventHandlerClass, root_package_name): registered_class = registration.registered for handler_function in registry.get_annotated_in_package( EventHandler, registered_class.__module__): if self._is_function_defined_inside_class( handler_function.registered, registered_class): dispatchers.append( JitMessageDispatcherClass( handler_function.arguments[EVENT_NAME_PARAMETER], registered_class, handler_function.registered, service_locator)) return dispatchers
def __init__(self, registry: Registry, root_package_name: str): resources = registry.get_annotated_in_package(Resource, root_package_name) super().__init__(registry, resources)