Пример #1
0
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
Пример #2
0
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"
Пример #3
0
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
Пример #4
0
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)
Пример #5
0
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)
Пример #6
0
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)
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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
Пример #10
0
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"
Пример #11
0
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"
Пример #12
0
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"
Пример #13
0
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"
Пример #14
0
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
Пример #15
0
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"
Пример #16
0
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"
Пример #17
0
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"
Пример #18
0
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"
Пример #19
0
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
Пример #20
0
def test_can_assign_name_to_connection():
    from moler.connection import Connection

    moler_conn = Connection(name="ctrl_server")
    assert moler_conn.name == "ctrl_server"
Пример #21
0
def test_can_switch_off_logging():
    from moler.connection import Connection

    moler_conn = Connection(logger_name=None)
    assert moler_conn.logger is None
Пример #22
0
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)