Skip to content
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

Fix/client spec constraint tests #116

Merged
merged 34 commits into from
Sep 13, 2022
Merged

Conversation

gardleopard
Copy link
Collaborator

This makes us ignore the constraint if the constraint operator is
unknown.
If input data to a constraint is missing, then its always not present in
a set. Nothing is not a part of anything.
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me but give me a bit of time to double check the client spec tests, this got crazy and I want to make sure we aren't crazy too

@sighphyre
Copy link
Member

Okay, never mind, looked over the client spec, this does look correct and the failing client spec test cases are because... well... nothing can't be in anything - i.e. this is isolated to the oddities of the IN/NOT_IN operators

The remaining failures are due to global constraints not being implemented and should be closed by #114

@coveralls
Copy link

Pull Request Test Coverage Report for Build 3020655652

  • -1 of 22 (95.45%) changed or added relevant lines in 2 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 91.102%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/unleash/constraint.rb 11 12 91.67%
Files with Coverage Reduction New Missed Lines %
lib/unleash/constraint.rb 3 80.0%
Totals Coverage Status
Change from base Build 3014380490: 0.01%
Covered Lines: 2017
Relevant Lines: 2214

💛 - Coveralls

@coveralls
Copy link

coveralls commented Sep 9, 2022

Pull Request Test Coverage Report for Build 3036905670

  • 36 of 36 (100.0%) changed or added relevant lines in 4 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-91.09%) to 0.0%

Files with Coverage Reduction New Missed Lines %
lib/unleash/constraint.rb 5 78.95%
Totals Coverage Status
Change from base Build 3014380490: -91.09%
Covered Lines: 0
Relevant Lines: 2092

💛 - Coveralls

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looks good but can we make Rubocop happy

Copy link
Collaborator

@rarruda rarruda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some suggestions.

but as @sighphyre commented, rubocop doesn't seem quite happy yet.

lib/unleash/constraint.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
gardleopard and others added 3 commits September 9, 2022 15:20
Co-authored-by: Renato Arruda <rarruda@rarruda.org>
Co-authored-by: Renato Arruda <rarruda@rarruda.org>
Copy link
Collaborator

@rarruda rarruda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a corner case that needs to be looked at, plus some other general comments.

Comment on lines 53 to 54
# when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match.
return true if operator == :NOT_IN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about it again, this seems to belong in matches_constraint? Method and not in the matches_context?

So for this operator the matches_constraint? Should not throw an exception. (But still should for all other operators).

Also note that this doesn't seem to respect the inverted method parameter. Noting it up the stack should solve that. But maybe add a trivial assert for NOT using the inverted parameter (in addition to the current one using)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commit should solve all of this. The reversing happens in matches_context and in matches_constraint we are now handling the nil values.

spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
lib/unleash/constraint.rb Outdated Show resolved Hide resolved
Copy link
Collaborator

@rarruda rarruda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more feedback. It is looking promising!

lib/unleash/context.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Outdated Show resolved Hide resolved
spec/unleash/constraint_spec.rb Show resolved Hide resolved
lib/unleash/constraint.rb Show resolved Hide resolved
Comment on lines 97 to 101
# when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match.
return self.operator == :NOT_IN unless context.property?(self.context_name)

Unleash.logger.debug "Unleash::Constraint matches_context? value: #{self.value} context.get_by_name(#{self.context_name})" \
" #{context.get_by_name(self.context_name)} "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should switch order here, and rename the method name within the log message / rewrite it to the log messages current context.

Suggested change
# when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match.
return self.operator == :NOT_IN unless context.property?(self.context_name)
Unleash.logger.debug "Unleash::Constraint matches_context? value: #{self.value} context.get_by_name(#{self.context_name})" \
" #{context.get_by_name(self.context_name)} "
Unleash.logger.debug "Unleash::Constraint matches_constraint? value: #{self.value} constraint: #{self.operator} context.get_by_name(#{self.context_name})" \
" #{context.get_by_name(self.context_name)} "
# when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match.
return self.operator == :NOT_IN unless context.property?(self.context_name)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cant log it before we validate that it is set. This is the reason the log is behind the check if property is set.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not? context.get_by_name() should return nil if no value is found.

We would like to get this debug message for all operators, including the :NOT_IN, and even if the value is not in the context.

Also the node regarding having matches_constraint? instead of matches_context? within the log message is still valid.

Unleash::Constraint.new('env', operator_name, '')
end.to raise_error
expect(Unleash.logger).to receive(:warn).with("value is a String, operator is expecting an Array")
Unleash::Constraint.new('env', operator_name, '')
end

string_constraints = ['NUM_EQ', 'NUM_GT', 'NUM_GTE', 'NUM_LT', 'NUM_LTE',
'DATE_AFTER', 'DATE_BEFORE', 'SEMVER_EQ', 'SEMVER_GT', 'SEMVER_LT']
string_constraints.each do |operator_name|
Unleash::Constraint.new('env', operator_name, '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to test that the creation of the object succeeded w/o an exception anymore. It now should be a safe assumption.

Suggested change
Unleash::Constraint.new('env', operator_name, '')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test based on review you had earlier. Its not testing the object creation, its checking that unleash logs a warn message.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two Unleash::Constraint.new in this test (per operator_name). We only need to test the "sad" path, that the logger is getting called. No need to test the happy path....

That is at least what I meant to say.

expect(constraint.matches_context?(context)).to be true
end

it 'warns about constraint construction for invalid value types for operator' do
array_constraints = ['STR_CONTAINS', 'STR_ENDS_WITH', 'STR_STARTS_WITH', 'IN', 'NOT_IN']

array_constraints.each do |operator_name|
Unleash::Constraint.new('env', operator_name, [])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to test that the creation of the object succeeded w/o an exception anymore. It now should be a safe assumption.

Suggested change
Unleash::Constraint.new('env', operator_name, [])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test based on review you had earlier. Its not testing the object creation, its checking that unleash logs a warn message.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two Unleash::Constraint.new in this test (per operator_name). We only need to test the "sad" path, that the logger is getting called. No need to test the happy path....

That is at least what I meant to say.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done.

spec/unleash/context_spec.rb Outdated Show resolved Hide resolved
expect(constraint.matches_context?(context)).to be true
end

it 'warns about constraint construction for invalid value types for operator' do
array_constraints = ['STR_CONTAINS', 'STR_ENDS_WITH', 'STR_STARTS_WITH', 'IN', 'NOT_IN']

array_constraints.each do |operator_name|
Unleash::Constraint.new('env', operator_name, [])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two Unleash::Constraint.new in this test (per operator_name). We only need to test the "sad" path, that the logger is getting called. No need to test the happy path....

That is at least what I meant to say.

Unleash::Constraint.new('env', operator_name, '')
end.to raise_error
expect(Unleash.logger).to receive(:warn).with("value is a String, operator is expecting an Array")
Unleash::Constraint.new('env', operator_name, '')
end

string_constraints = ['NUM_EQ', 'NUM_GT', 'NUM_GTE', 'NUM_LT', 'NUM_LTE',
'DATE_AFTER', 'DATE_BEFORE', 'SEMVER_EQ', 'SEMVER_GT', 'SEMVER_LT']
string_constraints.each do |operator_name|
Unleash::Constraint.new('env', operator_name, '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two Unleash::Constraint.new in this test (per operator_name). We only need to test the "sad" path, that the logger is getting called. No need to test the happy path....

That is at least what I meant to say.

lib/unleash/constraint.rb Outdated Show resolved Hide resolved
Comment on lines 97 to 101
# when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match.
return self.operator == :NOT_IN unless context.property?(self.context_name)

Unleash.logger.debug "Unleash::Constraint matches_context? value: #{self.value} context.get_by_name(#{self.context_name})" \
" #{context.get_by_name(self.context_name)} "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not? context.get_by_name() should return nil if no value is found.

We would like to get this debug message for all operators, including the :NOT_IN, and even if the value is not in the context.

Also the node regarding having matches_constraint? instead of matches_context? within the log message is still valid.

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woot!

@gardleopard gardleopard merged commit 285ab7e into main Sep 13, 2022
@gardleopard gardleopard deleted the fix/client_spec_constraint_tests branch September 13, 2022 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants