# Path: mcm/librouteros/datastructures.py
# def parsresp(sentences):
#     '''
#     Parse given response to set of dictionaries containing parsed sentences.
# 
#     :praam sentences: Iterable (tuple,list) with each element as a sentence
#     '''
# 
#     response = map( parsnt, sentences )
#     # filter out empty sentences
#     filtered = filter( None, response )
#     return tuple( filtered )
# 
# def parsnt( snt ):
#     '''
#     Parse given sentence from attribute words to dictionary.
#     Only retrieve words that start with '='.
#     This method may return an empty dictionary.
# 
#     :param snt: Iterable sentence.
#     :returns: Dictionary with attributes.
#     '''
# 
#     attrs = ( word for word in snt if word.startswith( '=' ) )
#     attrs = map( convattrwrd, attrs )
# 
#     return dict( attrs )
# 
# def mksnt( data ):
#     '''
#     Convert dictionary to attribute words.
# 
#     :param args: Dictionary with key value attributes.
#     Those will be converted to attribute words.
# 
#     :returns: Tuple with attribute words.
#     '''
# 
#     converted = map( mkattrwrd, data.items() )
#     return tuple( converted )
# 
# def mkattrwrd( kv ):
#     '''
#     Make an attribute word out of key value tuple. Values and keys are automaticly casted
#     to api equivalents.
# 
#     :param kv: Two element tuple. First key, second value.
#     '''
# 
#     key, value = kv
#     casted_value = castValToApi( value )
#     prefix = '' if key == '.tag' else '='
# 
#     return '{prefix}{key}={value}'.format( prefix=prefix, key=key, value=casted_value )
# 
# def convattrwrd( word ):
#     '''
#     Make an key value tuple out of attribute word. Values and keys
#     are automaticly casted to python equivalents.
# 
#     :param word: Attribute word.
#     '''
# 
#     splitted = word.split( '=', 2 )
#     key = splitted[1]
#     value = splitted[2]
#     casted_value = castValToPy( value )
# 
#     return ( key, casted_value )
# 
# def castValToPy( value ):
# 
#     try:
#         casted = int( value )
#     except ValueError:
#         casted = to_py_mapping.get( value, value )
#     return casted
# 
# def castValToApi( value ):
# 
#     if type(value) == int:
#         return str(value)
#     else:
#         return to_api_mapping.get( value, str( value ) )
# 
# def raiseIfFatal( sentence ):
#     '''
#     Check if a given sentence contains error message. If it does then raise an exception.
#     !fatal means that connection have been closed and no further transmission will work.
#     '''
# 
#     if '!fatal' in sentence:
#         error = ', '.join( word for word in sentence if word != '!fatal' )
#         raise ConnError( error )
# 
# def trapCheck( snts ):
#     '''
#     Check for existance of !trap word. This word indicates that an error occured.
#     At least one !trap word in any sentence triggers an error.
#     Unfortunatelly mikrotik api may throw one or more errors as response.
# 
#     :param snts: Iterable with each sentence as tuple
#     '''
# 
#     errmsgs = []
# 
#     for snt in snts:
#         if '!trap' in snt:
#             emsg = ' '.join( word for word in snt )
#             errmsgs.append( emsg )
# 
#     if errmsgs:
#         e = ', '.join( errmsgs )
#         raise CmdError( e )
# 
# Path: mcm/librouteros/exceptions.py
# class CmdError( LibError ):
#     '''
#     Commend execution errors.
#     '''
# 
# class ConnError( LibError ):
#     '''
#     Connection related errors.
#     '''


import pytest
from mcm.librouteros.datastructures import parsresp, parsnt, mksnt, mkattrwrd, convattrwrd, castValToPy, castValToApi, raiseIfFatal, trapCheck
from mcm.librouteros.exceptions import CmdError, ConnError
        (None, ''),
        ('string', 'string'),
        ('22.2', '22.2'),
        (22.2, '22.2'),
        (22, '22'),
        (1, '1'),
        (0, '0')
        ))
def test_casting_from_python_to_api(python_type, api_type):
    assert castValToApi(python_type) == api_type

api_words = (
        '=key=value',
        '=.id=value',
        )
key_value_tuples = (
        ('key','value'),
        ('.id','value'),
        )

@pytest.mark.parametrize("word,expected", zip(api_words,key_value_tuples))
def test_converting_api_word_to_tuple(word, expected):
    assert convattrwrd(word) == expected


@pytest.mark.parametrize("word,pair", zip(api_words,key_value_tuples))
def test_converting_tuple_to_api_word(word,pair):
    assert mkattrwrd(pair) == word



# Path: mcm/librouteros/datastructures.py
# def parsresp(sentences):
#     '''
#     Parse given response to set of dictionaries containing parsed sentences.
# 
#     :praam sentences: Iterable (tuple,list) with each element as a sentence
#     '''
# 
#     response = map( parsnt, sentences )
#     # filter out empty sentences
#     filtered = filter( None, response )
#     return tuple( filtered )
# 
# def parsnt( snt ):
#     '''
#     Parse given sentence from attribute words to dictionary.
#     Only retrieve words that start with '='.
#     This method may return an empty dictionary.
# 
#     :param snt: Iterable sentence.
#     :returns: Dictionary with attributes.
#     '''
# 
#     attrs = ( word for word in snt if word.startswith( '=' ) )
#     attrs = map( convattrwrd, attrs )
# 
#     return dict( attrs )
# 
# def mksnt( data ):
#     '''
#     Convert dictionary to attribute words.
# 
#     :param args: Dictionary with key value attributes.
#     Those will be converted to attribute words.
# 
#     :returns: Tuple with attribute words.
#     '''
# 
#     converted = map( mkattrwrd, data.items() )
#     return tuple( converted )
# 
# def mkattrwrd( kv ):
#     '''
#     Make an attribute word out of key value tuple. Values and keys are automaticly casted
#     to api equivalents.
# 
#     :param kv: Two element tuple. First key, second value.
#     '''
# 
#     key, value = kv
#     casted_value = castValToApi( value )
#     prefix = '' if key == '.tag' else '='
# 
#     return '{prefix}{key}={value}'.format( prefix=prefix, key=key, value=casted_value )
# 
# def convattrwrd( word ):
#     '''
#     Make an key value tuple out of attribute word. Values and keys
#     are automaticly casted to python equivalents.
# 
#     :param word: Attribute word.
#     '''
# 
#     splitted = word.split( '=', 2 )
#     key = splitted[1]
#     value = splitted[2]
#     casted_value = castValToPy( value )
# 
#     return ( key, casted_value )
# 
# def castValToPy( value ):
# 
#     try:
#         casted = int( value )
#     except ValueError:
#         casted = to_py_mapping.get( value, value )
#     return casted
# 
# def castValToApi( value ):
# 
#     if type(value) == int:
#         return str(value)
#     else:
#         return to_api_mapping.get( value, str( value ) )
# 
# def raiseIfFatal( sentence ):
#     '''
#     Check if a given sentence contains error message. If it does then raise an exception.
#     !fatal means that connection have been closed and no further transmission will work.
#     '''
# 
#     if '!fatal' in sentence:
#         error = ', '.join( word for word in sentence if word != '!fatal' )
#         raise ConnError( error )
# 
# def trapCheck( snts ):
#     '''
#     Check for existance of !trap word. This word indicates that an error occured.
#     At least one !trap word in any sentence triggers an error.
#     Unfortunatelly mikrotik api may throw one or more errors as response.
# 
#     :param snts: Iterable with each sentence as tuple
#     '''
# 
#     errmsgs = []
# 
#     for snt in snts:
#         if '!trap' in snt:
#             emsg = ' '.join( word for word in snt )
#             errmsgs.append( emsg )
# 
#     if errmsgs:
#         e = ', '.join( errmsgs )
#         raise CmdError( e )
# 
# Path: mcm/librouteros/exceptions.py
# class CmdError( LibError ):
#     '''
#     Commend execution errors.
#     '''
# 
# class ConnError( LibError ):
#     '''
#     Connection related errors.
#     '''


# Path: tests/test_routeros_datasrtuctures.py
import pytest
from mcm.librouteros.datastructures import parsresp, parsnt, mksnt, mkattrwrd, convattrwrd, castValToPy, castValToApi, raiseIfFatal, trapCheck
from mcm.librouteros.exceptions import CmdError, ConnError

        (None, ''),
        ('string', 'string'),
        ('22.2', '22.2'),
        (22.2, '22.2'),
        (22, '22'),
        (1, '1'),
        (0, '0')
        ))
def test_casting_from_python_to_api(python_type, api_type):
    assert castValToApi(python_type) == api_type

api_words = (
        '=key=value',
        '=.id=value',
        )
key_value_tuples = (
        ('key','value'),
        ('.id','value'),
        )

@pytest.mark.parametrize("word,expected", zip(api_words,key_value_tuples))
def test_converting_api_word_to_tuple(word, expected):
    assert convattrwrd(word) == expected


@pytest.mark.parametrize("word,pair", zip(api_words,key_value_tuples))
def test_converting_tuple_to_api_word(word,pair):
    assert mkattrwrd(pair) == word



@pytest.mark.parametrize("sentence,api_sentence", (