| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import typing as t |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from ansible_collections.community.general.plugins.module_utils import _csv as csv |
| 12 |
|
| 13 |
VALID_CSV = [ |
| 14 |
( |
| 15 |
"excel", |
| 16 |
{}, |
| 17 |
None, |
| 18 |
"id,name,role\n1,foo,bar\n2,bar,baz", |
| 19 |
[ |
| 20 |
{ |
| 21 |
"id": "1", |
| 22 |
"name": "foo", |
| 23 |
"role": "bar", |
| 24 |
}, |
| 25 |
{ |
| 26 |
"id": "2", |
| 27 |
"name": "bar", |
| 28 |
"role": "baz", |
| 29 |
}, |
| 30 |
], |
| 31 |
), |
| 32 |
( |
| 33 |
"excel", |
| 34 |
{"skipinitialspace": True}, |
| 35 |
None, |
| 36 |
"id,name,role\n1, foo, bar\n2, bar, baz", |
| 37 |
[ |
| 38 |
{ |
| 39 |
"id": "1", |
| 40 |
"name": "foo", |
| 41 |
"role": "bar", |
| 42 |
}, |
| 43 |
{ |
| 44 |
"id": "2", |
| 45 |
"name": "bar", |
| 46 |
"role": "baz", |
| 47 |
}, |
| 48 |
], |
| 49 |
), |
| 50 |
( |
| 51 |
"excel", |
| 52 |
{"delimiter": "|"}, |
| 53 |
None, |
| 54 |
"id|name|role\n1|foo|bar\n2|bar|baz", |
| 55 |
[ |
| 56 |
{ |
| 57 |
"id": "1", |
| 58 |
"name": "foo", |
| 59 |
"role": "bar", |
| 60 |
}, |
| 61 |
{ |
| 62 |
"id": "2", |
| 63 |
"name": "bar", |
| 64 |
"role": "baz", |
| 65 |
}, |
| 66 |
], |
| 67 |
), |
| 68 |
( |
| 69 |
"unix", |
| 70 |
{}, |
| 71 |
None, |
| 72 |
"id,name,role\n1,foo,bar\n2,bar,baz", |
| 73 |
[ |
| 74 |
{ |
| 75 |
"id": "1", |
| 76 |
"name": "foo", |
| 77 |
"role": "bar", |
| 78 |
}, |
| 79 |
{ |
| 80 |
"id": "2", |
| 81 |
"name": "bar", |
| 82 |
"role": "baz", |
| 83 |
}, |
| 84 |
], |
| 85 |
), |
| 86 |
( |
| 87 |
"excel", |
| 88 |
{}, |
| 89 |
["id", "name", "role"], |
| 90 |
"1,foo,bar\n2,bar,baz", |
| 91 |
[ |
| 92 |
{ |
| 93 |
"id": "1", |
| 94 |
"name": "foo", |
| 95 |
"role": "bar", |
| 96 |
}, |
| 97 |
{ |
| 98 |
"id": "2", |
| 99 |
"name": "bar", |
| 100 |
"role": "baz", |
| 101 |
}, |
| 102 |
], |
| 103 |
), |
| 104 |
] |
| 105 |
|
| 106 |
INVALID_CSV = [ |
| 107 |
( |
| 108 |
"excel", |
| 109 |
{"strict": True}, |
| 110 |
None, |
| 111 |
'id,name,role\n1,"f"oo",bar\n2,bar,baz', |
| 112 |
), |
| 113 |
] |
| 114 |
|
| 115 |
INVALID_DIALECT: list[tuple[str, t.Any, t.Any, str]] = [ |
| 116 |
( |
| 117 |
"invalid", |
| 118 |
{}, |
| 119 |
None, |
| 120 |
"id,name,role\n1,foo,bar\n2,bar,baz", |
| 121 |
), |
| 122 |
] |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("dialect,dialect_params,fieldnames,data,expected", VALID_CSV) |
| 126 |
def test_valid_csv(data, dialect, dialect_params, fieldnames, expected): |
| 127 |
dialect = csv.initialize_dialect(dialect, **dialect_params) |
| 128 |
reader = csv.read_csv(data, dialect, fieldnames) |
| 129 |
result = True |
| 130 |
|
| 131 |
for idx, row in enumerate(reader): |
| 132 |
for k, v in row.items(): |
| 133 |
if expected[idx][k] != v: |
| 134 |
result = False |
| 135 |
break |
| 136 |
|
| 137 |
assert result |
| 138 |
|
| 139 |
|
| 140 |
@pytest.mark.parametrize("dialect,dialect_params,fieldnames,data", INVALID_CSV) |
| 141 |
def test_invalid_csv(data, dialect, dialect_params, fieldnames): |
| 142 |
dialect = csv.initialize_dialect(dialect, **dialect_params) |
| 143 |
reader = csv.read_csv(data, dialect, fieldnames) |
| 144 |
result = False |
| 145 |
|
| 146 |
try: |
| 147 |
for _row in reader: |
| 148 |
continue |
| 149 |
except csv.CSVError: |
| 150 |
result = True |
| 151 |
|
| 152 |
assert result |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.parametrize("dialect,dialect_params,fieldnames,data", INVALID_DIALECT) |
| 156 |
def test_invalid_dialect(data: str, dialect: t.Any, dialect_params: t.Any, fieldnames: str) -> None: |
| 157 |
result = False |
| 158 |
|
| 159 |
try: |
| 160 |
dialect = csv.initialize_dialect(dialect, **dialect_params) |
| 161 |
except csv.DialectNotAvailableError: |
| 162 |
result = True |
| 163 |
|
| 164 |
assert result |