# Path: mcm/configurators.py
# class CmdPathConfigurator:
# 
# 
#     def __init__(self, configurator, path, comparator, addfunc, setfunc, delfunc):
#         self.configurator = configurator
#         self.path = path
#         self.comparator = comparator
#         self.ADD = MethodType( addfunc, self )
#         self.DEL = MethodType( delfunc, self )
#         self.SET = MethodType( setfunc, self )
# 
# 
#     @classmethod
#     def with_ensure(cls, configurator, path):
#         comparator = get_comparator(path=path)
#         return cls(configurator=configurator, path=path, comparator=comparator,
#                 addfunc=real_action, setfunc=real_action, delfunc=no_action)
# 
#     @classmethod
#     def with_exact(cls, configurator, path):
#         comparator = get_comparator(path=path)
#         return cls(configurator=configurator, path=path, comparator=comparator,
#                 addfunc=real_action, setfunc=real_action, delfunc=real_action)
# 
# 
#     def run(self):
# 
#         data = self.readData()
#         result = self.compareData(data)
#         self.applyData(data=result)
# 
# 
#     def applyData(self, data):
# 
#         for action in self.path.modord:
#             action_data = self.extartActionData( data=data, action=action )
#             action_method = getattr(self, action)
#             action_method(action, action_data)
# 
# 
#     def compareData(self, data):
# 
#         master_data, slave_data = data
#         return self.comparator.compare(wanted=master_data, present=slave_data)
# 
# 
#     def readData(self):
# 
#         master_data = self.configurator.master.read( path=self.path )
#         slave_data = self.configurator.slave.read( path=self.path )
#         return master_data, slave_data
# 
# 
#     @staticmethod
#     def extartActionData(data, action):
# 
#         ADD, SET, DEL = data
#         action_map = {'ADD':ADD, 'SET':SET, 'DEL':DEL}
#         return action_map[action]
# 
# def real_action(self, action, data):
# 
#     self.configurator.slave.write(data=data, path=self.path, action=action)
# 
# def no_action(self, action, data):
#     pass
# 
# class Configurator:
# 
# 
#     def __init__(self, master, slave):
#         self.master = master
#         self.slave = slave
# 
# 
#     def run(self, paths):
#         for path in paths:
#             configurator = self.getPathConfigurator(path=path)
#             try:
#                 configurator.run()
#             except ReadError as error:
#                 msg = 'Failed to read {path} {reason}'.format(path=path.absolute, reason=error)
#                 logger.error(msg)
#             except ConnError as error:
#                 msg = 'Connection broken {reason}'.format(reason=error)
#                 logger.error(msg)
#                 break
# 
#         self.master.close()
#         self.slave.close()
# 
# 
#     def getPathConfigurator(self, path):
#         strategy = 'with_' + path.strategy
#         constructor = getattr(CmdPathConfigurator, strategy)
#         return constructor(configurator=self, path=path)
# 
# Path: mcm/exceptions.py
# class ReadError(Exception):
#     '''
#     Exception raised when reading from master or slave occured.
#     '''
# 
# Path: mcm/librouteros/exceptions.py
# class ConnError( LibError ):
#     '''
#     Connection related errors.
#     '''


from mock import MagicMock, patch
from unittest import TestCase
from mcm.configurators import CmdPathConfigurator, real_action, no_action, Configurator
from mcm.exceptions import ReadError
from mcm.librouteros.exceptions import ConnError
    def test_readData_calls_master_read(self):
        self.TestCls.readData()
        self.TestCls.configurator.master.read.assert_called_once_with(path=self.TestCls.path)


    def test_extractActionData_returns_ADD_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='ADD')
        self.assertEqual(returned, ADD)

    def test_extractActionData_returns_SET_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='SET')
        self.assertEqual(returned, SET)

    def test_extractActionData_returns_DEL_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='DEL')
        self.assertEqual(returned, DEL)



@patch('mcm.configurators.get_comparator')
@patch.object(CmdPathConfigurator, '__init__', return_value=None)
class Strategy_Factory_Tests(TestCase):

    def setUp(self):
        self.path, self.configurator = MagicMock(), MagicMock()

    def test_with_ensure_calls_init_with_appropriate_methods(self, initmock, cmpmock):

# Path: mcm/configurators.py
# class CmdPathConfigurator:
# 
# 
#     def __init__(self, configurator, path, comparator, addfunc, setfunc, delfunc):
#         self.configurator = configurator
#         self.path = path
#         self.comparator = comparator
#         self.ADD = MethodType( addfunc, self )
#         self.DEL = MethodType( delfunc, self )
#         self.SET = MethodType( setfunc, self )
# 
# 
#     @classmethod
#     def with_ensure(cls, configurator, path):
#         comparator = get_comparator(path=path)
#         return cls(configurator=configurator, path=path, comparator=comparator,
#                 addfunc=real_action, setfunc=real_action, delfunc=no_action)
# 
#     @classmethod
#     def with_exact(cls, configurator, path):
#         comparator = get_comparator(path=path)
#         return cls(configurator=configurator, path=path, comparator=comparator,
#                 addfunc=real_action, setfunc=real_action, delfunc=real_action)
# 
# 
#     def run(self):
# 
#         data = self.readData()
#         result = self.compareData(data)
#         self.applyData(data=result)
# 
# 
#     def applyData(self, data):
# 
#         for action in self.path.modord:
#             action_data = self.extartActionData( data=data, action=action )
#             action_method = getattr(self, action)
#             action_method(action, action_data)
# 
# 
#     def compareData(self, data):
# 
#         master_data, slave_data = data
#         return self.comparator.compare(wanted=master_data, present=slave_data)
# 
# 
#     def readData(self):
# 
#         master_data = self.configurator.master.read( path=self.path )
#         slave_data = self.configurator.slave.read( path=self.path )
#         return master_data, slave_data
# 
# 
#     @staticmethod
#     def extartActionData(data, action):
# 
#         ADD, SET, DEL = data
#         action_map = {'ADD':ADD, 'SET':SET, 'DEL':DEL}
#         return action_map[action]
# 
# def real_action(self, action, data):
# 
#     self.configurator.slave.write(data=data, path=self.path, action=action)
# 
# def no_action(self, action, data):
#     pass
# 
# class Configurator:
# 
# 
#     def __init__(self, master, slave):
#         self.master = master
#         self.slave = slave
# 
# 
#     def run(self, paths):
#         for path in paths:
#             configurator = self.getPathConfigurator(path=path)
#             try:
#                 configurator.run()
#             except ReadError as error:
#                 msg = 'Failed to read {path} {reason}'.format(path=path.absolute, reason=error)
#                 logger.error(msg)
#             except ConnError as error:
#                 msg = 'Connection broken {reason}'.format(reason=error)
#                 logger.error(msg)
#                 break
# 
#         self.master.close()
#         self.slave.close()
# 
# 
#     def getPathConfigurator(self, path):
#         strategy = 'with_' + path.strategy
#         constructor = getattr(CmdPathConfigurator, strategy)
#         return constructor(configurator=self, path=path)
# 
# Path: mcm/exceptions.py
# class ReadError(Exception):
#     '''
#     Exception raised when reading from master or slave occured.
#     '''
# 
# Path: mcm/librouteros/exceptions.py
# class ConnError( LibError ):
#     '''
#     Connection related errors.
#     '''


# Path: tests/test_configurators.py
from mock import MagicMock, patch
from unittest import TestCase
from mcm.configurators import CmdPathConfigurator, real_action, no_action, Configurator
from mcm.exceptions import ReadError
from mcm.librouteros.exceptions import ConnError

    def test_readData_calls_master_read(self):
        self.TestCls.readData()
        self.TestCls.configurator.master.read.assert_called_once_with(path=self.TestCls.path)


    def test_extractActionData_returns_ADD_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='ADD')
        self.assertEqual(returned, ADD)

    def test_extractActionData_returns_SET_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='SET')
        self.assertEqual(returned, SET)

    def test_extractActionData_returns_DEL_data_when_requested(self):
        data = ADD, SET, DEL = ( MagicMock(), MagicMock(), MagicMock() )
        returned = self.TestCls.extartActionData(data=data, action='DEL')
        self.assertEqual(returned, DEL)



@patch('mcm.configurators.get_comparator')
@patch.object(CmdPathConfigurator, '__init__', return_value=None)
class Strategy_Factory_Tests(TestCase):

    def setUp(self):
        self.path, self.configurator = MagicMock(), MagicMock()

    def test_with_ensure_calls_init_with_appropriate_methods(self, initmock, cmpmock):

        CmdPathConfigurator.with_ensure(path=self.path, configurator=self.configurator)