# code in 6 places for all the updates and deletes. Some of these tests are
# needed for creates, but are currently not run :-(
# This will throw an exception if there is an inconsistency
- def check_consistency(old, new, user)
+ def check_update_element_consistency(old, new, user)
if new.id != old.id || new.id.nil? || old.id.nil?
raise OSM::APIPreconditionFailedError, "New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}."
elsif new.version != old.version
# shouldn't be possible to get race conditions.
Node.transaction do
lock!
- check_consistency(self, new_node, user)
+ check_update_element_consistency(self, new_node, user)
ways = Way.joins(:way_nodes).where(:visible => true, :current_way_nodes => { :node_id => id }).order(:id)
raise OSM::APIPreconditionFailedError, "Node #{id} is still used by ways #{ways.collect(&:id).join(',')}." unless ways.empty?
def update_from(new_node, user)
Node.transaction do
lock!
- check_consistency(self, new_node, user)
+ check_update_element_consistency(self, new_node, user)
# update changeset first
self.changeset_id = new_node.changeset_id
# shouldn't be possible to get race conditions.
Relation.transaction do
lock!
- check_consistency(self, new_relation, user)
+ check_update_element_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
rel = RelationMember.joins(:relation).find_by("visible = ? AND member_type = 'Relation' and member_id = ? ", true, id)
raise OSM::APIPreconditionFailedError, "The relation #{new_relation.id} is used in relation #{rel.relation.id}." unless rel.nil?
def update_from(new_relation, user)
Relation.transaction do
lock!
- check_consistency(self, new_relation, user)
+ check_update_element_consistency(self, new_relation, user)
raise OSM::APIPreconditionFailedError, "Cannot update relation #{id}: data or member data is invalid." unless new_relation.preconditions_ok?(members)
self.changeset_id = new_relation.changeset_id
def update_from(new_way, user)
Way.transaction do
lock!
- check_consistency(self, new_way, user)
+ check_update_element_consistency(self, new_way, user)
raise OSM::APIPreconditionFailedError, "Cannot update way #{id}: data is invalid." unless new_way.preconditions_ok?(nds)
self.changeset_id = new_way.changeset_id
# shouldn't be possible to get race conditions.
Way.transaction do
lock!
- check_consistency(self, new_way, user)
+ check_update_element_consistency(self, new_way, user)
rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Way", :member_id => id }).order(:id)
raise OSM::APIPreconditionFailedError, "Way #{id} is still used by relations #{rels.collect(&:id).join(',')}." unless rels.empty?
node.update_from(new_node, user)
end
end
+
+ test "raises id precondition exception when deleting" do
+ user = create(:user)
+ node = Node.new(:id => 23, :visible => true)
+ new_node = Node.new(:id => 42, :visible => false)
+ assert_raises OSM::APIPreconditionFailedError do
+ node.delete_with_history!(new_node, user)
+ end
+ end
end
relation.update_from(new_relation, user)
end
end
+
+ test "raises id precondition exception when deleting" do
+ user = create(:user)
+ relation = Relation.new(:id => 23, :visible => true)
+ new_relation = Relation.new(:id => 42, :visible => false)
+ assert_raises OSM::APIPreconditionFailedError do
+ relation.delete_with_history!(new_relation, user)
+ end
+ end
end
way.update_from(new_way, user)
end
end
+
+ test "raises id precondition exception when deleting" do
+ user = create(:user)
+ way = Way.new(:id => 23, :visible => true)
+ new_way = Way.new(:id => 42, :visible => false)
+ assert_raises OSM::APIPreconditionFailedError do
+ way.delete_with_history!(new_way, user)
+ end
+ end
end