# Path: mcm/iodevices.py
# class RouterOsAPIDevice(ReadOnlyRouterOS):
# 
# 
#     def write(self, path, action, data):
#         command = self.cmd_action_join(path=path, action=action)
#         method = getattr(self, action)
#         method(command=command, data=data)
# 
# 
#     def DEL(self, command, data):
#         ID = {'.id':data['.id']}
#         try:
#             self.api.run(cmd=command, args=ID)
#         except CmdError as error:
#             raise WriteError(error)
# 
# 
#     def SET(self, command, data):
#         try:
#             self.api.run(cmd=command, args=data)
#         except CmdError as error:
#             raise WriteError(error)
# 
# 
#     def ADD(self, command, data):
#         try:
#             self.api.run(cmd=command, args=data)
#         except CmdError as error:
#             raise WriteError(error)
# 
# class StaticConfig:
# 
# 
#     def __init__(self, data):
#         self.data = data
# 
# 
#     def read(self, path):
#         for section in self.data:
#             if section['path'] == path:
#                 return section['rules']
# 
# 
#     def close(self):
#         pass
# 
# class ReadOnlyRouterOS:
# 
# 
#     def __init__(self, api):
#         self.api = api
# 
# 
#     def read(self, path):
#         cmd = self.cmd_action_join(path=path, action='GET')
#         try:
#             data = self.api.run(cmd=cmd)
#         except CmdError as error:
#             raise ReadError(error)
#         return self.filter_dynamic(data)
# 
# 
#     def write(self, path, action, data):
#         pass
# 
# 
#     def close(self):
#         self.api.close()
# 
# 
#     @staticmethod
#     def cmd_action_join(path, action):
#         actions = dict(ADD='add', SET='set', DEL='remove', GET='getall')
#         return pjoin(path, actions[action])
# 
# 
#     @staticmethod
#     def filter_dynamic(data):
#         return tuple(row for row in data if not row.get('dynamic'))
# 
# Path: mcm/librouteros/exceptions.py
# class CmdError( LibError ):
#     '''
#     Commend execution errors.
#     '''
# 
# Path: mcm/exceptions.py
# class ReadError(Exception):
#     '''
#     Exception raised when reading from master or slave occured.
#     '''
# 
# class WriteError(Exception):
#     '''
#     Exception raised when writing to master or slave occured.
#     '''


from mock import MagicMock, patch
from unittest import TestCase
from inspect import ismethod
from mcm.iodevices import RouterOsAPIDevice, StaticConfig, ReadOnlyRouterOS
from mcm.librouteros import CmdError
from mcm.exceptions import ReadError, WriteError
import pytest
        self.TestCls = StaticConfig(data=data)

    def test_read_returns_valid_rules(self):
        returned = self.TestCls.read(path='test_path')
        assert returned == [{'key':'value'}]

    def test_has_close_method(self):
        assert ismethod(self.TestCls.close)



class RouterOsAPIDevice_Tests(TestCase):

    def setUp(self):
        self.TestCls = RouterOsAPIDevice(api=MagicMock())
        self.pathmock = MagicMock()
        self.datamock = MagicMock()

    @patch.object(RouterOsAPIDevice, 'filter_dynamic')
    @patch.object(RouterOsAPIDevice, 'cmd_action_join')
    def test_read_calls_api_run_method(self, joinmock, filter_mock):
        self.TestCls.read(self.pathmock)
        self.TestCls.api.run.assert_called_once_with(cmd=joinmock.return_value)

    @patch.object(RouterOsAPIDevice, 'cmd_action_join')
    def test_read_raises_ReadError(self, joinmock):
        self.TestCls.api.run.side_effect = CmdError
        with pytest.raises(ReadError):
            self.TestCls.read(path=MagicMock())


# Path: mcm/iodevices.py
# class RouterOsAPIDevice(ReadOnlyRouterOS):
# 
# 
#     def write(self, path, action, data):
#         command = self.cmd_action_join(path=path, action=action)
#         method = getattr(self, action)
#         method(command=command, data=data)
# 
# 
#     def DEL(self, command, data):
#         ID = {'.id':data['.id']}
#         try:
#             self.api.run(cmd=command, args=ID)
#         except CmdError as error:
#             raise WriteError(error)
# 
# 
#     def SET(self, command, data):
#         try:
#             self.api.run(cmd=command, args=data)
#         except CmdError as error:
#             raise WriteError(error)
# 
# 
#     def ADD(self, command, data):
#         try:
#             self.api.run(cmd=command, args=data)
#         except CmdError as error:
#             raise WriteError(error)
# 
# class StaticConfig:
# 
# 
#     def __init__(self, data):
#         self.data = data
# 
# 
#     def read(self, path):
#         for section in self.data:
#             if section['path'] == path:
#                 return section['rules']
# 
# 
#     def close(self):
#         pass
# 
# class ReadOnlyRouterOS:
# 
# 
#     def __init__(self, api):
#         self.api = api
# 
# 
#     def read(self, path):
#         cmd = self.cmd_action_join(path=path, action='GET')
#         try:
#             data = self.api.run(cmd=cmd)
#         except CmdError as error:
#             raise ReadError(error)
#         return self.filter_dynamic(data)
# 
# 
#     def write(self, path, action, data):
#         pass
# 
# 
#     def close(self):
#         self.api.close()
# 
# 
#     @staticmethod
#     def cmd_action_join(path, action):
#         actions = dict(ADD='add', SET='set', DEL='remove', GET='getall')
#         return pjoin(path, actions[action])
# 
# 
#     @staticmethod
#     def filter_dynamic(data):
#         return tuple(row for row in data if not row.get('dynamic'))
# 
# Path: mcm/librouteros/exceptions.py
# class CmdError( LibError ):
#     '''
#     Commend execution errors.
#     '''
# 
# Path: mcm/exceptions.py
# class ReadError(Exception):
#     '''
#     Exception raised when reading from master or slave occured.
#     '''
# 
# class WriteError(Exception):
#     '''
#     Exception raised when writing to master or slave occured.
#     '''


# Path: tests/test_iodevices.py
from mock import MagicMock, patch
from unittest import TestCase
from inspect import ismethod
from mcm.iodevices import RouterOsAPIDevice, StaticConfig, ReadOnlyRouterOS
from mcm.librouteros import CmdError
from mcm.exceptions import ReadError, WriteError
import pytest

        self.TestCls = StaticConfig(data=data)

    def test_read_returns_valid_rules(self):
        returned = self.TestCls.read(path='test_path')
        assert returned == [{'key':'value'}]

    def test_has_close_method(self):
        assert ismethod(self.TestCls.close)



class RouterOsAPIDevice_Tests(TestCase):

    def setUp(self):
        self.TestCls = RouterOsAPIDevice(api=MagicMock())
        self.pathmock = MagicMock()
        self.datamock = MagicMock()

    @patch.object(RouterOsAPIDevice, 'filter_dynamic')
    @patch.object(RouterOsAPIDevice, 'cmd_action_join')
    def test_read_calls_api_run_method(self, joinmock, filter_mock):
        self.TestCls.read(self.pathmock)
        self.TestCls.api.run.assert_called_once_with(cmd=joinmock.return_value)

    @patch.object(RouterOsAPIDevice, 'cmd_action_join')
    def test_read_raises_ReadError(self, joinmock):
        self.TestCls.api.run.side_effect = CmdError
        with pytest.raises(ReadError):
            self.TestCls.read(path=MagicMock())


    @patch.object(RouterOsAPIDevice, 'filter_dynamic')