@ -9,7 +9,7 @@ import pytest
from jinja2 import Environment
import ansible . plugins . filter . mathstuff as ms
from ansible . errors import AnsibleFilterError
from ansible . errors import AnsibleFilterError , AnsibleFilterTypeError
UNIQUE_DATA = ( ( [ 1 , 3 , 4 , 2 ] , sorted ( [ 1 , 2 , 3 , 4 ] ) ) ,
@ -79,9 +79,9 @@ class TestMax:
class TestLogarithm :
def test_log_non_number ( self ) :
# Message changed in python3.6
with pytest . raises ( AnsibleFilter Error, match = ' log \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
with pytest . raises ( AnsibleFilter Type Error, match = ' log \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
ms . logarithm ( ' a ' )
with pytest . raises ( AnsibleFilter Error, match = ' log \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
with pytest . raises ( AnsibleFilter Type Error, match = ' log \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
ms . logarithm ( 10 , base = ' a ' )
def test_log_ten ( self ) :
@ -98,10 +98,10 @@ class TestLogarithm:
class TestPower :
def test_power_non_number ( self ) :
# Message changed in python3.6
with pytest . raises ( AnsibleFilter Error, match = ' pow \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
with pytest . raises ( AnsibleFilter Type Error, match = ' pow \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
ms . power ( ' a ' , 10 )
with pytest . raises ( AnsibleFilter Error, match = ' pow \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
with pytest . raises ( AnsibleFilter Type Error, match = ' pow \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) ' ) :
ms . power ( 10 , ' a ' )
def test_power_squared ( self ) :
@ -114,13 +114,13 @@ class TestPower:
class TestInversePower :
def test_root_non_number ( self ) :
# Messages differed in python-2.6, python-2.7-3.5, and python-3.6+
with pytest . raises ( AnsibleFilter Error, match = " root \\ ( \\ ) can only be used on numbers: "
with pytest . raises ( AnsibleFilter Type Error, match = " root \\ ( \\ ) can only be used on numbers: "
" (invalid literal for float \\ ( \\ ): a "
" |could not convert string to float: a "
" |could not convert string to float: ' a ' ) " ) :
ms . inversepower ( 10 , ' a ' )
with pytest . raises ( AnsibleFilter Error, match = " root \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) " ) :
with pytest . raises ( AnsibleFilter Type Error, match = " root \\ ( \\ ) can only be used on numbers: (a float is required|must be real number, not str) " ) :
ms . inversepower ( ' a ' , 10 )
def test_square_root ( self ) :
@ -145,27 +145,27 @@ class TestRekeyOnMember():
# (Input data structure, member to rekey on, expected error message)
INVALID_ENTRIES = (
# Fail when key is not found
( [ { " proto " : " eigrp " , " state " : " enabled " } ] , ' invalid_key ' , " Key invalid_key was not found " ) ,
( { " eigrp " : { " proto " : " eigrp " , " state " : " enabled " } } , ' invalid_key ' , " Key invalid_key was not found " ) ,
( AnsibleFilterError , [ { " proto " : " eigrp " , " state " : " enabled " } ] , ' invalid_key ' , " Key invalid_key was not found " ) ,
( AnsibleFilterError , { " eigrp " : { " proto " : " eigrp " , " state " : " enabled " } } , ' invalid_key ' , " Key invalid_key was not found " ) ,
# Fail when key is duplicated
( [ { " proto " : " eigrp " } , { " proto " : " ospf " } , { " proto " : " ospf " } ] ,
( AnsibleFilterError , [ { " proto " : " eigrp " } , { " proto " : " ospf " } , { " proto " : " ospf " } ] ,
' proto ' , ' Key ospf is not unique, cannot correctly turn into dict ' ) ,
# Fail when value is not a dict
( [ " string " ] , ' proto ' , " List item is not a valid dict " ) ,
( [ 123 ] , ' proto ' , " List item is not a valid dict " ) ,
( [ [ { ' proto ' : 1 } ] ] , ' proto ' , " List item is not a valid dict " ) ,
( AnsibleFilterTypeError , [ " string " ] , ' proto ' , " List item is not a valid dict " ) ,
( AnsibleFilterTypeError , [ 123 ] , ' proto ' , " List item is not a valid dict " ) ,
( AnsibleFilterTypeError , [ [ { ' proto ' : 1 } ] ] , ' proto ' , " List item is not a valid dict " ) ,
# Fail when we do not send a dict or list
( " string " , ' proto ' , " Type is not a valid list, set, or dict " ) ,
( 123 , ' proto ' , " Type is not a valid list, set, or dict " ) ,
( AnsibleFilterTypeError , " string " , ' proto ' , " Type is not a valid list, set, or dict " ) ,
( AnsibleFilterTypeError , 123 , ' proto ' , " Type is not a valid list, set, or dict " ) ,
)
@pytest.mark.parametrize ( " list_original, key, expected " , VALID_ENTRIES )
def test_rekey_on_member_success ( self , list_original , key , expected ) :
assert ms . rekey_on_member ( list_original , key ) == expected
@pytest.mark.parametrize ( " list_original, key, expected" , INVALID_ENTRIES )
def test_fail_rekey_on_member ( self , list_original, key , expected ) :
with pytest . raises ( AnsibleFilterError ) as err :
@pytest.mark.parametrize ( " expected_exception_type, list_original, key, expected" , INVALID_ENTRIES )
def test_fail_rekey_on_member ( self , expected_exception_type, list_original, key , expected ) :
with pytest . raises ( expected_exception_type ) as err :
ms . rekey_on_member ( list_original , key )
assert err . value . message == expected