# 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
# -*- coding: UTF-8 -*-





def test_trap_string_in_sentences_raises_exception():
    with pytest.raises(CmdError):
        trapCheck(('!trap','=key=value'))


def test_raises_when_fatal():
    with pytest.raises(ConnError):
        raiseIfFatal(('!fatal','connection terminated by remote hoost'))


def test_does_not_raise_exception_if_no_fatal_string():

# 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

# -*- coding: UTF-8 -*-





def test_trap_string_in_sentences_raises_exception():
    with pytest.raises(CmdError):
        trapCheck(('!trap','=key=value'))


def test_raises_when_fatal():
    with pytest.raises(ConnError):
        raiseIfFatal(('!fatal','connection terminated by remote hoost'))


def test_does_not_raise_exception_if_no_fatal_string():

    raiseIfFatal( 'some string without error' )