Skip to content

Commit

Permalink
compare implemetation and invalid testcase fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
uzair-folio3 committed Aug 26, 2020
1 parent 2affb9d commit 8577f7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 13 additions & 5 deletions optimizely/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def compare_user_version_with_target_version(self, index):

target_version_parts = self.split_semantic_version(target_version)
user_version_parts = self.split_semantic_version(user_version)
if not user_version_parts:
return None

user_version_parts_len = len(user_version_parts)

for (idx, _) in enumerate(target_version_parts):
Expand Down Expand Up @@ -403,7 +406,8 @@ def semver_equal_evaluator(self, index):
None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) == 0
return self.compare_user_version_with_target_version(
index) == 0 if self.compare_user_version_with_target_version(index) else None

def semver_greater_than_evaluator(self, index):
""" Evaluate the given semantic version greater than match target version for the user version.
Expand All @@ -418,7 +422,8 @@ def semver_greater_than_evaluator(self, index):
None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) > 0
return self.compare_user_version_with_target_version(
index) > 0 if self.compare_user_version_with_target_version(index) else None

def semver_less_than_evaluator(self, index):
""" Evaluate the given semantic version less than match target version for the user version.
Expand All @@ -433,7 +438,8 @@ def semver_less_than_evaluator(self, index):
None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) < 0
return self.compare_user_version_with_target_version(
index) < 0 if self.compare_user_version_with_target_version(index) else None

def semver_less_than_or_equal_evaluator(self, index):
""" Evaluate the given semantic version less than or equal to match target version for the user version.
Expand All @@ -448,7 +454,8 @@ def semver_less_than_or_equal_evaluator(self, index):
None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) <= 0
return self.compare_user_version_with_target_version(
index) <= 0 if self.compare_user_version_with_target_version(index) else None

def semver_greater_than_or_equal_evaluator(self, index):
""" Evaluate the given semantic version greater than or equal to match target version for the user version.
Expand All @@ -463,7 +470,8 @@ def semver_greater_than_or_equal_evaluator(self, index):
None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) >= 0
return self.compare_user_version_with_target_version(
index) >= 0 if self.compare_user_version_with_target_version(index) else None

EVALUATORS_BY_MATCH_TYPE = {
ConditionMatchTypes.EXACT: exact_evaluator,
Expand Down
16 changes: 9 additions & 7 deletions tests/helpers_tests/test_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2019,11 +2019,13 @@ def test_substring__condition_value_invalid(self):
).format(json.dumps(expected_condition_log))
)

@pytest.mark.parametrize("test_input,expected", [(i, None) for i in ["-", ".", "..", "+", "+test", " ", "2 .0. 0",
"2.", ".0.0", "1.2.2.2", "2.x", ",",
"+build-prerelese"]])
def test_invalid_semver__returns_null__when_semver_is_invalid(self, test_input, expected):
evaluator = condition_helper.CustomAttributeConditionEvaluator(
semver_less_than_or_equal_2_0_1_condition_list, {'Android': test_input}, self.mock_client_logger)
def test_invalid_semver__returns_None__when_semver_is_invalid(self):
invalid_test_cases = ["-", ".", "..", "+", "+test", " ", "2 .0. 0",
"2.", ".0.0", "1.2.2.2", "2.x", ",",
"+build-prerelese"]

for invalid_test_case in invalid_test_cases:
evaluator = condition_helper.CustomAttributeConditionEvaluator(
semver_less_than_or_equal_2_0_1_condition_list, {'Android': invalid_test_case}, self.mock_client_logger)

assert eval(evaluator.evaluate(0)) == expected
self.assertIsNone(evaluator.evaluate(0))

0 comments on commit 8577f7c

Please sign in to comment.