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

Commit a0ecbfb

Browse files
committed
Remove unecessary assertions from DataMapper::Query
* These assertions were originally meant to prevent common errors, but I now believe they do more harm than good because they prevent other kinds of objects from being injected into the system and then used in the adapters.
1 parent b7f51bd commit a0ecbfb

2 files changed

Lines changed: 1 addition & 147 deletions

File tree

lib/dm-core/query.rb

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,6 @@ def assert_valid_options(options)
783783
#
784784
# @api private
785785
def assert_valid_fields(fields, unique)
786-
fields = fields.to_ary
787-
788-
model = self.model
789-
790786
valid_properties = model.properties
791787

792788
model.descendants.each do |descendant|
@@ -799,14 +795,6 @@ def assert_valid_fields(fields, unique)
799795
unless valid_properties.named?(field)
800796
raise ArgumentError, "+options[:fields]+ entry #{field.inspect} does not map to a property in #{model}"
801797
end
802-
803-
when Property
804-
unless valid_properties.include?(field)
805-
raise ArgumentError, "+options[:field]+ entry #{field.name.inspect} does not map to a property in #{model}"
806-
end
807-
808-
else
809-
raise ArgumentError, "+options[:fields]+ entry #{field.inspect} of an unsupported object #{field.class}"
810798
end
811799
end
812800
end
@@ -816,8 +804,6 @@ def assert_valid_fields(fields, unique)
816804
#
817805
# @api private
818806
def assert_valid_links(links)
819-
links = links.to_ary
820-
821807
if links.empty?
822808
raise ArgumentError, '+options[:links]+ should not be empty'
823809
end
@@ -828,15 +814,6 @@ def assert_valid_links(links)
828814
unless @relationships.named?(link.to_sym)
829815
raise ArgumentError, "+options[:links]+ entry #{link.inspect} does not map to a relationship in #{model}"
830816
end
831-
832-
when Associations::Relationship
833-
# TODO: figure out how to validate links from other models
834-
#unless @relationships.value?(link)
835-
# raise ArgumentError, "+options[:links]+ entry #{link.name.inspect} does not map to a relationship in #{model}"
836-
#end
837-
838-
else
839-
raise ArgumentError, "+options[:links]+ entry #{link.inspect} of an unsupported object #{link.class}"
840817
end
841818
end
842819
end
@@ -852,40 +829,14 @@ def assert_valid_conditions(conditions)
852829
when Hash
853830
conditions.each do |subject, bind_value|
854831
case subject
855-
when Symbol, ::String
832+
when Symbol, String
856833
original = subject
857834
subject = subject.to_s
858835
name = subject[0, subject.index('.') || subject.length]
859836

860837
unless @properties.named?(name) || @relationships.named?(name)
861838
raise ArgumentError, "condition #{original.inspect} does not map to a property or relationship in #{model}"
862839
end
863-
864-
when Property
865-
unless @properties.include?(subject)
866-
raise ArgumentError, "condition #{subject.name.inspect} does not map to a property in #{model}, but belongs to #{subject.model}"
867-
end
868-
869-
when Operator
870-
operator = subject.operator
871-
872-
unless Conditions::Comparison.slugs.include?(operator) || operator == :not
873-
raise ArgumentError, "condition #{subject.inspect} used an invalid operator #{operator}"
874-
end
875-
876-
assert_valid_conditions(subject.target => bind_value)
877-
878-
when Path
879-
assert_valid_links(subject.relationships)
880-
881-
when Associations::Relationship
882-
# TODO: validate that it belongs to the current model
883-
#unless subject.source_model.equal?(model)
884-
# raise ArgumentError, "condition #{subject.name.inspect} is not a valid relationship for #{model}, it's source model was #{subject.source_model}"
885-
#end
886-
887-
else
888-
raise ArgumentError, "condition #{subject.inspect} of an unsupported object #{subject.class}"
889840
end
890841
end
891842

@@ -942,30 +893,12 @@ def assert_valid_order(order, fields)
942893
raise ArgumentError, '+options[:order]+ should not be empty if +options[:fields] contains a non-operator'
943894
end
944895

945-
model = self.model
946-
947896
order.each do |order_entry|
948897
case order_entry
949898
when Symbol, String
950899
unless @properties.named?(order_entry)
951900
raise ArgumentError, "+options[:order]+ entry #{order_entry.inspect} does not map to a property in #{model}"
952901
end
953-
954-
when Property, Path
955-
# Allow any arbitrary property, since it may map to a model
956-
# that has been included via the :links option
957-
958-
when Operator, Direction
959-
operator = order_entry.operator
960-
961-
unless operator == :asc || operator == :desc
962-
raise ArgumentError, "+options[:order]+ entry #{order_entry.inspect} used an invalid operator #{operator}"
963-
end
964-
965-
assert_valid_order([ order_entry.target ], fields)
966-
967-
else
968-
raise ArgumentError, "+options[:order]+ entry #{order_entry.inspect} of an unsupported object #{order_entry.class}"
969902
end
970903
end
971904
end

spec/semipublic/query_spec.rb

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,6 @@ class ::Contact < User; end
189189
}.should raise_error(ArgumentError, "+options[:fields]+ entry \"unknown\" does not map to a property in #{@model}")
190190
end
191191
end
192-
193-
describe 'that is an Array containing an invalid object' do
194-
it 'should raise an exception' do
195-
lambda {
196-
DataMapper::Query.new(@repository, @model, @options.update(:fields => [ 1 ]))
197-
}.should raise_error(ArgumentError, '+options[:fields]+ entry 1 of an unsupported object Fixnum')
198-
end
199-
end
200-
201-
describe 'that is an Array containing an unknown Property' do
202-
it 'should raise an exception' do
203-
lambda {
204-
DataMapper::Query.new(@repository, @model, @options.update(:fields => [ DataMapper::Property::String.new(@model, :unknown) ]))
205-
}.should raise_error(ArgumentError, "+options[:field]+ entry :unknown does not map to a property in #{@model}")
206-
end
207-
end
208192
end
209193

210194
describe 'with a links option' do
@@ -291,14 +275,6 @@ class ::Contact < User; end
291275
}.should raise_error(ArgumentError, "+options[:links]+ entry \"unknown\" does not map to a relationship in #{@model}")
292276
end
293277
end
294-
295-
describe 'that is an Array containing an invalid object' do
296-
it 'should raise an exception' do
297-
lambda {
298-
DataMapper::Query.new(@repository, @model, @options.update(:links => [ 1 ]))
299-
}.should raise_error(ArgumentError, '+options[:links]+ entry 1 of an unsupported object Fixnum')
300-
end
301-
end
302278
end
303279

304280
describe 'with a conditions option' do
@@ -794,37 +770,6 @@ class ::Contact < User; end
794770
}.should raise_error(ArgumentError, "condition \"unknown.id\" does not map to a property or relationship in #{@model}")
795771
end
796772
end
797-
798-
describe 'that is a Hash with a Property that does not belong to the model' do
799-
before do
800-
Object.send(:remove_const, :Alternate) if Object.const_defined?(:Alternate)
801-
@alternate_model = DataMapper::Model.new('Alternate') do
802-
property :id, DataMapper::Property::Serial
803-
end
804-
end
805-
806-
it 'should raise an exception' do
807-
lambda {
808-
DataMapper::Query.new(@repository, @model, @options.update(:conditions => { @alternate_model.properties[:id] => 1 }))
809-
}.should raise_error(ArgumentError, "condition :id does not map to a property in #{@model}, but belongs to #{@alternate_model}")
810-
end
811-
end
812-
813-
describe 'that is a Hash with a Query::Operator key that is not for a Property in the model' do
814-
it 'should raise an exception' do
815-
lambda {
816-
DataMapper::Query.new(@repository, @model, @options.update(:conditions => { :unknown.asc => 1 }))
817-
}.should raise_error(ArgumentError, 'condition #<DataMapper::Query::Operator @target=:unknown @operator=:asc> used an invalid operator asc')
818-
end
819-
end
820-
821-
describe 'that is a Hash with a key of a type that is not permitted' do
822-
it 'should raise an exception' do
823-
lambda {
824-
DataMapper::Query.new(@repository, @model, @options.update(:conditions => { 1 => 1 }))
825-
}.should raise_error(ArgumentError, 'condition 1 of an unsupported object Fixnum')
826-
end
827-
end
828773
end
829774

830775
describe 'with an offset option' do
@@ -1129,30 +1074,6 @@ class ::Contact < User; end
11291074
end
11301075
end
11311076

1132-
describe 'that is an Array containing an invalid object' do
1133-
it 'should raise an exception' do
1134-
lambda {
1135-
DataMapper::Query.new(@repository, @model, @options.update(:order => [ 1 ]))
1136-
}.should raise_error(ArgumentError, '+options[:order]+ entry 1 of an unsupported object Fixnum')
1137-
end
1138-
end
1139-
1140-
describe 'that contains a Query::Operator with a target that is not part of the model' do
1141-
it 'should raise an exception' do
1142-
lambda {
1143-
DataMapper::Query.new(@repository, @model, @options.update(:order => [ :unknown.desc ]))
1144-
}.should raise_error(ArgumentError, "+options[:order]+ entry :unknown does not map to a property in #{@model}")
1145-
end
1146-
end
1147-
1148-
describe 'that contains a Query::Operator with an unknown operator' do
1149-
it 'should raise an exception' do
1150-
lambda {
1151-
DataMapper::Query.new(@repository, @model, @options.update(:order => [ :name.gt ]))
1152-
}.should raise_error(ArgumentError, '+options[:order]+ entry #<DataMapper::Query::Operator @target=:name @operator=:gt> used an invalid operator gt')
1153-
end
1154-
end
1155-
11561077
describe 'that contains a Symbol that is not for a Property in the model' do
11571078
it 'should raise an exception' do
11581079
lambda {

0 commit comments

Comments
 (0)