Skip to content

Commit

Permalink
Flip unit tests to pytest.mark.parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Dec 28, 2024
1 parent fa4dd7d commit d88b677
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 102 deletions.
4 changes: 3 additions & 1 deletion plugins/modules/s3_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ def compare_and_update_configuration(client, module, current_lifecycle_rules, ru
if current_lifecycle_rules:
# If rule ID exists, use that for comparison otherwise compare based on prefix
for existing_rule in current_lifecycle_rules:
if rule.get("ID") == existing_rule.get("ID") and not filters_are_equal(rule.get("Filter"), existing_rule.get("Filter")):
if rule.get("ID") == existing_rule.get("ID") and not filters_are_equal(
rule.get("Filter"), existing_rule.get("Filter")
):
existing_rule.pop("ID")
elif rule_id is None and filters_are_equal(rule.get("Filter"), existing_rule.get("Filter")):
existing_rule.pop("ID")
Expand Down
210 changes: 109 additions & 101 deletions tests/unit/plugins/modules/s3_lifecycle/test_filters_are_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,117 @@
# Copyright: (c) 2023, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from ansible_collections.community.aws.plugins.modules.s3_lifecycle import filters_are_equal

# @pytest.mark.parametrize(
# "filter1,filter2,result",
# []
# )
# def test_filters_are_equal(filter1, filter2, result):
def test_filters_are_equal():
# Test case 1: Both filters are identical
filter1 = {
"And": {
"Prefix": "nested/",
"ObjectSizeGreaterThan": 150,
"ObjectSizeLessThan": 180,
},
}
filter2 = filter1.copy()
assert filters_are_equal(filter1, filter2) is True

# Test case 2: One filter is None
filter1 = None
filter2 = {
"Prefix": "test/"
}
assert filters_are_equal(filter1, filter2) is False

# Test case 3: One filter is empty
filter1 = {}
filter2 = {
"ObjectSizeGreaterThan": 100,
}
assert filters_are_equal(filter1, filter2) is False
import pytest

# Test case 4: Filters differ in a single key
filter1 = {
"ObjectSizeGreaterThan": 100,
}
filter2 = {
"ObjectSizeGreaterThan": 200, # Different value
}
assert filters_are_equal(filter1, filter2) is False

# Test case 5: Filters with missing `And` key in one filter
filter1 = {
"Prefix": "test/",
}
filter2 = {
"And": {
"Prefix": "nested/",
"ObjectSizeGreaterThan": 100,
},
}
assert filters_are_equal(filter1, filter2) is False

# Test case 6: Filters with nested `And` keys matching
filter1 = {
"And": {
"Prefix": "nested/",
"ObjectSizeGreaterThan": 150,
},
}
filter2 = {
"And": {
"Prefix": "nested/",
"ObjectSizeGreaterThan": 150,
},
}
assert filters_are_equal(filter1, filter2) is True
from ansible_collections.community.aws.plugins.modules.s3_lifecycle import filters_are_equal

# Test case 7: Filters with nested `And` keys differing
filter1 = {
"And": {
"Prefix": "test/",
"ObjectSizeGreaterThan": 150,
},
}
filter2 = {
"And": {
"Prefix": "nested/", # Different key/value
"ObjectSizeLessThan": 150,
},
}
assert filters_are_equal(filter1, filter2) is False

# Test case 8: Both filters are None
filter1 = None
filter2 = None
assert filters_are_equal(filter1, filter2) is False
@pytest.mark.parametrize(
"filter1,filter2,result",
[
[None, None, True],
[{}, {}, True],
# Simple filters equal
[{"Prefix": ""}, {"Prefix": ""}, True],
[{"Prefix": "prefix/"}, {"Prefix": "prefix/"}, True],
[{"ObjectSizeGreaterThan": 100}, {"ObjectSizeGreaterThan": 100}, True],
[{"ObjectSizeLessThan": 100}, {"ObjectSizeLessThan": 100}, True],
# One filter is empty
[{"Prefix": ""}, {}, False],
[{"ObjectSizeGreaterThan": 100}, {}, False],
[{"ObjectSizeLessThan": 100}, {}, False],
# One filter is None
[{"Prefix": ""}, None, False],
[{"ObjectSizeGreaterThan": 100}, None, False],
[{"ObjectSizeLessThan": 100}, None, False],
# Filters differ in a single key
[{"Prefix": "prefix/"}, {"Prefix": "prefix2/"}, False],
[{"ObjectSizeGreaterThan": 100}, {"ObjectSizeGreaterThan": 200}, False],
[{"ObjectSizeLessThan": 100}, {"ObjectSizeLessThan": 200}, False],
# While in theory, these would be equal. We currently don't treat them as such and
# a single key in the "And" dict is technically not valid.
[{"Prefix": "prefix/"}, {"And": {"Prefix": "prefix/"}}, False],
[{"ObjectSizeGreaterThan": 100}, {"And": {"ObjectSizeGreaterThan": 100}}, False],
[{"ObjectSizeLessThan": 100}, {"And": {"ObjectSizeLessThan": 100}}, False],
],
)
def test_filters_are_equal_simple(filter1, filter2, result):
assert filters_are_equal(filter1, filter2) is result
assert filters_are_equal(filter2, filter1) is result

# Test case 9: Filters with different `Prefix` values
filter1 = {
"Prefix": "test/"
}
filter2 = {
"Prefix": "different/"
}
assert filters_are_equal(filter1, filter2) is False

# Test case 10: Filters with empty strings for `Prefix`
filter1 = {
"Prefix": ""
}
filter2 = {
"Prefix": ""
}
assert filters_are_equal(filter1, filter2) is True
# Could be merged with the ones above, but naming will give a better idea of what's wrong
@pytest.mark.parametrize(
"filter1,filter2,result",
[
# Equal
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
True,
],
# Special case of "Prefix" missing == Prefix of ""
[
{"And": {"Prefix": "", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
True,
],
# Equal but with 2 of 3 "And" keys
[
{"And": {"ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
True,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeLessThan": 180}},
True,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150}},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150}},
True,
],
# One key missing
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
False,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeLessThan": 180}},
False,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150}},
False,
],
# One key different
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "another/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
False,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 42, "ObjectSizeLessThan": 180}},
False,
],
[
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 180}},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 90}},
False,
],
# Mixed with a non-and
[
{"Prefix": "test/"},
{"And": {"Prefix": "nested/", "ObjectSizeGreaterThan": 150, "ObjectSizeLessThan": 90}},
False,
],
],
)
def test_filters_are_equal_and(filter1, filter2, result):
assert filters_are_equal(filter1, filter2) is result
assert filters_are_equal(filter2, filter1) is result

0 comments on commit d88b677

Please sign in to comment.