Skip to content

Commit

Permalink
Added more specs for Query :order option validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Kubb committed Mar 1, 2009
1 parent ecdd279 commit 25f26b1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
15 changes: 1 addition & 14 deletions lib/dm-core/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,20 +666,7 @@ def assert_valid_order(order, fields)
raise ArgumentError, "+options[:order]+ entry #{order.inspect} used an invalid operator #{order.operator}"
end

case target = order.target
when Property
unless @properties.include?(target)
raise ArgumentError, "+options[:order]+ entry #{target.inspect} does not map to a property"
end

when Symbol, String
unless @properties.named?(target)
raise ArgumentError, "+options[:order]+ entry #{target.inspect} does not map to a property"
end

else
raise ArgumentError, "+options[:order]+ entry #{order.inspect} does not contain a Property, Symbol or String, but was #{target.class}"
end
assert_valid_order([ order.target ], fields)

when Symbol, String
unless @properties.named?(order)
Expand Down
57 changes: 57 additions & 0 deletions spec/semipublic/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,63 @@ class ::User
}.should raise_error(ArgumentError, '+options[:order]+ entry 1 of an unsupported object Fixnum')
end
end

describe 'that contains a Query::Direction with a property that is not part of the model' do
before :all do
@property = DataMapper::Property.new(@model, :unknown, String)
@direction = DataMapper::Query::Direction.new(@property, :desc)
end

it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ @direction ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry :unknown does not map to a property')
end
end

describe 'that contains a Query::Operator with a target that is not part of the model' do
it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ :unknown.desc ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry :unknown does not map to a property')
end
end

describe 'that contains a Query::Operator with an unknown operator' do
it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ :name.gt ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry #<DataMapper::Query::Operator @target=:name @operator=:gt> used an invalid operator gt')
end
end

describe 'that contains a Property that is not part of the model' do
before :all do
@property = DataMapper::Property.new(@model, :unknown, String)
end

it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ @property ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry :unknown does not map to a property')
end
end

describe 'that contains a Symbol that is not for a Property in the model' do
it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ :unknown ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry :unknown does not map to a property')
end
end

describe 'that contains a String that is not for a Property in the model' do
it 'should raise an exception' do
lambda {
DataMapper::Query.new(@repository, @model, @options.update(:order => [ 'unknown' ]))
}.should raise_error(ArgumentError, '+options[:order]+ entry "unknown" does not map to a property')
end
end
end

describe 'with a unique option' do
Expand Down

0 comments on commit 25f26b1

Please sign in to comment.