Skip to content

Commit 16f768a

Browse files
committed
Add tests for existing filters
Signed-off-by: Webster Mudge <[email protected]>
1 parent 5fde0fb commit 16f768a

File tree

8 files changed

+188
-112
lines changed

8 files changed

+188
-112
lines changed

plugins/module_utils/cldr_version.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@
4141
)?
4242
$
4343
""",
44-
flags=re.X
44+
flags=re.X,
4545
)
4646

47+
4748
class ClouderaVersion(Version):
4849
"""Version comparison class that implements Cloudera versioning.
4950
5051
Cloudera versioning is an extension of Semantic Versioning that uses the
5152
``prerelease`` segment as service packs or patch releases.
52-
53+
5354
Moreover, this versioning scheme allow the ``prerelease`` delimiter
54-
to use whitespace (' ') and dots ('.') in addition to the semantic
55+
to use whitespace (' ') and dots ('.') in addition to the semantic
5556
version's dash ('-').
5657
5758
Based off of ``distutils.version.Version`` and ``ansible.utils.version``.
@@ -71,7 +72,7 @@ def __init__(self, vstring=None):
7172
self.parse(vstring)
7273

7374
def __repr__(self):
74-
return 'ClouderaVersion(%r)' % self.vstring
75+
return "ClouderaVersion(%r)" % self.vstring
7576

7677
@staticmethod
7778
def from_loose_version(loose_version):
@@ -90,7 +91,7 @@ def from_loose_version(loose_version):
9091
raise ValueError("%r is not a LooseVersion" % loose_version)
9192

9293
extra_idx = 3
93-
for marker in ('-', '+'):
94+
for marker in ("-", "+"):
9495
try:
9596
idx = version.index(marker)
9697
except ValueError:
@@ -104,14 +105,15 @@ def from_loose_version(loose_version):
104105
raise ValueError("Non integer values in %r" % loose_version)
105106

106107
# Extra is everything to the right of the core version
107-
extra = re.search('[+-].+$', loose_version.vstring)
108+
extra = re.search("[+-].+$", loose_version.vstring)
108109

109110
version = version + [0] * (3 - len(version))
110111
return ClouderaVersion(
111-
'%s%s' % (
112-
'.'.join(str(v) for v in version),
113-
extra.group(0) if extra else ''
114-
)
112+
"%s%s"
113+
% (
114+
".".join(str(v) for v in version),
115+
extra.group(0) if extra else "",
116+
),
115117
)
116118

117119
def parse(self, vstring) -> None:
@@ -129,9 +131,14 @@ def parse(self, vstring) -> None:
129131
self.buildmetadata = None
130132

131133
if prerelease:
132-
self.prerelease = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in prerelease.split('.'))
134+
self.prerelease = tuple(
135+
_Numeric(x) if x.isdigit() else _Alpha(x) for x in prerelease.split(".")
136+
)
133137
if buildmetadata:
134-
self.buildmetadata = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in buildmetadata.split('.'))
138+
self.buildmetadata = tuple(
139+
_Numeric(x) if x.isdigit() else _Alpha(x)
140+
for x in buildmetadata.split(".")
141+
)
135142

136143
@property
137144
def core(self) -> tuple[int | None, int | None, int | None]:

plugins/test/cldr.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,53 @@
1919
from ansible import errors
2020
from ansible.module_utils.common.text.converters import to_native, to_text
2121

22-
from ansible_collections.cloudera.exe.plugins.module_utils.cldr_version import ClouderaVersion
22+
from ansible_collections.cloudera.exe.plugins.module_utils.cldr_version import (
23+
ClouderaVersion,
24+
)
2325

2426

2527
def cldr_version_compare(value, version, operator="eq"):
26-
""" Perform a Cloudera version comparison on a value """
28+
"""Perform a Cloudera version comparison on a value"""
2729
op_map = {
28-
'==': 'eq', '=': 'eq', 'eq': 'eq',
29-
'<': 'lt', 'lt': 'lt',
30-
'<=': 'le', 'le': 'le',
31-
'>': 'gt', 'gt': 'gt',
32-
'>=': 'ge', 'ge': 'ge',
33-
'!=': 'ne', '<>': 'ne', 'ne': 'ne'
30+
"==": "eq",
31+
"=": "eq",
32+
"eq": "eq",
33+
"<": "lt",
34+
"lt": "lt",
35+
"<=": "le",
36+
"le": "le",
37+
">": "gt",
38+
"gt": "gt",
39+
">=": "ge",
40+
"ge": "ge",
41+
"!=": "ne",
42+
"<>": "ne",
43+
"ne": "ne",
3444
}
3545

3646
if operator in op_map:
3747
operator = op_map[operator]
3848
else:
3949
raise errors.AnsibleFilterError(
40-
'Invalid operator type (%s). Must be one of %s' % (operator, ', '.join(map(repr, op_map)))
50+
"Invalid operator type (%s). Must be one of %s"
51+
% (operator, ", ".join(map(repr, op_map))),
4152
)
42-
53+
4354
try:
4455
method = getattr(py_operator, operator)
45-
return method(ClouderaVersion(to_text(value)), ClouderaVersion(to_text(version)))
56+
return method(
57+
ClouderaVersion(to_text(value)),
58+
ClouderaVersion(to_text(version)),
59+
)
4660
except Exception as e:
47-
raise errors.AnsibleFilterError('Version comparison failed: %s' % to_native(e))
61+
raise errors.AnsibleFilterError("Version comparison failed: %s" % to_native(e))
4862

4963

5064
class TestModule(object):
51-
""" Cloudera jinja2 tests """
65+
"""Cloudera jinja2 tests"""
5266

5367
def tests(self):
5468
return {
5569
# failure testing
56-
'version': cldr_version_compare,
70+
"version": cldr_version_compare,
5771
}
58-

tests/unit/plugins/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,28 @@
2121
import logging
2222
import pytest
2323

24-
from ansible_collections.cloudera.exe.plugins.filter.core_exe import FilterModule as filters_exe
24+
from ansible_collections.cloudera.exe.plugins.filter.core_exe import (
25+
FilterModule as filters_exe,
26+
)
2527
from ansible_collections.cloudera.exe.plugins.test.cldr import TestModule as tests_exe
2628

2729

2830
LOG = logging.getLogger(__name__)
2931

3032
# Pytest fixtures
3133

34+
3235
@pytest.fixture(scope="module")
3336
def filter():
3437
def get_filter(filter_short_name: str):
3538
return filters_exe().filters().get(filter_short_name)
39+
3640
return get_filter
3741

42+
3843
@pytest.fixture(scope="module")
3944
def test():
4045
def get_test(test_short_name: str):
4146
return tests_exe().tests().get(test_short_name)
47+
4248
return get_test

tests/unit/plugins/filter/test_core_exe.py

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2025 Cloudera, Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
21+
import pytest
22+
23+
from unittest import TestCase
24+
25+
26+
dataset = [
27+
(
28+
{"foo": "bar", "gaz": "blaz", "nested": {"duz": "ferr"}},
29+
{"gaz": "blergh", "derr": "zaar", "nested": {"wuz": "gug"}},
30+
False,
31+
{
32+
"foo": "bar",
33+
"gaz": "blaz",
34+
"derr": "zaar",
35+
"nested": {"duz": "ferr"},
36+
},
37+
),
38+
(
39+
{"foo": "bar", "gaz": "blaz", "nested": {"duz": "ferr"}},
40+
{"gaz": "blergh", "derr": "zaar", "nested": {"wuz": "gug"}},
41+
True,
42+
{
43+
"foo": "bar",
44+
"gaz": "blaz",
45+
"derr": "zaar",
46+
"nested": {"duz": "ferr", "wuz": "gug"},
47+
},
48+
),
49+
]
50+
51+
52+
@pytest.mark.parametrize("source,target,recursive,expected", dataset)
53+
def test_filter_version(filter, source, target, recursive, expected):
54+
onto_filter = filter("combine_onto")
55+
56+
actual = onto_filter([source, target], recursive=recursive)
57+
58+
TestCase().assertDictEqual(expected, actual)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2025 Cloudera, Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
21+
import pytest
22+
23+
24+
dataset = [
25+
("1.2.3", (1, 2, 3, None, None)),
26+
("1.2.3 SP1", (1, 2, 3, tuple(["SP1"]), None)),
27+
("1.2.3-SP1", (1, 2, 3, tuple(["SP1"]), None)),
28+
("1.2.3.SP1", (1, 2, 3, tuple(["SP1"]), None)),
29+
("1.2.3 SP1.400", (1, 2, 3, tuple(["SP1", 400]), None)),
30+
("1.2.3+Build", (1, 2, 3, None, tuple(["Build"]))),
31+
("1.2.3+Build.400", (1, 2, 3, None, tuple(["Build", 400]))),
32+
]
33+
34+
35+
@pytest.mark.parametrize("vstring,expected", dataset)
36+
def test_filter_version(filter, vstring, expected):
37+
version_filter = filter("version")
38+
39+
actual = version_filter(vstring)
40+
41+
assert actual.get("major") == expected[0]
42+
assert actual.get("minor") == expected[1]
43+
assert actual.get("patch") == expected[2]
44+
assert actual.get("prerelease") == expected[3]
45+
assert actual.get("buildmetadata") == expected[4]

0 commit comments

Comments
 (0)