]> git.openstreetmap.org Git - rails.git/commitdiff
Move Relation.to_xml and to_xml_node out of the model and into tests
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 20 Nov 2019 14:45:28 +0000 (15:45 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 20 Nov 2019 14:45:28 +0000 (15:45 +0100)
app/models/relation.rb
test/controllers/api/changesets_controller_test.rb
test/controllers/api/relations_controller_test.rb
test/test_helper.rb

index dc2b71ac4193f292ea6febf9e9df8176cc606647..a73d63183b44ee2b31dd7348f9d4e44e5e956c28 100644 (file)
@@ -121,31 +121,6 @@ class Relation < ActiveRecord::Base
     relation
   end
 
     relation
   end
 
-  def to_xml
-    doc = OSM::API.new.get_xml_doc
-    doc.root << to_xml_node
-    doc
-  end
-
-  def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
-    el = XML::Node.new "relation"
-    el["id"] = id.to_s
-
-    add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
-
-    relation_members.each do |member|
-      member_el = XML::Node.new "member"
-      member_el["type"] = member.member_type.downcase
-      member_el["ref"] = member.member_id.to_s
-      member_el["role"] = member.member_role
-      el << member_el
-    end
-
-    add_tags_to_xml_node(el, relation_tags)
-
-    el
-  end
-
   # FIXME: is this really needed?
   def members
     @members ||= relation_members.map do |member|
   # FIXME: is this really needed?
   def members
     @members ||= relation_members.map do |member|
index e7ad88eb8cb12c672c6db2357d464fe6a2ee12b6..a4583a9289e4c95ec90fc476afc163c94bfed890 100644 (file)
@@ -458,8 +458,8 @@ CHANGESET
       diff.root = XML::Node.new "osmChange"
       delete = XML::Node.new "delete"
       diff.root << delete
       diff.root = XML::Node.new "osmChange"
       delete = XML::Node.new "delete"
       diff.root << delete
-      delete << super_relation.to_xml_node
-      delete << used_relation.to_xml_node
+      delete << xml_node_for_relation(super_relation)
+      delete << xml_node_for_relation(used_relation)
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
@@ -590,7 +590,7 @@ CHANGESET
       diff.root = XML::Node.new "osmChange"
       delete = XML::Node.new "delete"
       diff.root << delete
       diff.root = XML::Node.new "osmChange"
       delete = XML::Node.new "delete"
       diff.root << delete
-      delete << other_relation.to_xml_node
+      delete << xml_node_for_relation(other_relation)
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
@@ -633,7 +633,7 @@ CHANGESET
       delete = XML::Node.new "delete"
       diff.root << delete
       delete["if-unused"] = ""
       delete = XML::Node.new "delete"
       diff.root << delete
       delete["if-unused"] = ""
-      delete << used_relation.to_xml_node
+      delete << xml_node_for_relation(used_relation)
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
       delete << xml_node_for_way(used_way)
       delete << xml_node_for_node(used_node)
 
index 4d2969026aa3621dea4936c2abf674425880553d..1b54e4863c52fe6024bd6a0eb1079012b40e1c24 100644 (file)
@@ -525,28 +525,28 @@ module Api
       assert_response :forbidden
 
       # try to delete with an invalid (closed) changeset
       assert_response :forbidden
 
       # try to delete with an invalid (closed) changeset
-      xml = update_changeset(relation.to_xml,
+      xml = update_changeset(xml_for_relation(relation),
                              private_user_closed_changeset.id)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # try to delete with an invalid (non-existent) changeset
                              private_user_closed_changeset.id)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # try to delete with an invalid (non-existent) changeset
-      xml = update_changeset(relation.to_xml, 0)
+      xml = update_changeset(xml_for_relation(relation), 0)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this won't work because the relation is in-use by another relation
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this won't work because the relation is in-use by another relation
-      xml = used_relation.to_xml
+      xml = xml_for_relation(used_relation)
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this should work when we provide the appropriate payload...
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this should work when we provide the appropriate payload...
-      xml = relation.to_xml
+      xml = xml_for_relation(relation)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this won't work since the relation is already deleted
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       # this won't work since the relation is already deleted
-      xml = deleted_relation.to_xml
+      xml = xml_for_relation(deleted_relation)
       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
       assert_response :forbidden
 
       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
       assert_response :forbidden
 
@@ -568,36 +568,36 @@ module Api
       assert_match(/Changeset id is missing/, @response.body)
 
       # try to delete with an invalid (closed) changeset
       assert_match(/Changeset id is missing/, @response.body)
 
       # try to delete with an invalid (closed) changeset
-      xml = update_changeset(relation.to_xml,
+      xml = update_changeset(xml_for_relation(relation),
                              closed_changeset.id)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict
 
       # try to delete with an invalid (non-existent) changeset
                              closed_changeset.id)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict
 
       # try to delete with an invalid (non-existent) changeset
-      xml = update_changeset(relation.to_xml, 0)
+      xml = update_changeset(xml_for_relation(relation), 0)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict
 
       # this won't work because the relation is in a changeset owned by someone else
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict
 
       # this won't work because the relation is in a changeset owned by someone else
-      xml = update_changeset(relation.to_xml, create(:changeset).id)
+      xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict,
                       "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
 
       # this won't work because the relation in the payload is different to that passed
       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
       assert_response :conflict,
                       "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
 
       # this won't work because the relation in the payload is different to that passed
-      xml = update_changeset(relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(relation), changeset.id)
       delete :delete, :params => { :id => create(:relation).id }, :body => xml.to_s
       assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
 
       # this won't work because the relation is in-use by another relation
       delete :delete, :params => { :id => create(:relation).id }, :body => xml.to_s
       assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
 
       # this won't work because the relation is in-use by another relation
-      xml = update_changeset(used_relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(used_relation), changeset.id)
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :precondition_failed,
                       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
       assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
 
       # this should work when we provide the appropriate payload...
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :precondition_failed,
                       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
       assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
 
       # this should work when we provide the appropriate payload...
-      xml = update_changeset(multi_tag_relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
       delete :delete, :params => { :id => multi_tag_relation.id }, :body => xml.to_s
       assert_response :success
 
       delete :delete, :params => { :id => multi_tag_relation.id }, :body => xml.to_s
       assert_response :success
 
@@ -607,18 +607,18 @@ module Api
              "delete request should return a new version number for relation"
 
       # this won't work since the relation is already deleted
              "delete request should return a new version number for relation"
 
       # this won't work since the relation is already deleted
-      xml = update_changeset(deleted_relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
       assert_response :gone
 
       # Public visible relation needs to be deleted
       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
       assert_response :gone
 
       # Public visible relation needs to be deleted
-      xml = update_changeset(super_relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(super_relation), changeset.id)
       delete :delete, :params => { :id => super_relation.id }, :body => xml.to_s
       assert_response :success
 
       # this works now because the relation which was using this one
       # has been deleted.
       delete :delete, :params => { :id => super_relation.id }, :body => xml.to_s
       assert_response :success
 
       # this works now because the relation which was using this one
       # has been deleted.
-      xml = update_changeset(used_relation.to_xml, changeset.id)
+      xml = update_changeset(xml_for_relation(used_relation), changeset.id)
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :success,
                       "should be able to delete a relation used in an old relation (#{@response.body})"
       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
       assert_response :success,
                       "should be able to delete a relation used in an old relation (#{@response.body})"
@@ -643,7 +643,7 @@ module Api
       # indirectly via the way), so the bbox should be [3,3,5,5].
       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
         # add a tag to an existing relation
       # indirectly via the way), so the bbox should be [3,3,5,5].
       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
         # add a tag to an existing relation
-        relation_xml = relation.to_xml
+        relation_xml = xml_for_relation(relation)
         relation_element = relation_xml.find("//osm/relation").first
         new_tag = XML::Node.new("tag")
         new_tag["k"] = "some_new_tag"
         relation_element = relation_xml.find("//osm/relation").first
         new_tag = XML::Node.new("tag")
         new_tag["k"] = "some_new_tag"
@@ -675,7 +675,7 @@ module Api
       [node1, node2, way1, way2].each do |element|
         bbox = element.bbox.to_unscaled
         check_changeset_modify(bbox) do |changeset_id|
       [node1, node2, way1, way2].each do |element|
         bbox = element.bbox.to_unscaled
         check_changeset_modify(bbox) do |changeset_id|
-          relation_xml = Relation.find(relation.id).to_xml
+          relation_xml = xml_for_relation(Relation.find(relation.id))
           relation_element = relation_xml.find("//osm/relation").first
           new_member = XML::Node.new("member")
           new_member["ref"] = element.id.to_s
           relation_element = relation_xml.find("//osm/relation").first
           new_member = XML::Node.new("member")
           new_member["ref"] = element.id.to_s
@@ -710,7 +710,7 @@ module Api
 
       check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
         # remove node 5 (5,5) from an existing relation
 
       check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
         # remove node 5 (5,5) from an existing relation
-        relation_xml = relation.to_xml
+        relation_xml = xml_for_relation(relation)
         relation_xml
           .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
           .first.remove!
         relation_xml
           .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
           .first.remove!
@@ -879,7 +879,7 @@ OSM
       create(:relation_member, :relation => relation, :member => node2)
 
       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
       create(:relation_member, :relation => relation, :member => node2)
 
       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
-        relation_xml = relation.to_xml
+        relation_xml = xml_for_relation(relation)
         relation_xml
           .find("//osm/relation/member")
           .each(&:remove!)
         relation_xml
           .find("//osm/relation/member")
           .each(&:remove!)
index 0ebd6313abd38b6de1f9a6608988713bf87cd0ab..05385b4f9c8678b85b207e352fe047e52bb9956c 100644 (file)
@@ -224,6 +224,31 @@ module ActiveSupport
       el
     end
 
       el
     end
 
+    def xml_for_relation(relation)
+      doc = OSM::API.new.get_xml_doc
+      doc.root << xml_node_for_relation(relation)
+      doc
+    end
+
+    def xml_node_for_relation(relation)
+      el = XML::Node.new "relation"
+      el["id"] = relation.id.to_s
+
+      OMHelper.add_metadata_to_xml_node(el, relation, {}, {})
+
+      relation.relation_members.each do |member|
+        member_el = XML::Node.new "member"
+        member_el["type"] = member.member_type.downcase
+        member_el["ref"] = member.member_id.to_s
+        member_el["role"] = member.member_role
+        el << member_el
+      end
+
+      OMHelper.add_tags_to_xml_node(el, relation.relation_tags)
+
+      el
+    end
+
     class OMHelper
       extend ObjectMetadata
     end
     class OMHelper
       extend ObjectMetadata
     end