1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
4 class RelationControllerTest < ActionController::TestCase
7 # -------------------------------------
8 # Test reading relations.
9 # -------------------------------------
12 # check that a visible relation is returned properly
13 get :read, :id => current_relations(:visible_relation).id
14 assert_response :success
16 # check that an invisible relation is not returned
17 get :read, :id => current_relations(:invisible_relation).id
20 # check chat a non-existent relation is not returned
22 assert_response :not_found
26 # check that all relations containing a particular node, and no extra
27 # relations, are returned from the relations_for_node call.
28 def test_relations_for_node
29 check_relations_for_element(:relations_for_node, "node",
30 current_nodes(:node_used_by_relationship).id,
31 [ :visible_relation, :used_relation ])
34 def test_relations_for_way
35 check_relations_for_element(:relations_for_way, "way",
36 current_ways(:used_way).id,
37 [ :visible_relation ])
40 def test_relations_for_relation
41 check_relations_for_element(:relations_for_relation, "relation",
42 current_relations(:used_relation).id,
43 [ :visible_relation ])
46 def check_relations_for_element(method, type, id, expected_relations)
47 # check the "relations for relation" mode
49 assert_response :success
51 # count one osm element
52 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
54 # we should have only the expected number of relations
55 assert_select "osm>relation", expected_relations.size
57 # and each of them should contain the node we originally searched for
58 expected_relations.each do |r|
59 relation_id = current_relations(r).id
60 assert_select "osm>relation#?", relation_id
61 assert_select "osm>relation#?>member[type=\"#{type}\"][ref=#{id}]", relation_id
66 # check the "full" mode
67 get :full, :id => current_relations(:visible_relation).id
68 assert_response :success
69 # FIXME check whether this contains the stuff we want!
75 # -------------------------------------
76 # Test simple relation creation.
77 # -------------------------------------
80 basic_authorization users(:normal_user).email, "test"
82 # put the relation in a dummy fixture changset
83 changeset_id = changesets(:normal_user_first_change).id
85 # create an relation without members
86 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
88 # hope for forbidden, due to user
89 assert_response :forbidden,
90 "relation upload should have failed with forbidden"
93 # create an relation with a node as member
94 # This time try with a role attribute in the relation
95 nid = current_nodes(:used_node_1).id
96 content "<osm><relation changeset='#{changeset_id}'>" +
97 "<member ref='#{nid}' type='node' role='some'/>" +
98 "<tag k='test' v='yes' /></relation></osm>"
100 # hope for forbidden due to user
101 assert_response :forbidden,
102 "relation upload did not return forbidden status"
105 # create an relation with a node as member, this time test that we don't
106 # need a role attribute to be included
107 nid = current_nodes(:used_node_1).id
108 content "<osm><relation changeset='#{changeset_id}'>" +
109 "<member ref='#{nid}' type='node'/>"+
110 "<tag k='test' v='yes' /></relation></osm>"
112 # hope for forbidden due to user
113 assert_response :forbidden,
114 "relation upload did not return forbidden status"
117 # create an relation with a way and a node as members
118 nid = current_nodes(:used_node_1).id
119 wid = current_ways(:used_way).id
120 content "<osm><relation changeset='#{changeset_id}'>" +
121 "<member type='node' ref='#{nid}' role='some'/>" +
122 "<member type='way' ref='#{wid}' role='other'/>" +
123 "<tag k='test' v='yes' /></relation></osm>"
125 # hope for forbidden, due to user
126 assert_response :forbidden,
127 "relation upload did not return success status"
131 ## Now try with the public user
132 basic_authorization users(:public_user).email, "test"
134 # put the relation in a dummy fixture changset
135 changeset_id = changesets(:public_user_first_change).id
137 # create an relation without members
138 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
141 assert_response :success,
142 "relation upload did not return success status"
143 # read id of created relation and search for it
144 relationid = @response.body
145 checkrelation = Relation.find(relationid)
146 assert_not_nil checkrelation,
147 "uploaded relation not found in data base after upload"
149 assert_equal checkrelation.members.length, 0,
150 "saved relation contains members but should not"
151 assert_equal checkrelation.tags.length, 1,
152 "saved relation does not contain exactly one tag"
153 assert_equal changeset_id, checkrelation.changeset.id,
154 "saved relation does not belong in the changeset it was assigned to"
155 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
156 "saved relation does not belong to user that created it"
157 assert_equal true, checkrelation.visible,
158 "saved relation is not visible"
159 # ok the relation is there but can we also retrieve it?
160 get :read, :id => relationid
161 assert_response :success
165 # create an relation with a node as member
166 # This time try with a role attribute in the relation
167 nid = current_nodes(:used_node_1).id
168 content "<osm><relation changeset='#{changeset_id}'>" +
169 "<member ref='#{nid}' type='node' role='some'/>" +
170 "<tag k='test' v='yes' /></relation></osm>"
173 assert_response :success,
174 "relation upload did not return success status"
175 # read id of created relation and search for it
176 relationid = @response.body
177 checkrelation = Relation.find(relationid)
178 assert_not_nil checkrelation,
179 "uploaded relation not found in data base after upload"
181 assert_equal checkrelation.members.length, 1,
182 "saved relation does not contain exactly one member"
183 assert_equal checkrelation.tags.length, 1,
184 "saved relation does not contain exactly one tag"
185 assert_equal changeset_id, checkrelation.changeset.id,
186 "saved relation does not belong in the changeset it was assigned to"
187 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
188 "saved relation does not belong to user that created it"
189 assert_equal true, checkrelation.visible,
190 "saved relation is not visible"
191 # ok the relation is there but can we also retrieve it?
193 get :read, :id => relationid
194 assert_response :success
198 # create an relation with a node as member, this time test that we don't
199 # need a role attribute to be included
200 nid = current_nodes(:used_node_1).id
201 content "<osm><relation changeset='#{changeset_id}'>" +
202 "<member ref='#{nid}' type='node'/>"+
203 "<tag k='test' v='yes' /></relation></osm>"
206 assert_response :success,
207 "relation upload did not return success status"
208 # read id of created relation and search for it
209 relationid = @response.body
210 checkrelation = Relation.find(relationid)
211 assert_not_nil checkrelation,
212 "uploaded relation not found in data base after upload"
214 assert_equal checkrelation.members.length, 1,
215 "saved relation does not contain exactly one member"
216 assert_equal checkrelation.tags.length, 1,
217 "saved relation does not contain exactly one tag"
218 assert_equal changeset_id, checkrelation.changeset.id,
219 "saved relation does not belong in the changeset it was assigned to"
220 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
221 "saved relation does not belong to user that created it"
222 assert_equal true, checkrelation.visible,
223 "saved relation is not visible"
224 # ok the relation is there but can we also retrieve it?
226 get :read, :id => relationid
227 assert_response :success
230 # create an relation with a way and a node as members
231 nid = current_nodes(:used_node_1).id
232 wid = current_ways(:used_way).id
233 content "<osm><relation changeset='#{changeset_id}'>" +
234 "<member type='node' ref='#{nid}' role='some'/>" +
235 "<member type='way' ref='#{wid}' role='other'/>" +
236 "<tag k='test' v='yes' /></relation></osm>"
239 assert_response :success,
240 "relation upload did not return success status"
241 # read id of created relation and search for it
242 relationid = @response.body
243 checkrelation = Relation.find(relationid)
244 assert_not_nil checkrelation,
245 "uploaded relation not found in data base after upload"
247 assert_equal checkrelation.members.length, 2,
248 "saved relation does not have exactly two members"
249 assert_equal checkrelation.tags.length, 1,
250 "saved relation does not contain exactly one tag"
251 assert_equal changeset_id, checkrelation.changeset.id,
252 "saved relation does not belong in the changeset it was assigned to"
253 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
254 "saved relation does not belong to user that created it"
255 assert_equal true, checkrelation.visible,
256 "saved relation is not visible"
257 # ok the relation is there but can we also retrieve it?
258 get :read, :id => relationid
259 assert_response :success
263 # ------------------------------------
264 # Test updating relations
265 # ------------------------------------
268 # test that, when tags are updated on a relation, the correct things
269 # happen to the correct tables and the API gives sensible results.
270 # this is to test a case that gregory marler noticed and posted to
272 ## FIXME Move this to an integration test
273 def test_update_relation_tags
274 basic_authorization "test@example.com", "test"
275 rel_id = current_relations(:multi_tag_relation).id
276 cs_id = changesets(:public_user_first_change).id
278 with_relation(rel_id) do |rel|
279 # alter one of the tags
280 tag = rel.find("//osm/relation/tag").first
281 tag['v'] = 'some changed value'
282 update_changeset(rel, cs_id)
284 # check that the downloaded tags are the same as the uploaded tags...
285 new_version = with_update(rel) do |new_rel|
286 assert_tags_equal rel, new_rel
289 # check the original one in the current_* table again
290 with_relation(rel_id) { |r| assert_tags_equal rel, r }
292 # now check the version in the history
293 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
298 # test that, when tags are updated on a relation when using the diff
299 # upload function, the correct things happen to the correct tables
300 # and the API gives sensible results. this is to test a case that
301 # gregory marler noticed and posted to josm-dev.
302 def test_update_relation_tags_via_upload
303 basic_authorization users(:public_user).email, "test"
304 rel_id = current_relations(:multi_tag_relation).id
305 cs_id = changesets(:public_user_first_change).id
307 with_relation(rel_id) do |rel|
308 # alter one of the tags
309 tag = rel.find("//osm/relation/tag").first
310 tag['v'] = 'some changed value'
311 update_changeset(rel, cs_id)
313 # check that the downloaded tags are the same as the uploaded tags...
314 new_version = with_update_diff(rel) do |new_rel|
315 assert_tags_equal rel, new_rel
318 # check the original one in the current_* table again
319 with_relation(rel_id) { |r| assert_tags_equal rel, r }
321 # now check the version in the history
322 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
326 # -------------------------------------
327 # Test creating some invalid relations.
328 # -------------------------------------
330 def test_create_invalid
331 basic_authorization users(:public_user).email, "test"
333 # put the relation in a dummy fixture changset
334 changeset_id = changesets(:public_user_first_change).id
336 # create a relation with non-existing node as member
337 content "<osm><relation changeset='#{changeset_id}'>" +
338 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
342 assert_response :precondition_failed,
343 "relation upload with invalid node did not return 'precondition failed'"
344 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
347 # -------------------------------------
348 # Test creating a relation, with some invalid XML
349 # -------------------------------------
350 def test_create_invalid_xml
351 basic_authorization users(:public_user).email, "test"
353 # put the relation in a dummy fixture changeset that works
354 changeset_id = changesets(:public_user_first_change).id
356 # create some xml that should return an error
357 content "<osm><relation changeset='#{changeset_id}'>" +
358 "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
359 "<tag k='tester' v='yep'/></relation></osm>"
362 assert_response :bad_request
363 assert_match(/Cannot parse valid relation from xml string/, @response.body)
364 assert_match(/The type is not allowed only, /, @response.body)
368 # -------------------------------------
369 # Test deleting relations.
370 # -------------------------------------
373 ## First try to delete relation without auth
374 delete :delete, :id => current_relations(:visible_relation).id
375 assert_response :unauthorized
378 ## Then try with the private user, to make sure that you get a forbidden
379 basic_authorization(users(:normal_user).email, "test")
381 # this shouldn't work, as we should need the payload...
382 delete :delete, :id => current_relations(:visible_relation).id
383 assert_response :forbidden
385 # try to delete without specifying a changeset
386 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
387 delete :delete, :id => current_relations(:visible_relation).id
388 assert_response :forbidden
390 # try to delete with an invalid (closed) changeset
391 content update_changeset(current_relations(:visible_relation).to_xml,
392 changesets(:normal_user_closed_change).id)
393 delete :delete, :id => current_relations(:visible_relation).id
394 assert_response :forbidden
396 # try to delete with an invalid (non-existent) changeset
397 content update_changeset(current_relations(:visible_relation).to_xml,0)
398 delete :delete, :id => current_relations(:visible_relation).id
399 assert_response :forbidden
401 # this won't work because the relation is in-use by another relation
402 content(relations(:used_relation).to_xml)
403 delete :delete, :id => current_relations(:used_relation).id
404 assert_response :forbidden
406 # this should work when we provide the appropriate payload...
407 content(relations(:visible_relation).to_xml)
408 delete :delete, :id => current_relations(:visible_relation).id
409 assert_response :forbidden
411 # this won't work since the relation is already deleted
412 content(relations(:invisible_relation).to_xml)
413 delete :delete, :id => current_relations(:invisible_relation).id
414 assert_response :forbidden
416 # this works now because the relation which was using this one
418 content(relations(:used_relation).to_xml)
419 delete :delete, :id => current_relations(:used_relation).id
420 assert_response :forbidden
422 # this won't work since the relation never existed
423 delete :delete, :id => 0
424 assert_response :forbidden
428 ## now set auth for the public user
429 basic_authorization(users(:public_user).email, "test");
431 # this shouldn't work, as we should need the payload...
432 delete :delete, :id => current_relations(:visible_relation).id
433 assert_response :bad_request
435 # try to delete without specifying a changeset
436 content "<osm><relation id='#{current_relations(:visible_relation).id}' version='#{current_relations(:visible_relation).version}' /></osm>"
437 delete :delete, :id => current_relations(:visible_relation).id
438 assert_response :bad_request
439 assert_match(/Changeset id is missing/, @response.body)
441 # try to delete with an invalid (closed) changeset
442 content update_changeset(current_relations(:visible_relation).to_xml,
443 changesets(:normal_user_closed_change).id)
444 delete :delete, :id => current_relations(:visible_relation).id
445 assert_response :conflict
447 # try to delete with an invalid (non-existent) changeset
448 content update_changeset(current_relations(:visible_relation).to_xml,0)
449 delete :delete, :id => current_relations(:visible_relation).id
450 assert_response :conflict
452 # this won't work because the relation is in a changeset owned by someone else
453 content(relations(:used_relation).to_xml)
454 delete :delete, :id => current_relations(:used_relation).id
455 assert_response :conflict,
456 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
458 # this won't work because the relation in the payload is different to that passed
459 content(relations(:public_used_relation).to_xml)
460 delete :delete, :id => current_relations(:used_relation).id
461 assert_not_equal relations(:public_used_relation).id, current_relations(:used_relation).id
462 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
464 # this won't work because the relation is in-use by another relation
465 content(relations(:public_used_relation).to_xml)
466 delete :delete, :id => current_relations(:public_used_relation).id
467 assert_response :precondition_failed,
468 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
469 assert_equal "Precondition failed: The relation 5 is used in relation 6.", @response.body
471 # this should work when we provide the appropriate payload...
472 content(relations(:multi_tag_relation).to_xml)
473 delete :delete, :id => current_relations(:multi_tag_relation).id
474 assert_response :success
476 # valid delete should return the new version number, which should
477 # be greater than the old version number
478 assert @response.body.to_i > current_relations(:visible_relation).version,
479 "delete request should return a new version number for relation"
481 # this won't work since the relation is already deleted
482 content(relations(:invisible_relation).to_xml)
483 delete :delete, :id => current_relations(:invisible_relation).id
484 assert_response :gone
486 # Public visible relation needs to be deleted
487 content(relations(:public_visible_relation).to_xml)
488 delete :delete, :id => current_relations(:public_visible_relation).id
489 assert_response :success
491 # this works now because the relation which was using this one
493 content(relations(:public_used_relation).to_xml)
494 delete :delete, :id => current_relations(:public_used_relation).id
495 assert_response :success,
496 "should be able to delete a relation used in an old relation (#{@response.body})"
498 # this won't work since the relation never existed
499 delete :delete, :id => 0
500 assert_response :not_found
504 # when a relation's tag is modified then it should put the bounding
505 # box of all its members into the changeset.
506 def test_tag_modify_bounding_box
507 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
508 # indirectly via way 3), so the bbox should be [3,3,5,5].
509 check_changeset_modify(BoundingBox.new(3,3,5,5)) do |changeset_id|
510 # add a tag to an existing relation
511 relation_xml = current_relations(:visible_relation).to_xml
512 relation_element = relation_xml.find("//osm/relation").first
513 new_tag = XML::Node.new("tag")
514 new_tag['k'] = "some_new_tag"
515 new_tag['v'] = "some_new_value"
516 relation_element << new_tag
518 # update changeset ID to point to new changeset
519 update_changeset(relation_xml, changeset_id)
523 put :update, :id => current_relations(:visible_relation).id
524 assert_response :success, "can't update relation for tag/bbox test"
529 # add a member to a relation and check the bounding box is only that
531 def test_add_member_bounding_box
532 relation_id = current_relations(:visible_relation).id
534 [current_nodes(:used_node_1),
535 current_nodes(:used_node_2),
536 current_ways(:used_way),
537 current_ways(:way_with_versions)
538 ].each_with_index do |element, version|
539 bbox = element.bbox.to_unscaled
540 check_changeset_modify(bbox) do |changeset_id|
541 relation_xml = Relation.find(relation_id).to_xml
542 relation_element = relation_xml.find("//osm/relation").first
543 new_member = XML::Node.new("member")
544 new_member['ref'] = element.id.to_s
545 new_member['type'] = element.class.to_s.downcase
546 new_member['role'] = "some_role"
547 relation_element << new_member
549 # update changeset ID to point to new changeset
550 update_changeset(relation_xml, changeset_id)
554 put :update, :id => current_relations(:visible_relation).id
555 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
557 # get it back and check the ordering
558 get :read, :id => relation_id
559 assert_response :success, "can't read back the relation: #{@response.body}"
560 check_ordering(relation_xml, @response.body)
566 # remove a member from a relation and check the bounding box is
568 def test_remove_member_bounding_box
569 check_changeset_modify(BoundingBox.new(5,5,5,5)) do |changeset_id|
570 # remove node 5 (5,5) from an existing relation
571 relation_xml = current_relations(:visible_relation).to_xml
573 find("//osm/relation/member[@type='node'][@ref='5']").
576 # update changeset ID to point to new changeset
577 update_changeset(relation_xml, changeset_id)
581 put :update, :id => current_relations(:visible_relation).id
582 assert_response :success, "can't update relation for remove node/bbox test"
587 # check that relations are ordered
588 def test_relation_member_ordering
589 basic_authorization(users(:public_user).email, "test")
593 <relation changeset='4'>
594 <member ref='1' type='node' role='first'/>
595 <member ref='3' type='node' role='second'/>
596 <member ref='1' type='way' role='third'/>
597 <member ref='3' type='way' role='fourth'/>
601 doc = XML::Parser.string(doc_str).parse
605 assert_response :success, "can't create a relation: #{@response.body}"
606 relation_id = @response.body.to_i
608 # get it back and check the ordering
609 get :read, :id => relation_id
610 assert_response :success, "can't read back the relation: #{@response.body}"
611 check_ordering(doc, @response.body)
613 # insert a member at the front
614 new_member = XML::Node.new "member"
615 new_member['ref'] = 5.to_s
616 new_member['type'] = 'node'
617 new_member['role'] = 'new first'
618 doc.find("//osm/relation").first.child.prev = new_member
619 # update the version, should be 1?
620 doc.find("//osm/relation").first['id'] = relation_id.to_s
621 doc.find("//osm/relation").first['version'] = 1.to_s
623 # upload the next version of the relation
625 put :update, :id => relation_id
626 assert_response :success, "can't update relation: #{@response.body}"
627 new_version = @response.body.to_i
629 # get it back again and check the ordering again
630 get :read, :id => relation_id
631 assert_response :success, "can't read back the relation: #{@response.body}"
632 check_ordering(doc, @response.body)
634 # check the ordering in the history tables:
635 with_controller(OldRelationController.new) do
636 get :version, :id => relation_id, :version => 2
637 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
638 check_ordering(doc, @response.body)
643 # check that relations can contain duplicate members
644 def test_relation_member_duplicates
647 <relation changeset='4'>
648 <member ref='1' type='node' role='forward'/>
649 <member ref='3' type='node' role='forward'/>
650 <member ref='1' type='node' role='forward'/>
651 <member ref='3' type='node' role='forward'/>
655 doc = XML::Parser.string(doc_str).parse
657 ## First try with the private user
658 basic_authorization(users(:normal_user).email, "test");
662 assert_response :forbidden
664 ## Now try with the public user
665 basic_authorization(users(:public_user).email, "test");
669 assert_response :success, "can't create a relation: #{@response.body}"
670 relation_id = @response.body.to_i
672 # get it back and check the ordering
673 get :read, :id => relation_id
674 assert_response :success, "can't read back the relation: #{relation_id}"
675 check_ordering(doc, @response.body)
679 # test that the ordering of elements in the history is the same as in current.
680 def test_history_ordering
683 <relation changeset='4'>
684 <member ref='1' type='node' role='forward'/>
685 <member ref='5' type='node' role='forward'/>
686 <member ref='4' type='node' role='forward'/>
687 <member ref='3' type='node' role='forward'/>
691 doc = XML::Parser.string(doc_str).parse
692 basic_authorization(users(:public_user).email, "test");
696 assert_response :success, "can't create a relation: #{@response.body}"
697 relation_id = @response.body.to_i
699 # check the ordering in the current tables:
700 get :read, :id => relation_id
701 assert_response :success, "can't read back the relation: #{@response.body}"
702 check_ordering(doc, @response.body)
704 # check the ordering in the history tables:
705 with_controller(OldRelationController.new) do
706 get :version, :id => relation_id, :version => 1
707 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
708 check_ordering(doc, @response.body)
712 # ============================================================
714 # ============================================================
717 # checks that the XML document and the string arguments have
718 # members in the same order.
719 def check_ordering(doc, xml)
720 new_doc = XML::Parser.string(xml).parse
722 doc_members = doc.find("//osm/relation/member").collect do |m|
723 [m['ref'].to_i, m['type'].to_sym, m['role']]
726 new_members = new_doc.find("//osm/relation/member").collect do |m|
727 [m['ref'].to_i, m['type'].to_sym, m['role']]
730 doc_members.zip(new_members).each do |d, n|
731 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
736 # create a changeset and yield to the caller to set it up, then assert
737 # that the changeset bounding box is +bbox+.
738 def check_changeset_modify(bbox)
739 ## First test with the private user to check that you get a forbidden
740 basic_authorization(users(:normal_user).email, "test");
742 # create a new changeset for this operation, so we are assured
743 # that the bounding box will be newly-generated.
744 changeset_id = with_controller(ChangesetController.new) do
745 content "<osm><changeset/></osm>"
747 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
751 ## Now do the whole thing with the public user
752 basic_authorization(users(:public_user).email, "test")
754 # create a new changeset for this operation, so we are assured
755 # that the bounding box will be newly-generated.
756 changeset_id = with_controller(ChangesetController.new) do
757 content "<osm><changeset/></osm>"
759 assert_response :success, "couldn't create changeset for modify test"
763 # go back to the block to do the actual modifies
766 # now download the changeset to check its bounding box
767 with_controller(ChangesetController.new) do
768 get :read, :id => changeset_id
769 assert_response :success, "can't re-read changeset for modify test"
770 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
771 assert_select "osm>changeset[id=#{changeset_id}]", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
772 assert_select "osm>changeset[min_lon=#{bbox.min_lon}]", 1, "Changeset min_lon wrong in #{@response.body}"
773 assert_select "osm>changeset[min_lat=#{bbox.min_lat}]", 1, "Changeset min_lat wrong in #{@response.body}"
774 assert_select "osm>changeset[max_lon=#{bbox.max_lon}]", 1, "Changeset max_lon wrong in #{@response.body}"
775 assert_select "osm>changeset[max_lat=#{bbox.max_lat}]", 1, "Changeset max_lat wrong in #{@response.body}"
780 # yields the relation with the given +id+ (and optional +version+
781 # to read from the history tables) into the block. the parsed XML
783 def with_relation(id, ver = nil)
787 with_controller(OldRelationController.new) do
788 get :version, :id => id, :version => ver
791 assert_response :success
792 yield xml_parse(@response.body)
796 # updates the relation (XML) +rel+ and
797 # yields the new version of that relation into the block.
798 # the parsed XML doc is retured.
800 rel_id = rel.find("//osm/relation").first["id"].to_i
802 put :update, :id => rel_id
803 assert_response :success, "can't update relation: #{@response.body}"
804 version = @response.body.to_i
806 # now get the new version
807 get :read, :id => rel_id
808 assert_response :success
809 new_rel = xml_parse(@response.body)
817 # updates the relation (XML) +rel+ via the diff-upload API and
818 # yields the new version of that relation into the block.
819 # the parsed XML doc is retured.
820 def with_update_diff(rel)
821 rel_id = rel.find("//osm/relation").first["id"].to_i
822 cs_id = rel.find("//osm/relation").first['changeset'].to_i
825 with_controller(ChangesetController.new) do
826 doc = OSM::API.new.get_xml_doc
827 change = XML::Node.new 'osmChange'
829 modify = XML::Node.new 'modify'
831 modify << doc.import(rel.find("//osm/relation").first)
834 post :upload, :id => cs_id
835 assert_response :success, "can't upload diff relation: #{@response.body}"
836 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
839 # now get the new version
840 get :read, :id => rel_id
841 assert_response :success
842 new_rel = xml_parse(@response.body)
850 # returns a k->v hash of tags from an xml doc
851 def get_tags_as_hash(a)
852 a.find("//osm/relation/tag").inject({}) do |h,v|
859 # assert that all tags on relation documents +a+ and +b+
861 def assert_tags_equal(a, b)
862 # turn the XML doc into tags hashes
863 a_tags = get_tags_as_hash(a)
864 b_tags = get_tags_as_hash(b)
866 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
867 a_tags.each do |k, v|
868 assert_equal v, b_tags[k],
869 "Tags which were not altered should be the same. " +
870 "#{a_tags.inspect} != #{b_tags.inspect}"
875 # update the changeset_id of a node element
876 def update_changeset(xml, changeset_id)
877 xml_attr_rewrite(xml, 'changeset', changeset_id)
881 # update an attribute in the node element
882 def xml_attr_rewrite(xml, name, value)
883 xml.find("//osm/relation").first[name] = value.to_s
890 parser = XML::Parser.string(xml)