# Path: mcm/adapters.py
# class MasterAdapter:
# 
# 
#     def __init__(self, device):
#         self.device = device
# 
# 
#     def read(self, path):
#         content = self.device.read(path=path.absolute)
#         data = self.assemble_data(data=content)
#         for row in data:
#             logger.debug('{path} {data}'.format(path=path.absolute, data=row))
#         return data
# 
# 
#     def close(self):
#         self.device.close()
# 
# 
#     @staticmethod
#     def assemble_data(data):
#         return tuple(CmdPathRow(elem) for elem in data)
# 
# class SlaveAdapter(MasterAdapter):
# 
# 
#     def write(self, path, action, data):
#         for row in data:
#             try:
#                 self.device.write(path=path.absolute, action=action, data=row)
#                 logger.info('{path} {action} {data}'.format(path=path.absolute, action=action, data=row))
#             except WriteError as error:
#                 logger.error('Failed to send {path} {action} {data} to device: {reason}'.format(path=path.absolute, action=action, data=row, reason=error))
# 
# Path: mcm/exceptions.py
# class WriteError(Exception):
#     '''
#     Exception raised when writing to master or slave occured.
#     '''


from mock import MagicMock, patch
from mcm.adapters import MasterAdapter, SlaveAdapter
from mcm.exceptions import WriteError
import pytest

def test_MasterAdapter_calls_assemble_data(master_adapter, path):
    master_adapter.assemble_data = MagicMock()
    master_adapter.read(path=path)
    master_adapter.assemble_data.assert_called_once_with(data=master_adapter.device.read.return_value)


@pytest.mark.parametrize("adapter", (master_adapter(), slave_adapter()))
def test_adapters_device_close(adapter):
    adapter.close()
    adapter.device.close.assert_called_once_with()


@patch('mcm.adapters.CmdPathRow')
def test_assemble_data_calls_CmdPathRow(rowmock, master_adapter, data_set, data_row):
    master_adapter.assemble_data(data=data_set)
    rowmock.assert_any_call(data_row)


@patch('mcm.adapters.CmdPathRow')
def test_assemble_data_returns_tuple(rowmock, master_adapter, data_set, data_row):
    result = master_adapter.assemble_data(data=data_set)
    assert type(result) == tuple


def test_SlaveAdapter_write_calls_device_write(slave_adapter, data_set, path, data_row):
    slave_adapter.write(path=path, action='ADD', data=data_set)
    slave_adapter.device.write.assert_any_call(path=path.absolute, action='ADD', data=data_row)



# Path: mcm/adapters.py
# class MasterAdapter:
# 
# 
#     def __init__(self, device):
#         self.device = device
# 
# 
#     def read(self, path):
#         content = self.device.read(path=path.absolute)
#         data = self.assemble_data(data=content)
#         for row in data:
#             logger.debug('{path} {data}'.format(path=path.absolute, data=row))
#         return data
# 
# 
#     def close(self):
#         self.device.close()
# 
# 
#     @staticmethod
#     def assemble_data(data):
#         return tuple(CmdPathRow(elem) for elem in data)
# 
# class SlaveAdapter(MasterAdapter):
# 
# 
#     def write(self, path, action, data):
#         for row in data:
#             try:
#                 self.device.write(path=path.absolute, action=action, data=row)
#                 logger.info('{path} {action} {data}'.format(path=path.absolute, action=action, data=row))
#             except WriteError as error:
#                 logger.error('Failed to send {path} {action} {data} to device: {reason}'.format(path=path.absolute, action=action, data=row, reason=error))
# 
# Path: mcm/exceptions.py
# class WriteError(Exception):
#     '''
#     Exception raised when writing to master or slave occured.
#     '''


# Path: tests/test_adapters.py
from mock import MagicMock, patch
from mcm.adapters import MasterAdapter, SlaveAdapter
from mcm.exceptions import WriteError
import pytest


def test_MasterAdapter_calls_assemble_data(master_adapter, path):
    master_adapter.assemble_data = MagicMock()
    master_adapter.read(path=path)
    master_adapter.assemble_data.assert_called_once_with(data=master_adapter.device.read.return_value)


@pytest.mark.parametrize("adapter", (master_adapter(), slave_adapter()))
def test_adapters_device_close(adapter):
    adapter.close()
    adapter.device.close.assert_called_once_with()


@patch('mcm.adapters.CmdPathRow')
def test_assemble_data_calls_CmdPathRow(rowmock, master_adapter, data_set, data_row):
    master_adapter.assemble_data(data=data_set)
    rowmock.assert_any_call(data_row)


@patch('mcm.adapters.CmdPathRow')
def test_assemble_data_returns_tuple(rowmock, master_adapter, data_set, data_row):
    result = master_adapter.assemble_data(data=data_set)
    assert type(result) == tuple


def test_SlaveAdapter_write_calls_device_write(slave_adapter, data_set, path, data_row):
    slave_adapter.write(path=path, action='ADD', data=data_set)
    slave_adapter.device.write.assert_any_call(path=path.absolute, action='ADD', data=data_row)



def test_SlaveAdapter_write_catches_WriteError(slave_adapter, data_set, path):