-
Notifications
You must be signed in to change notification settings - Fork 0
Amna ejaz/semver python #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
partial review
@@ -32,6 +32,11 @@ class ConditionMatchTypes(object): | |||
GREATER_THAN = 'gt' | |||
LESS_THAN = 'lt' | |||
SUBSTRING = 'substring' | |||
SEMVER_EQ = 'semver_eq' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please alphabetize.
optimizely/helpers/condition.py
Outdated
|
||
return self.compare_user_version_with_target_version(index) <= 0 | ||
|
||
def semver_greater_than_and_equal_evaluator(self, index): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
semver_greater_than_or_equal_evaluator
optimizely/helpers/condition.py
Outdated
|
||
return self.compare_user_version_with_target_version(index) == -1 | ||
|
||
def semver_less_than_and_equal_evaluator(self, index): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or equal
EVALUATORS_BY_MATCH_TYPE = { | ||
ConditionMatchTypes.EXACT: exact_evaluator, | ||
ConditionMatchTypes.EXISTS: exists_evaluator, | ||
ConditionMatchTypes.GREATER_THAN: greater_than_evaluator, | ||
ConditionMatchTypes.LESS_THAN: less_than_evaluator, | ||
ConditionMatchTypes.SUBSTRING: substring_evaluator, | ||
ConditionMatchTypes.SEMVER_EQ: semver_equal_evaluator, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will appreciate please alphabetize.
optimizely/helpers/condition.py
Outdated
user_version_parts = user_version.split(".") | ||
|
||
for (idx, _) in enumerate(target_version_parts): | ||
if len(user_version_parts) <= idx: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take it variable outside if, so don't need to call len
again and again.
tests/base.py
Outdated
@@ -837,13 +840,46 @@ def setUp(self, config_dict='config_dict'): | |||
], | |||
], | |||
}, | |||
{ | |||
"id": "18278344267", | |||
"name": "semver_audience", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
give proper audience name.
tests/base.py
Outdated
{ | ||
"value": "1.2.3", | ||
"type": "custom_attribute", | ||
"name": "Android", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
give proper attribute name.
@@ -24,6 +24,17 @@ | |||
integerCondition = ['num_users', 10, 'custom_attribute', 'exact'] | |||
doubleCondition = ['pi_value', 3.14, 'custom_attribute', 'exact'] | |||
|
|||
eq_semver_condition_list_variation_1 = [['Android', "2.0", 'custom_attribute', 'semver_eq']] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give proper names, so anyone seeing testcase, should able to judge what this variable holds.
|
||
self.assertStrictTrue(evaluator.evaluate(0)) | ||
|
||
def test_evaluate__returns_true__when_user_version_does_not_match_target_version(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returns_true, interesting...
what's the difference between the following, both returning true.
test_evaluate__returns_true__when_user_version_does_not_match_target_version
test_evaluate__returns_true__when_user_version_matches_target_version
|
||
self.assertStrictFalse(evaluator.evaluate(0)) | ||
|
||
def test_evaluate__returns_true__when_user_version_is_gt_target_version(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename all unit test method names names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor feedback.
@@ -233,12 +238,74 @@ def substring_evaluator(self, index): | |||
|
|||
return condition_value in user_value | |||
|
|||
def semver_equal_evaluator(self, index): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add doc string for all the new methods. See other method docs for reference.
optimizely/helpers/condition.py
Outdated
return -1 | ||
# compare strings e.g: n1.n2.n3-alpha/beta | ||
if not user_version_parts[idx].isdigit(): | ||
if user_version_parts[idx] != target_version_parts[idx]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my understanding here is that if string contains alpha/beta, we look for exact match. If this is the case, we should also exit with 0 here if it matches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are just copying logic from swift, so no worries. w
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address
optimizely/helpers/condition.py
Outdated
@@ -19,6 +19,8 @@ | |||
from . import validator | |||
from .enums import CommonAudienceEvaluationLogs as audience_logs | |||
|
|||
class OptimizelyErrors(): | |||
AttributeFormatInvalid = "Provided attributes are in an invalid format." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We normally add these constants to enums.py. Use an existing or add a new message in there. https://github.com/AmnaEjaz/python-sdk/blob/master/optimizely/helpers/enums.py#L96
optimizely/helpers/condition.py
Outdated
return "-" in target | ||
|
||
def pre_release_separator(self): | ||
return "-" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not make this a constant instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to do that but i thought i am supposed to replicate the swift code as it is. Will make the suggested changes now
optimizely/helpers/condition.py
Outdated
return "+" in target | ||
|
||
def build_separator(self): | ||
return "+" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same. why not a constant?
optimizely/helpers/condition.py
Outdated
- True if the given version does contain "-" | ||
- False if it doesn't | ||
""" | ||
return "-" in target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use that constant here instead of hardcoding "-"
optimizely/helpers/condition.py
Outdated
target_suffix = "" | ||
|
||
if self.is_pre_release(target) or self.is_build(target): | ||
target_parts = target.split(self.pre_release_separator() if self.is_pre_release(target) else self.build_separator()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (self.is_pre_release(target):
split here
elif (self.is_build()):
split here
This will make the code more readable and you won't be calling is_pre_release twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will improve readability but at the cost of repetition. For instance:
if self.is_pre_release((target)):
target_parts = target.split(self.pre_release_separator())
if len(target_parts) < 1:
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
target_prefix = str(target_parts[0])
target_suffix = target_parts[1:]
elif self.is_build(target):
target_parts = target.split(self.build_separator())
if len(target_parts) < 1:
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
target_prefix = str(target_parts[0])
target_suffix = target_parts[1:]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can move out all logic by
if target_parts:
if len(target_parts) < 1:
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
target_prefix = str(target_parts[0])
target_suffix = target_parts[1:]
optimizely/helpers/condition.py
Outdated
target_version_parts.extend(target_suffix) | ||
return target_version_parts | ||
else: | ||
return target_version_parts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you are returning in both cases? Why not have a single return after if block ends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall Looks good. Please resolve the comments.
None: | ||
- if the user version value is not string type or is null. | ||
""" | ||
return self.compare_user_version_with_target_version(index) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.compare_user_version_with_target_version(index) == 1 | |
return self.compare_user_version_with_target_version(index) > 0 |
None: | ||
- if the user version value is not string type or is null. | ||
""" | ||
return self.compare_user_version_with_target_version(index) == -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.compare_user_version_with_target_version(index) == -1 | |
return self.compare_user_version_with_target_version(index) < 0 |
return self.compare_user_version_with_target_version(index) == 1 | ||
|
||
def semver_less_than_evaluator(self, index): | ||
""" Evaluate the given semver less than match target version for the user version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use "semantic version" instead of "semver" in all comments.
target_suffix = "" | ||
|
||
if self.is_pre_release(target) or self.is_build(target): | ||
target_parts = target.split(SemverType.IS_PRE_RELEASE if self.is_pre_release(target) else SemverType.IS_BUILD) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the semantic version = "2.1.2-beta.2+2.3" will it split preRelease and Build number seperately? I think it will only split on preRelease ("-") in the given case.
This will cause the problem when comparing the preRelease version as we ignore the build version during comparison.
@msohailhussain can you please ask Thomas if the above explanation I gave is correct. I can add the test case in FSC as well.
) | ||
|
||
self.assertStrictFalse(evaluator.evaluate(0)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove extra lines
elif user_version_part < target_version_part: | ||
return -1 | ||
if user_version_parts_len > target_version_parts_len: | ||
if self.is_patch_pre_release(user_version_parts_len-1, user_version_parts[user_version_parts_len-1]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not needed.
], | ||
'groups': [], | ||
'attributes': [ | ||
{'key': 'house', 'id': '594015'}, | ||
{'key': 'lasers', 'id': '594016'}, | ||
{'key': 'should_do_it', 'id': '594017'}, | ||
{'key': 'favorite_ice_cream', 'id': '594018'}, | ||
{'key': 'android-release', 'id': '594019'}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove this line
|
||
self.assertStrictTrue(evaluator.evaluate(0)) | ||
|
||
evaluator = condition_helper.CustomAttributeConditionEvaluator( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate each case into separate definition. In case of failure It would be helpful to identify the exact failed scenario.
|
||
self.assertStrictFalse(evaluator.evaluate(0)) | ||
|
||
evaluator = condition_helper.CustomAttributeConditionEvaluator( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above separate each individual case, with self explanatory name of def.
@@ -24,6 +24,20 @@ | |||
integerCondition = ['num_users', 10, 'custom_attribute', 'exact'] | |||
doubleCondition = ['pi_value', 3.14, 'custom_attribute', 'exact'] | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the header.
Summary
The “why”, or other context.
Test plan
Issues