Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Comparison operators should handle case where expectation is nil
Browse files Browse the repository at this point in the history
* While this should be rare, this does occur in the dm-is-list specs when
  testing against the yaml/in_memory adapters.
* Possible optimization in the future might be to add #valid? methods to
  each gt/gte/lt/lte Comparison operator that returns false if the
  expected value is nil. This will cause the query to never execute, saving
  the comparison operator from being called when in an invalid state. The
  object may still need to handle invalid states when called stand-alone
  through.
  • Loading branch information
dkubb committed Apr 9, 2011
1 parent 1dfc49a commit bfc0f83
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/dm-core/query/conditions/comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ class GreaterThanComparison < AbstractComparison
#
# @api semipublic
def matches?(record)
return false if expected.nil?
record_value = record_value(record)
!record_value.nil? && record_value > expected
end
Expand Down Expand Up @@ -827,6 +828,7 @@ class LessThanComparison < AbstractComparison
#
# @api semipublic
def matches?(record)
return false if expected.nil?
record_value = record_value(record)
!record_value.nil? && record_value < expected
end
Expand Down Expand Up @@ -857,6 +859,7 @@ class GreaterThanOrEqualToComparison < AbstractComparison
#
# @api semipublic
def matches?(record)
return false if expected.nil?
record_value = record_value(record)
!record_value.nil? && record_value >= expected
end
Expand Down Expand Up @@ -885,6 +888,7 @@ class LessThanOrEqualToComparison < AbstractComparison
#
# @api semipublic
def matches?(record)
return false if expected.nil?
record_value = record_value(record)
!record_value.nil? && record_value <= expected
end
Expand Down
40 changes: 40 additions & 0 deletions spec/semipublic/query/conditions/comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,16 @@ class Article

it { should be(false) }
end

describe 'with an expected value of nil' do
subject { @comparison.matches?(@model.new(@property => 2)) }

before do
@comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
end

it { should be(false) }
end
end
end

Expand Down Expand Up @@ -1276,6 +1286,16 @@ class Article

it { should be(false) }
end

describe 'with an expected value of nil' do
subject { @comparison.matches?(@model.new(@property => 0)) }

before do
@comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
end

it { should be(false) }
end
end
end

Expand Down Expand Up @@ -1358,6 +1378,16 @@ class Article

it { should be(false) }
end

describe 'with an expected value of nil' do
subject { @comparison.matches?(@model.new(@property => 1)) }

before do
@comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
end

it { should be(false) }
end
end
end

Expand Down Expand Up @@ -1440,6 +1470,16 @@ class Article

it { should be(false) }
end

describe 'with an expected value of nil' do
subject { @comparison.matches?(@model.new(@property => 1)) }

before do
@comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
end

it { should be(false) }
end
end
end

Expand Down

0 comments on commit bfc0f83

Please sign in to comment.