2022-05-24 18:23:44 +02:00
# -*- coding: utf-8 -*-
2022-07-18 23:16:54 +02:00
# Copyright (c) 2021, Felix Fontein (@felixfontein) <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
2022-05-24 18:23:44 +02:00
from __future__ import absolute_import , division , print_function
__metaclass__ = type
import pytest
from ansible_collections . community . routeros . plugins . module_utils . _api_data import (
2023-09-01 22:27:18 +02:00
VersionedAPIData ,
2022-05-24 18:23:44 +02:00
KeyInfo ,
split_path ,
join_path ,
)
def test_api_data_errors ( ) :
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' fields must be provided '
values = [
( ' primary_keys ' , [ ] ) ,
( ' stratify_keys ' , [ ] ) ,
( ' has_identifier ' , True ) ,
( ' single_value ' , True ) ,
( ' unknown_mechanism ' , True ) ,
]
for index , ( param , param_value ) in enumerate ( values ) :
for param2 , param2_value in values [ index + 1 : ] :
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( * * { param : param_value , param2 : param2_value } )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' primary_keys, stratify_keys, has_identifier, single_value, and unknown_mechanism are mutually exclusive '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( unknown_mechanism = True , fully_understood = True )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' unknown_mechanism and fully_understood cannot be combined '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( unknown_mechanism = True , fixed_entries = True )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' fixed_entries can only be used with primary_keys '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( primary_keys = [ ' foo ' ] , fields = { } )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' Primary key foo must be in fields! '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( stratify_keys = [ ' foo ' ] , fields = { } )
2022-05-24 18:23:44 +02:00
assert exc . value . args [ 0 ] == ' Stratify key foo must be in fields! '
2022-12-29 08:51:40 +01:00
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( required_one_of = [ ' foo ' ] , fields = { } )
2022-12-29 08:51:40 +01:00
assert exc . value . args [ 0 ] == ' Require one of element at index #1 must be a list! '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( required_one_of = [ [ ' foo ' ] ] , fields = { } )
2022-12-29 08:51:40 +01:00
assert exc . value . args [ 0 ] == ' Require one of key foo must be in fields! '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( mutually_exclusive = [ ' foo ' ] , fields = { } )
2022-12-29 08:51:40 +01:00
assert exc . value . args [ 0 ] == ' Mutually exclusive element at index #1 must be a list! '
with pytest . raises ( ValueError ) as exc :
2023-09-01 22:27:18 +02:00
VersionedAPIData ( mutually_exclusive = [ [ ' foo ' ] ] , fields = { } )
2022-12-29 08:51:40 +01:00
assert exc . value . args [ 0 ] == ' Mutually exclusive key foo must be in fields! '
2022-05-24 18:23:44 +02:00
def test_key_info_errors ( ) :
values = [
( ' required ' , True ) ,
( ' default ' , ' ' ) ,
( ' automatically_computed_from ' , ( ) ) ,
( ' can_disable ' , True ) ,
]
2022-11-17 12:48:50 +01:00
params_allowed_together = [
' default ' ,
' can_disable ' ,
]
emsg = ' required, default, automatically_computed_from, and can_disable are mutually exclusive besides default and can_disable which can be set together '
2022-05-24 18:23:44 +02:00
for index , ( param , param_value ) in enumerate ( values ) :
for param2 , param2_value in values [ index + 1 : ] :
2022-11-17 12:48:50 +01:00
if param in params_allowed_together and param2 in params_allowed_together :
continue
2022-05-24 18:23:44 +02:00
with pytest . raises ( ValueError ) as exc :
KeyInfo ( * * { param : param_value , param2 : param2_value } )
2022-11-17 12:48:50 +01:00
assert exc . value . args [ 0 ] == emsg
2022-05-24 18:23:44 +02:00
with pytest . raises ( ValueError ) as exc :
KeyInfo ( ' foo ' )
assert exc . value . args [ 0 ] == ' KeyInfo() does not have positional arguments '
with pytest . raises ( ValueError ) as exc :
KeyInfo ( remove_value = ' ' )
assert exc . value . args [ 0 ] == ' remove_value can only be specified if can_disable=True '
SPLITTED_PATHS = [
( ' ' , [ ] , ' ' ) ,
( ' ip ' , [ ' ip ' ] , ' ip ' ) ,
( ' ip ' , [ ' ip ' ] , ' ip ' ) ,
( ' ip \t \n \r address ' , [ ' ip ' , ' address ' ] , ' ip address ' ) ,
]
@pytest.mark.parametrize ( " joined_input, splitted, joined_output " , SPLITTED_PATHS )
def test_join_split_path ( joined_input , splitted , joined_output ) :
assert split_path ( joined_input ) == splitted
assert join_path ( splitted ) == joined_output