| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright (c) 2021, Felix Fontein (@felixfontein) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
import sys |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from ansible_collections.community.routeros.plugins.module_utils._api_data import ( |
| 15 |
PATHS, |
| 16 |
VersionedAPIData, |
| 17 |
KeyInfo, |
| 18 |
split_path, |
| 19 |
join_path, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def test_paths_sorted(): |
| 24 |
if sys.version_info < (3, 6): |
| 25 |
# PATHS is randomly ordered |
| 26 |
return |
| 27 |
paths = list(PATHS) |
| 28 |
s_paths = sorted(paths) |
| 29 |
if paths != s_paths: |
| 30 |
for i, (a, b) in enumerate(zip(paths, s_paths)): |
| 31 |
if a != b: |
| 32 |
print(i, a, b) |
| 33 |
assert paths == s_paths |
| 34 |
|
| 35 |
|
| 36 |
def test_api_data_errors(): |
| 37 |
with pytest.raises(ValueError) as exc: |
| 38 |
VersionedAPIData() |
| 39 |
assert exc.value.args[0] == 'fields must be provided' |
| 40 |
|
| 41 |
values = [ |
| 42 |
('primary_keys', []), |
| 43 |
('stratify_keys', []), |
| 44 |
('has_identifier', True), |
| 45 |
('single_value', True), |
| 46 |
('unknown_mechanism', True), |
| 47 |
] |
| 48 |
|
| 49 |
for index, (param, param_value) in enumerate(values): |
| 50 |
for param2, param2_value in values[index + 1:]: |
| 51 |
with pytest.raises(ValueError) as exc: |
| 52 |
VersionedAPIData(**{param: param_value, param2: param2_value}) |
| 53 |
assert exc.value.args[0] == 'primary_keys, stratify_keys, has_identifier, single_value, and unknown_mechanism are mutually exclusive' |
| 54 |
|
| 55 |
with pytest.raises(ValueError) as exc: |
| 56 |
VersionedAPIData(unknown_mechanism=True, fully_understood=True) |
| 57 |
assert exc.value.args[0] == 'unknown_mechanism and fully_understood cannot be combined' |
| 58 |
|
| 59 |
with pytest.raises(ValueError) as exc: |
| 60 |
VersionedAPIData(unknown_mechanism=True, fixed_entries=True) |
| 61 |
assert exc.value.args[0] == 'fixed_entries can only be used with primary_keys' |
| 62 |
|
| 63 |
with pytest.raises(ValueError) as exc: |
| 64 |
VersionedAPIData(primary_keys=['foo'], fields={}) |
| 65 |
assert exc.value.args[0] == 'Primary key foo must be in fields!' |
| 66 |
|
| 67 |
with pytest.raises(ValueError) as exc: |
| 68 |
VersionedAPIData(stratify_keys=['foo'], fields={}) |
| 69 |
assert exc.value.args[0] == 'Stratify key foo must be in fields!' |
| 70 |
|
| 71 |
with pytest.raises(ValueError) as exc: |
| 72 |
VersionedAPIData(required_one_of=['foo'], fields={}) |
| 73 |
assert exc.value.args[0] == 'Require one of element at index #1 must be a list!' |
| 74 |
|
| 75 |
with pytest.raises(ValueError) as exc: |
| 76 |
VersionedAPIData(required_one_of=[['foo']], fields={}) |
| 77 |
assert exc.value.args[0] == 'Require one of key foo must be in fields!' |
| 78 |
|
| 79 |
with pytest.raises(ValueError) as exc: |
| 80 |
VersionedAPIData(mutually_exclusive=['foo'], fields={}) |
| 81 |
assert exc.value.args[0] == 'Mutually exclusive element at index #1 must be a list!' |
| 82 |
|
| 83 |
with pytest.raises(ValueError) as exc: |
| 84 |
VersionedAPIData(mutually_exclusive=[['foo']], fields={}) |
| 85 |
assert exc.value.args[0] == 'Mutually exclusive key foo must be in fields!' |
| 86 |
|
| 87 |
|
| 88 |
def test_key_info_errors(): |
| 89 |
values = [ |
| 90 |
('required', True), |
| 91 |
('default', ''), |
| 92 |
('automatically_computed_from', ()), |
| 93 |
('can_disable', True), |
| 94 |
] |
| 95 |
|
| 96 |
params_allowed_together = [ |
| 97 |
'default', |
| 98 |
'can_disable', |
| 99 |
] |
| 100 |
|
| 101 |
emsg = 'required, default, automatically_computed_from, and can_disable are mutually exclusive besides default and can_disable which can be set together' |
| 102 |
for index, (param, param_value) in enumerate(values): |
| 103 |
for param2, param2_value in values[index + 1:]: |
| 104 |
if param in params_allowed_together and param2 in params_allowed_together: |
| 105 |
continue |
| 106 |
with pytest.raises(ValueError) as exc: |
| 107 |
KeyInfo(**{param: param_value, param2: param2_value}) |
| 108 |
assert exc.value.args[0] == emsg |
| 109 |
|
| 110 |
with pytest.raises(ValueError) as exc: |
| 111 |
KeyInfo('foo') |
| 112 |
assert exc.value.args[0] == 'KeyInfo() does not have positional arguments' |
| 113 |
|
| 114 |
with pytest.raises(ValueError) as exc: |
| 115 |
KeyInfo(remove_value='') |
| 116 |
assert exc.value.args[0] == 'remove_value can only be specified if can_disable=True' |
| 117 |
|
| 118 |
with pytest.raises(ValueError) as exc: |
| 119 |
KeyInfo(read_only=True, write_only=True) |
| 120 |
assert exc.value.args[0] == 'read_only and write_only cannot be used at the same time' |
| 121 |
|
| 122 |
with pytest.raises(ValueError) as exc: |
| 123 |
KeyInfo(read_only=True, default=0) |
| 124 |
assert exc.value.args[0] == 'read_only can not be combined with can_disable, remove_value, absent_value, default, or required' |
| 125 |
|
| 126 |
|
| 127 |
SPLIT_PATHS = [ |
| 128 |
('', [], ''), |
| 129 |
(' ip ', ['ip'], 'ip'), |
| 130 |
('ip', ['ip'], 'ip'), |
| 131 |
(' ip \t\n\raddress ', ['ip', 'address'], 'ip address'), |
| 132 |
] |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize("joined_input, split, joined_output", SPLIT_PATHS) |
| 136 |
def test_join_split_path(joined_input, split, joined_output): |
| 137 |
assert split_path(joined_input) == split |
| 138 |
assert join_path(split) == joined_output |