# Path: grazyna/irc/client.py
# class IrcClient(asyncio.Protocol, IrcSender):
#     ready = False
#     config = None
#     importer = None
#     db = None
#     connection_lost_future = None
#     ping_counter = 0
# 
#     def __init__(self, config, connection_lost_future):
#         super().__init__()
#         self.config = config
#         self.importer = self.config.getmodule('main', 'importer')(self)
#         self.importer.load_all()
#         if 'db_uri' in config['main']:
#             self.db = get_engine(config['main']['db_uri'])
#         self.connection_lost_future = connection_lost_future
# 
#     def connection_made(self, transport):
#         config = self.config['main']
#         self.transport = transport
#         self.send('PASS', config['password'])
#         self.send('NICK', config['nick'])
#         self.send_msg('USER', config['ircname'], 'wr', '*', config['realname'])
# 
#     def data_received(self, raw_messages):
#         codecs = self.config.getlist('main', 'codecs')
#         for message in self._parse_raw_messages(raw_messages, codecs):
#             MessageController(self, message).execute_message()
# 
#     def connection_lost(self, exc):
#         self.connection_lost_future.set_exception(exc)
#         self.importer.cancel_tasks()
# 
#     def get_session(self):
#         session = get_session(self.db)
#         return session.scope()
# 
#     @staticmethod
#     def _parse_raw_messages(raw_messages, codecs=('utf-8',)):
#         for codec in codecs:
#             try:
#                 encoded_data = raw_messages.decode(codec)
#             except UnicodeDecodeError:
#                 continue
#             else:
#                 break
#         else:
#             return
# 
#         for match in re_message.finditer(encoded_data):
#             prefix, messages, long_message = match.groups()
#             data = [prefix] if prefix else []
#             data += messages.split()
#             if long_message:
#                 data.append(long_message)
#             yield data
# 
# Path: grazyna/irc/models.py
# class WhoisData(object):
#     __slots__ = ('idle', 'account', 'ircname', 'host',
#                  'channels', 'realname', 'server', 'nick')
# 
#     def __init__(self):
#         self.channels = []
#         self.idle = 0
#         self.nick = None
#         self.account = None
#         self.ircname = None
#         self.host = None
#         self.realname = None
# 
# Path: grazyna/test_mocks/sender.py
# class IrcSender(DefaultIrcSender):
#     ready = False
#     messages = None
#     config = None
#     db = None
#     transport = None
#     ping_counter = 0
# 
#     def __init__(self):
#         super().__init__()
#         self.messages = []
#         self.config = config = create_empty_config()
#         self.transport = Mock(spec=BaseTransport)
#         self.importer = Importer(self)
#         config.read_dict(dict(
#             main={
#                 'nick': 'bot',
#                 'importer': 'grazyna.test_mocks.importer.Importer',
#                 'command-prefix': '.',
#                 'time_to_block': '30',
#                 'executed_commands_per_time': '20',
#                 'db_uri': 'sqlite://',
#             },
#             auth=dict(
#                 module='grazyna.auths.NonAuth',
#             )
#         ))
# 
#     def send(self, *args):
#         self.messages.append(Message(*args))
# 
#     def get_session(self):
#         return get_session(self.db).scope()
# 
# class Message(list):
# 
#     def __init__(self, *args):
#         arg, *args = args
#         super().__init__([arg.upper()] + args)
# 
# Path: grazyna/config.py
# def create_empty_config():
#     return MyConfigParser(
#         comment_prefixes=[';', '//'],
#         dict_type=dict,
#     )


from asyncio import Future
from grazyna.irc.client import IrcClient as DefaultIrcClient
from grazyna.irc.models import WhoisData
from grazyna.test_mocks.sender import IrcSender, Message
from grazyna.config import create_empty_config
from unittest.mock import Mock, patch, call
import pytest
import asyncio


def make_client(**dict_cfg):
    config = create_empty_config()
    config.read_dict({'main': {
        'importer': 'grazyna.test_mocks.importer.Importer',
    }})
    config.read_dict(dict_cfg)
    return IrcClient(config, Future())


def test_init():
    client = make_client()
    assert client.importer.is_loaded is True
    assert client.importer.protocol is client


def test_connection_made():
    client = make_client(main=dict(
        nick='socek',
        password='klocek',
        ircname='superSocek',
        realname='realSocek',
    ))
    transport = Mock()
    client.connection_made(transport)
    assert client.transport is transport
    assert client.messages == [
        Message('PASS', 'klocek'),
        Message('NICK', 'socek'),

# Path: grazyna/irc/client.py
# class IrcClient(asyncio.Protocol, IrcSender):
#     ready = False
#     config = None
#     importer = None
#     db = None
#     connection_lost_future = None
#     ping_counter = 0
# 
#     def __init__(self, config, connection_lost_future):
#         super().__init__()
#         self.config = config
#         self.importer = self.config.getmodule('main', 'importer')(self)
#         self.importer.load_all()
#         if 'db_uri' in config['main']:
#             self.db = get_engine(config['main']['db_uri'])
#         self.connection_lost_future = connection_lost_future
# 
#     def connection_made(self, transport):
#         config = self.config['main']
#         self.transport = transport
#         self.send('PASS', config['password'])
#         self.send('NICK', config['nick'])
#         self.send_msg('USER', config['ircname'], 'wr', '*', config['realname'])
# 
#     def data_received(self, raw_messages):
#         codecs = self.config.getlist('main', 'codecs')
#         for message in self._parse_raw_messages(raw_messages, codecs):
#             MessageController(self, message).execute_message()
# 
#     def connection_lost(self, exc):
#         self.connection_lost_future.set_exception(exc)
#         self.importer.cancel_tasks()
# 
#     def get_session(self):
#         session = get_session(self.db)
#         return session.scope()
# 
#     @staticmethod
#     def _parse_raw_messages(raw_messages, codecs=('utf-8',)):
#         for codec in codecs:
#             try:
#                 encoded_data = raw_messages.decode(codec)
#             except UnicodeDecodeError:
#                 continue
#             else:
#                 break
#         else:
#             return
# 
#         for match in re_message.finditer(encoded_data):
#             prefix, messages, long_message = match.groups()
#             data = [prefix] if prefix else []
#             data += messages.split()
#             if long_message:
#                 data.append(long_message)
#             yield data
# 
# Path: grazyna/irc/models.py
# class WhoisData(object):
#     __slots__ = ('idle', 'account', 'ircname', 'host',
#                  'channels', 'realname', 'server', 'nick')
# 
#     def __init__(self):
#         self.channels = []
#         self.idle = 0
#         self.nick = None
#         self.account = None
#         self.ircname = None
#         self.host = None
#         self.realname = None
# 
# Path: grazyna/test_mocks/sender.py
# class IrcSender(DefaultIrcSender):
#     ready = False
#     messages = None
#     config = None
#     db = None
#     transport = None
#     ping_counter = 0
# 
#     def __init__(self):
#         super().__init__()
#         self.messages = []
#         self.config = config = create_empty_config()
#         self.transport = Mock(spec=BaseTransport)
#         self.importer = Importer(self)
#         config.read_dict(dict(
#             main={
#                 'nick': 'bot',
#                 'importer': 'grazyna.test_mocks.importer.Importer',
#                 'command-prefix': '.',
#                 'time_to_block': '30',
#                 'executed_commands_per_time': '20',
#                 'db_uri': 'sqlite://',
#             },
#             auth=dict(
#                 module='grazyna.auths.NonAuth',
#             )
#         ))
# 
#     def send(self, *args):
#         self.messages.append(Message(*args))
# 
#     def get_session(self):
#         return get_session(self.db).scope()
# 
# class Message(list):
# 
#     def __init__(self, *args):
#         arg, *args = args
#         super().__init__([arg.upper()] + args)
# 
# Path: grazyna/config.py
# def create_empty_config():
#     return MyConfigParser(
#         comment_prefixes=[';', '//'],
#         dict_type=dict,
#     )


# Path: grazyna/tests/test_client.py
from asyncio import Future
from grazyna.irc.client import IrcClient as DefaultIrcClient
from grazyna.irc.models import WhoisData
from grazyna.test_mocks.sender import IrcSender, Message
from grazyna.config import create_empty_config
from unittest.mock import Mock, patch, call
import pytest
import asyncio



def make_client(**dict_cfg):
    config = create_empty_config()
    config.read_dict({'main': {
        'importer': 'grazyna.test_mocks.importer.Importer',
    }})
    config.read_dict(dict_cfg)
    return IrcClient(config, Future())


def test_init():
    client = make_client()
    assert client.importer.is_loaded is True
    assert client.importer.protocol is client


def test_connection_made():
    client = make_client(main=dict(
        nick='socek',
        password='klocek',
        ircname='superSocek',
        realname='realSocek',
    ))
    transport = Mock()
    client.connection_made(transport)
    assert client.transport is transport
    assert client.messages == [
        Message('PASS', 'klocek'),
        Message('NICK', 'socek'),

        Message('USER', 'superSocek', 'wr', '*', ':realSocek'),