From 65eb6af30377bb98a0da691f69d3ad9b68b831e2 Mon Sep 17 00:00:00 2001 From: Shaun McDonald Date: Thu, 11 Jun 2009 11:08:37 +0000 Subject: [PATCH 1/1] Give a nice error message when parsing the nwr tags and they are missing the k and/or v. Also includes tests. --- app/models/node.rb | 2 ++ app/models/relation.rb | 2 ++ app/models/way.rb | 2 ++ test/unit/node_test.rb | 36 ++++++++++++++++++++++++++++++++++++ test/unit/relation_test.rb | 20 ++++++++++++++++++-- test/unit/way_test.rb | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index eeb6e6da5..dd8d96d12 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -105,6 +105,8 @@ class Node < ActiveRecord::Base node.visible = true pt.find('tag').each do |tag| + raise OSM::APIBadXMLError.new("node", pt, "tag is missing key") if tag['k'].nil? + raise OSM::APIBadXMLError.new("node", pt, "tag is missing value") if tag['v'].nil? node.add_tag_key_val(tag['k'],tag['v']) end diff --git a/app/models/relation.rb b/app/models/relation.rb index 28fa326ba..76cd86729 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -60,6 +60,8 @@ class Relation < ActiveRecord::Base relation.visible = true pt.find('tag').each do |tag| + raise OSM::APIBadXMLError.new("relation", pt, "tag is missing key") if tag['k'].nil? + raise OSM::APIBadXMLError.new("relation", pt, "tag is missing value") if tag['v'].nil? relation.add_tag_keyval(tag['k'], tag['v']) end diff --git a/app/models/way.rb b/app/models/way.rb index f9d36be93..8788bd671 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -61,6 +61,8 @@ class Way < ActiveRecord::Base way.visible = true pt.find('tag').each do |tag| + raise OSM::APIBadXMLError.new("way", pt, "tag is missing key") if tag['k'].nil? + raise OSM::APIBadXMLError.new("way", pt, "tag is missing value") if tag['v'].nil? way.add_tag_keyval(tag['k'], tag['v']) end diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 8e71233e6..dd907f0f4 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -269,4 +269,40 @@ class NodeTest < ActiveSupport::TestCase } assert_match /Must specify a string with one or more characters/, message_update.message end + + def test_from_xml_no_k_v + nokv = "" + message_create = assert_raise(OSM::APIBadXMLError) { + Node.from_xml(nokv, true) + } + assert_match /tag is missing key/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { + Node.from_xml(nokv, false) + } + assert_match /tag is missing key/, message_update.message + end + + def test_from_xml_no_v + no_v = "" + message_create = assert_raise(OSM::APIBadXMLError) { + Node.from_xml(no_v, true) + } + assert_match /tag is missing value/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { + Node.from_xml(no_v, false) + } + assert_match /tag is missing value/, message_update.message + end + + def test_from_xml_duplicate_k + dupk = "" + message_create = assert_raise(OSM::APIDuplicateTagsError) { + Node.from_xml(dupk, true) + } + assert_equal "Element node/ has duplicate tags with key dup", message_create.message + message_update = assert_raise(OSM::APIDuplicateTagsError) { + Node.from_xml(dupk, false) + } + assert_equal "Element node/23 has duplicate tags with key dup", message_update.message + end end diff --git a/test/unit/relation_test.rb b/test/unit/relation_test.rb index a878be8cb..b1fbc0fcd 100644 --- a/test/unit/relation_test.rb +++ b/test/unit/relation_test.rb @@ -69,14 +69,30 @@ class RelationTest < ActiveSupport::TestCase def test_from_xml_no_k_v nokv = "" - assert_nothing_raised(OSM::APIBadUserInput, OSM::APIBadXMLError) { + message_create = assert_raise(OSM::APIBadXMLError) { Relation.from_xml(nokv, true) + } + assert_match /tag is missing key/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { Relation.from_xml(nokv, false) } + assert_match /tag is missing key/, message_update.message + end + + def test_from_xml_no_v + no_v = "" + message_create = assert_raise(OSM::APIBadXMLError) { + Relation.from_xml(no_v, true) + } + assert_match /tag is missing value/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { + Relation.from_xml(no_v, false) + } + assert_match /tag is missing value/, message_update.message end def test_from_xml_duplicate_k - dupk = "" + dupk = "" message_create = assert_raise(OSM::APIDuplicateTagsError) { Relation.from_xml(dupk, true) } diff --git a/test/unit/way_test.rb b/test/unit/way_test.rb index bdf4ab365..c13c9755f 100644 --- a/test/unit/way_test.rb +++ b/test/unit/way_test.rb @@ -96,4 +96,40 @@ class WayTest < ActiveSupport::TestCase } assert_match /Must specify a string with one or more characters/, message_update.message end + + def test_from_xml_no_k_v + nokv = "" + message_create = assert_raise(OSM::APIBadXMLError) { + Way.from_xml(nokv, true) + } + assert_match /tag is missing key/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { + Way.from_xml(nokv, false) + } + assert_match /tag is missing key/, message_update.message + end + + def test_from_xml_no_v + no_v = "" + message_create = assert_raise(OSM::APIBadXMLError) { + Way.from_xml(no_v, true) + } + assert_match /tag is missing value/, message_create.message + message_update = assert_raise(OSM::APIBadXMLError) { + Way.from_xml(no_v, false) + } + assert_match /tag is missing value/, message_update.message + end + + def test_from_xml_duplicate_k + dupk = "" + message_create = assert_raise(OSM::APIDuplicateTagsError) { + Way.from_xml(dupk, true) + } + assert_equal "Element way/ has duplicate tags with key dup", message_create.message + message_update = assert_raise(OSM::APIDuplicateTagsError) { + Way.from_xml(dupk, false) + } + assert_equal "Element way/23 has duplicate tags with key dup", message_update.message + end end -- 2.39.5