def test_changing_connection_name_doesnt_activate_logger_if_logging_is_off(): from moler.connection import Connection moler_conn = Connection(name="ABC", logger_name=None) assert moler_conn.logger is None moler_conn.name = "DEF" assert moler_conn.logger is None
def test_changing_connection_name_doesnt_switch_logger_if_external_logger_used(): from moler.connection import Connection moler_conn = Connection(name="ABC", logger_name="conn.ABC") assert moler_conn.logger.name == "conn.ABC" moler_conn.name = "DEF" assert moler_conn.logger.name == "conn.ABC"
def test_can_attach_external_outgoing_io_to_moler_connection(): from moler.connection import Connection sent_data = [] def write_to_external_io(data): sent_data.append(data) moler_conn = Connection(how2send=write_to_external_io) moler_conn.send(data="outgoing data") assert "outgoing data" in sent_data
def test_not_attaching_external_outgoing_io_raises_exception_at_send_trial(): from moler.connection import Connection from moler.exceptions import WrongUsage moler_conn = Connection() with pytest.raises(WrongUsage) as err: moler_conn.send(data="outgoing data") assert "Can't send('outgoing data')" in str(err.value) assert "You haven't installed sending method of external-IO system" in str(err.value) assert "Do it either during connection construction: Connection(how2send=external_io_send)" in str(err.value) assert "or later via attribute direct set: connection.how2send = external_io_send" in str(err.value)
def test_can_use_default_logger_based_on_connection_name(): from moler.connection import Connection import logging moler_conn = Connection(name="ABC") assert isinstance(moler_conn.logger, logging.Logger) assert moler_conn.logger.name == "moler.connection.ABC" moler_conn = Connection() assert isinstance(moler_conn.logger, logging.Logger) assert moler_conn.logger.name == "moler.connection.{}".format(moler_conn.name)
def test_can_use_default_logger_based_on_connection_name(memory_connection_class): from moler.connection import Connection import logging moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ABC") assert isinstance(connection.logger, logging.Logger) assert connection.logger.name == "moler.connection.ABC.io" moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn) assert isinstance(connection.logger, logging.Logger) assert connection.logger.name == "moler.connection.{}.io".format(connection.name)
def test_can_attach_instance_method_as_external_outgoing_io(): from moler.connection import Connection class ExternalIO(object): def __init__(self): self.sent_data = [] def sendall(self, data): # external-IO native naming for outgoing data, f.ex. see socket self.sent_data.append(data) used_io = ExternalIO() unused_io = ExternalIO() moler_conn = Connection(how2send=used_io.sendall) moler_conn.send(data="outgoing data") assert "outgoing data" in used_io.sent_data assert "outgoing data" not in unused_io.sent_data
def test_can_encode_data_for_external_io_needs__encoder_via_composition(): from moler.connection import Connection class ExternalIO(object): def __init__(self): self.sent_data = [] def write(self, bytes): self.sent_data.append(bytes) used_io = ExternalIO() # compose Moler connection with external encoder hexlified_connection = Connection(how2send=used_io.write, encoder=lambda data: binascii.hexlify(bytearray(source=data, encoding="utf-8"))) hexlified_connection.send(data="hi") # 'h' is ASCII 0x68 'i' is ASCII 0x69 assert b'6869' in used_io.sent_data
def test_can_switch_off_logging(memory_connection_class): from moler.connection import Connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, logger_name=None) assert connection.logger is None
def test_can_assign_name_to_connection(memory_connection_class): from moler.connection import Connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ctrl_server") assert connection.name == "ctrl_server"
def test_can_use_provided_logger(): from moler.connection import Connection import logging moler_conn = Connection(logger_name="conn.XYZ") assert isinstance(moler_conn.logger, logging.Logger) assert moler_conn.logger.name == "conn.XYZ"
def test_can_use_default_logger_based_on_moler_connection_name(memory_connection_class): from moler.connection import Connection import logging moler_conn = Connection(name="ABC", logger_name="conn.DEF") connection = memory_connection_class(moler_connection=moler_conn) assert isinstance(connection.logger, logging.Logger) assert connection.logger.name == "conn.DEF.io"
def test_can_use_provided_logger(memory_connection_class): from moler.connection import Connection import logging moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, logger_name="conn.web_srv") assert isinstance(connection.logger, logging.Logger) assert connection.logger.name == "conn.web_srv"
def test_changing_connection_name_doesnt_activate_logger_if_logging_is_off(memory_connection_class): from moler.connection import Connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ABC", logger_name=None) assert connection.logger is None connection.name = "DEF" assert connection.logger is None
def test_overwrites_moler_connection_name_with_own_one(memory_connection_class): from moler.connection import Connection moler_conn = Connection() # during construction connection = memory_connection_class(moler_connection=moler_conn, name="web_srv") assert moler_conn.name == "web_srv" # during direct attribute set connection.name = "http_srv" assert moler_conn.name == "http_srv"
def test_changing_connection_name_switches_logger_if_default_logger_used(memory_connection_class): from moler.connection import Connection # default logger generated internally by connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ABC") assert connection.logger.name == "moler.connection.ABC.io" connection.name = "DEF" assert connection.logger.name == "moler.connection.DEF.io" # default logger via default naming moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ABC", logger_name="moler.connection.ABC.io") connection.name = "DEF" assert connection.logger.name == "moler.connection.DEF.io"
def test_changing_connection_name_doesnt_switch_logger_if_external_logger_used(memory_connection_class): from moler.connection import Connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn, name="ABC", logger_name="conn.ABC") assert connection.logger.name == "conn.ABC" connection.name = "DEF" assert connection.logger.name == "conn.ABC"
def test_changing_connection_name_switches_logger_if_default_logger_used(): from moler.connection import Connection # default logger generated internally by connection moler_conn = Connection(name="ABC") assert moler_conn.logger.name == "moler.connection.ABC" moler_conn.name = "DEF" assert moler_conn.logger.name == "moler.connection.DEF" # default logger via default naming moler_conn = Connection(name="ABC", logger_name="moler.connection.ABC") moler_conn.name = "DEF" assert moler_conn.logger.name == "moler.connection.DEF"
def test_uses_moler_connection_name_if_none_given(memory_connection_class): from moler.connection import Connection moler_conn = Connection() connection = memory_connection_class(moler_connection=moler_conn) assert connection.name == moler_conn.name
def test_can_assign_name_to_connection(): from moler.connection import Connection moler_conn = Connection(name="ctrl_server") assert moler_conn.name == "ctrl_server"
def test_can_switch_off_logging(): from moler.connection import Connection moler_conn = Connection(logger_name=None) assert moler_conn.logger is None
def test_can_generate_connection_name_if_none_given(): from moler.connection import Connection from moler.helpers import instance_id moler_conn = Connection() assert moler_conn.name == instance_id(moler_conn)