Esempio n. 1
0
def attach_mock_provider(consumer, ports):
    assert isinstance(consumer, INeed)
    assert isinstance(ports, (list, dict))

    invalid_ports = set(ports).difference(consumer.get_needs())
    if invalid_ports:
        raise UnknownPort('Invalid ports for {} - {}'.format(
            consumer, ', '.join(invalid_ports)))

    # create the underlying object that will hold all the mock functions for the specified ports
    template_funcs = get_template_funcs(service=consumer)
    template_func_subset = {port: template_funcs[port] for port in ports}
    template_class = type('Mock' + consumer.__class__.__name__, (),
                          template_func_subset)
    provider_impl = mock.create_autospec(spec=template_class,
                                         spec_set=True,
                                         instance=True)

    # supply return_value if provider
    if isinstance(ports, dict):
        for port, value in ports.iteritems():
            getattr(provider_impl, port).return_value = value

    provider = object_as_provider(provider=provider_impl, ports=ports)
    auto_wire(components=[consumer, provider])

    return provider_impl
Esempio n. 2
0
    def connect_port(self, port_name, func):
        if not callable(func):
            raise WiringError('Cannot connect port to a non-callable object')
        if port_name not in self._ports:
            raise UnknownPort('"{}" is not a valid port'.format(port_name))

        for array in self._ports[port_name]:
            array.connect_port(port_name, func)
Esempio n. 3
0
    def __init__(self, component, port_name, wraps_provider=False, side_effect=None, return_value=mock.DEFAULT):
        self.component = component
        self.port_name = port_name
        self.wraps_provider = wraps_provider
        self._patches = []
        self.is_started = False
        self.side_effect = side_effect
        self.return_value = return_value

        targets = self._find_services_that_needs_port(component=component, port_name=port_name)
        if not targets:
            raise UnknownPort('Could not find instances of port "{}"'.format(port_name))

        self.provider = self._get_common_provider(services=targets, port_name=port_name)
        self.targets = targets
Esempio n. 4
0
    def validate_deps_declaration_and_usage(mcs, class_state, class_name):
        deps = class_state.get('deps', None)
        needs_ports_defined = frozenset(deps.get_ports() if deps else ())
        all_deps_used = set()

        for attr_name, member in class_state.iteritems():
            if callable(member):
                deps_used = parse_deps_used(member)
                invalid_ports = deps_used.difference(
                    needs_ports_defined).difference(RESERVED_PORT_NAMES)
                all_deps_used.update(deps_used)
                if invalid_ports:
                    raise UnknownPort(
                        '{}.{} references undeclared Needs - {}'.format(
                            class_name, attr_name,
                            ', '.join(sorted(invalid_ports))))

        unused_needs = needs_ports_defined.difference(all_deps_used)
        if unused_needs:
            raise UnusedPort('{} has unused Needs - {}'.format(
                class_name, ', '.join(sorted(unused_needs))))
Esempio n. 5
0
 def is_disconnected_port(self, port_name):
     if port_name not in self._ports:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
     port = getattr(self, port_name)
     return getattr(port, 'disconnected', False)
Esempio n. 6
0
 def disconnect_port(self, port_name):
     if port_name not in self._ports:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
     raise_not_connected = self._get_placeholder_func_for_disconnected_port(
         port_name=port_name)
     setattr(self, port_name, raise_not_connected)
Esempio n. 7
0
    def disconnect_port(self, port_name):
        if port_name not in self._ports:
            raise UnknownPort('"{}" is not a valid port'.format(port_name))

        for array in self._ports[port_name]:
            array.disconnect_port(port_name)
Esempio n. 8
0
 def get_provider_method_name(self, port_name):
     try:
         return self._providers[port_name]
     except KeyError:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
Esempio n. 9
0
 def get_provider_flags(self, port_name):
     try:
         return self._flags[port_name].copy()
     except KeyError:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
Esempio n. 10
0
 def get_provider_method_name(self, port_name):
     if port_name not in self.get_provides():
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
     else:
         return port_name
Esempio n. 11
0
 def get_provider_func(self, port_name):
     if port_name not in self.ports:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
     return getattr(self.provider, port_name)
Esempio n. 12
0
 def get_provider_func(self, port_name):
     if port_name != self.port_name:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))
     else:
         return self.provider
Esempio n. 13
0
 def _assert_valid_port(self, port_name):
     if port_name not in self._ports:
         raise UnknownPort('"{}" is not a valid port'.format(port_name))