3 class NodeTest < ActiveSupport::TestCase
 
   4   def test_node_too_far_north
 
   5     node = build(:node, :latitude => 90.01 * OldNode::SCALE)
 
   7     assert_includes node.errors.full_messages, "Node is not in the world"
 
  10   def test_node_north_limit
 
  11     node = build(:node, :latitude => 90 * OldNode::SCALE)
 
  13     assert_not_includes node.errors.full_messages, "Node is not in the world"
 
  16   def test_node_too_far_south
 
  17     node = build(:node, :latitude => -90.01 * OldNode::SCALE)
 
  19     assert_includes node.errors.full_messages, "Node is not in the world"
 
  22   def test_node_south_limit
 
  23     node = build(:node, :latitude => -90 * OldNode::SCALE)
 
  25     assert_not_includes node.errors.full_messages, "Node is not in the world"
 
  28   def test_node_too_far_west
 
  29     node = build(:node, :longitude => -180.01 * OldNode::SCALE)
 
  31     assert_includes node.errors.full_messages, "Node is not in the world"
 
  34   def test_node_west_limit
 
  35     node = build(:node, :longitude => -180 * OldNode::SCALE)
 
  37     assert_not_includes node.errors.full_messages, "Node is not in the world"
 
  40   def test_node_too_far_east
 
  41     node = build(:node, :longitude => 180.01 * OldNode::SCALE)
 
  43     assert_includes node.errors.full_messages, "Node is not in the world"
 
  46   def test_node_east_limit
 
  47     node = build(:node, :longitude => 180 * OldNode::SCALE)
 
  49     assert_not_includes node.errors.full_messages, "Node is not in the world"
 
  52   def test_totally_wrong
 
  53     node = build(:node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
 
  55     assert_includes node.errors.full_messages, "Node is not in the world"
 
  59     node = build(:node, :latitude => 12.345 * OldNode::SCALE, :longitude => 34.567 * OldNode::SCALE)
 
  61     assert_in_delta 12.345, node.lat, 0.0000001
 
  62     assert_in_delta 34.567, node.lon, 0.0000001
 
  67     assert_in_delta 54.321 * OldNode::SCALE, node.latitude, 0.000001
 
  68     assert_in_delta 76.543 * OldNode::SCALE, node.longitude, 0.000001
 
  71   # Check that you can create a node and store it
 
  73     changeset = create(:changeset)
 
  74     node_template = build(:node, :lat => 12.3456,
 
  76                                  :changeset_id => changeset.id,
 
  79     assert node_template.create_with_history(changeset.user)
 
  81     node = Node.find(node_template.id)
 
  83     assert_equal node_template.latitude, node.latitude
 
  84     assert_equal node_template.longitude, node.longitude
 
  85     assert_equal node_template.changeset_id, node.changeset_id
 
  86     assert_equal node_template.visible, node.visible
 
  87     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
 
  89     assert_equal(1, OldNode.where(:node_id => node_template.id).count)
 
  90     old_node = OldNode.find_by(:node_id => node_template.id, :version => 1)
 
  91     assert_not_nil old_node
 
  92     assert_equal node_template.latitude, old_node.latitude
 
  93     assert_equal node_template.longitude, old_node.longitude
 
  94     assert_equal node_template.changeset_id, old_node.changeset_id
 
  95     assert_equal node_template.visible, old_node.visible
 
  96     assert_equal node_template.tags, old_node.tags
 
  97     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
 
 101     node = create(:node, :lat => 12.6543, :lon => 65.1234)
 
 102     create(:old_node, :node_id => node.id, :version => 1, :lat => node.lat, :lon => node.lon)
 
 104     node_template = Node.find(node.id)
 
 106     assert_not_nil node_template
 
 107     assert_equal(1, OldNode.where(:node_id => node_template.id).count)
 
 110     node_template.lat = 12.3456
 
 111     node_template.lon = 65.4321
 
 112     # node_template.tags = "updated=yes"
 
 113     assert node.update_from(node_template, node.changeset.user)
 
 115     node = Node.find(node_template.id)
 
 117     assert_equal node_template.latitude, node.latitude
 
 118     assert_equal node_template.longitude, node.longitude
 
 119     assert_equal node_template.changeset_id, node.changeset_id
 
 120     assert_equal node_template.visible, node.visible
 
 121     # assert_equal node_template.tags, node.tags
 
 123     assert_equal(2, OldNode.where(:node_id => node_template.id).count)
 
 124     old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
 
 125     assert_not_nil old_node
 
 126     assert_equal node_template.latitude, old_node.latitude
 
 127     assert_equal node_template.longitude, old_node.longitude
 
 128     assert_equal node_template.changeset_id, old_node.changeset_id
 
 129     assert_equal node_template.visible, old_node.visible
 
 130     # assert_equal node_template.tags, old_node.tags
 
 135     create(:old_node, :node_id => node.id, :version => 1)
 
 136     node_template = Node.find(node.id)
 
 138     assert_not_nil node_template
 
 139     assert_equal(1, OldNode.where(:node_id => node_template.id).count)
 
 142     assert node.delete_with_history!(node_template, node.changeset.user)
 
 144     node = Node.find(node_template.id)
 
 146     assert_equal node_template.latitude, node.latitude
 
 147     assert_equal node_template.longitude, node.longitude
 
 148     assert_equal node_template.changeset_id, node.changeset_id
 
 149     assert_not node.visible
 
 150     # assert_equal node_template.tags, node.tags
 
 152     assert_equal(2, OldNode.where(:node_id => node_template.id).count)
 
 153     old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
 
 154     assert_not_nil old_node
 
 155     assert_equal node_template.latitude, old_node.latitude
 
 156     assert_equal node_template.longitude, old_node.longitude
 
 157     assert_equal node_template.changeset_id, old_node.changeset_id
 
 158     assert_not old_node.visible
 
 159     # assert_equal node_template.tags, old_node.tags
 
 162   def test_from_xml_no_id
 
 167     noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
 
 168     # First try a create which doesn't need the id
 
 169     assert_nothing_raised do
 
 170       Node.from_xml(noid, :create => true)
 
 172     # Now try an update with no id, and make sure that it gives the appropriate exception
 
 173     message = assert_raise(OSM::APIBadXMLError) do
 
 174       Node.from_xml(noid, :create => false)
 
 176     assert_match(/ID is required when updating./, message.message)
 
 179   def test_from_xml_no_lat
 
 180     nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
 
 181     message_create = assert_raise(OSM::APIBadXMLError) do
 
 182       Node.from_xml(nolat, :create => true)
 
 184     assert_match(/lat missing/, message_create.message)
 
 185     message_update = assert_raise(OSM::APIBadXMLError) do
 
 186       Node.from_xml(nolat, :create => false)
 
 188     assert_match(/lat missing/, message_update.message)
 
 191   def test_from_xml_no_lon
 
 192     nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
 
 193     message_create = assert_raise(OSM::APIBadXMLError) do
 
 194       Node.from_xml(nolon, :create => true)
 
 196     assert_match(/lon missing/, message_create.message)
 
 197     message_update = assert_raise(OSM::APIBadXMLError) do
 
 198       Node.from_xml(nolon, :create => false)
 
 200     assert_match(/lon missing/, message_update.message)
 
 203   def test_from_xml_no_changeset_id
 
 204     nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
 
 205     message_create = assert_raise(OSM::APIBadXMLError) do
 
 206       Node.from_xml(nocs, :create => true)
 
 208     assert_match(/Changeset id is missing/, message_create.message)
 
 209     message_update = assert_raise(OSM::APIBadXMLError) do
 
 210       Node.from_xml(nocs, :create => false)
 
 212     assert_match(/Changeset id is missing/, message_update.message)
 
 215   def test_from_xml_no_version
 
 216     no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
 
 217     assert_nothing_raised do
 
 218       Node.from_xml(no_version, :create => true)
 
 220     message_update = assert_raise(OSM::APIBadXMLError) do
 
 221       Node.from_xml(no_version, :create => false)
 
 223     assert_match(/Version is required when updating/, message_update.message)
 
 226   def test_from_xml_double_lat
 
 227     nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
 
 228     message_create = assert_raise(OSM::APIBadXMLError) do
 
 229       Node.from_xml(nocs, :create => true)
 
 231     assert_match(/Fatal error: Attribute lat redefined at/, message_create.message)
 
 232     message_update = assert_raise(OSM::APIBadXMLError) do
 
 233       Node.from_xml(nocs, :create => false)
 
 235     assert_match(/Fatal error: Attribute lat redefined at/, message_update.message)
 
 238   def test_from_xml_id_zero
 
 239     id_list = ["", "0", "00", "0.0", "a"]
 
 241       zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
 
 242       assert_nothing_raised do
 
 243         Node.from_xml(zero_id, :create => true)
 
 245       message_update = assert_raise(OSM::APIBadUserInput) do
 
 246         Node.from_xml(zero_id, :create => false)
 
 248       assert_match(/ID of node cannot be zero when updating/, message_update.message)
 
 252   def test_from_xml_no_text
 
 254     message_create = assert_raise(OSM::APIBadXMLError) do
 
 255       Node.from_xml(no_text, :create => true)
 
 257     assert_match(/Must specify a string with one or more characters/, message_create.message)
 
 258     message_update = assert_raise(OSM::APIBadXMLError) do
 
 259       Node.from_xml(no_text, :create => false)
 
 261     assert_match(/Must specify a string with one or more characters/, message_update.message)
 
 264   def test_from_xml_no_node
 
 265     no_node = "<osm></osm>"
 
 266     message_create = assert_raise(OSM::APIBadXMLError) do
 
 267       Node.from_xml(no_node, :create => true)
 
 269     assert_match %r{XML doesn't contain an osm/node element}, message_create.message
 
 270     message_update = assert_raise(OSM::APIBadXMLError) do
 
 271       Node.from_xml(no_node, :create => false)
 
 273     assert_match %r{XML doesn't contain an osm/node element}, message_update.message
 
 276   def test_from_xml_no_k_v
 
 277     nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
 
 278     message_create = assert_raise(OSM::APIBadXMLError) do
 
 279       Node.from_xml(nokv, :create => true)
 
 281     assert_match(/tag is missing key/, message_create.message)
 
 282     message_update = assert_raise(OSM::APIBadXMLError) do
 
 283       Node.from_xml(nokv, :create => false)
 
 285     assert_match(/tag is missing key/, message_update.message)
 
 288   def test_from_xml_no_v
 
 289     no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
 
 290     message_create = assert_raise(OSM::APIBadXMLError) do
 
 291       Node.from_xml(no_v, :create => true)
 
 293     assert_match(/tag is missing value/, message_create.message)
 
 294     message_update = assert_raise(OSM::APIBadXMLError) do
 
 295       Node.from_xml(no_v, :create => false)
 
 297     assert_match(/tag is missing value/, message_update.message)
 
 300   def test_from_xml_duplicate_k
 
 301     dupk = "<osm><node id='23' lat='23.2' lon='23' changeset='34' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></node></osm>"
 
 302     message_create = assert_raise(OSM::APIDuplicateTagsError) do
 
 303       Node.from_xml(dupk, :create => true)
 
 305     assert_equal "Element node/ has duplicate tags with key dup", message_create.message
 
 306     message_update = assert_raise(OSM::APIDuplicateTagsError) do
 
 307       Node.from_xml(dupk, :create => false)
 
 309     assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
 
 314     taglist = create_list(:node_tag, 2, :node => node)
 
 315     tags = Node.find(node.id).node_tags.order(:k)
 
 316     assert_equal taglist.count, tags.count
 
 317     taglist.sort_by!(&:k).each_index do |i|
 
 318       assert_equal taglist[i].k, tags[i].k
 
 319       assert_equal taglist[i].v, tags[i].v
 
 325     taglist = create_list(:node_tag, 2, :node => node)
 
 326     tags = Node.find(node.id).tags
 
 327     assert_equal taglist.count, tags.count
 
 328     taglist.each do |tag|
 
 329       assert_equal tag.v, tags[tag.k]
 
 333   def test_containing_relation_members
 
 335     relation_member1 = create(:relation_member, :member => node)
 
 336     relation_member2 = create(:relation_member, :member => node)
 
 337     relation_member3 = create(:relation_member, :member => node)
 
 338     crm = Node.find(node.id).containing_relation_members.order(:relation_id)
 
 339     #    assert_equal 3, crm.size
 
 340     assert_equal relation_member1.relation_id, crm.first.relation_id
 
 341     assert_equal "Node", crm.first.member_type
 
 342     assert_equal node.id, crm.first.member_id
 
 343     assert_equal relation_member1.relation_id, crm.first.relation.id
 
 344     assert_equal relation_member2.relation_id, crm.second.relation_id
 
 345     assert_equal "Node", crm.second.member_type
 
 346     assert_equal node.id, crm.second.member_id
 
 347     assert_equal relation_member2.relation_id, crm.second.relation.id
 
 348     assert_equal relation_member3.relation_id, crm.third.relation_id
 
 349     assert_equal "Node", crm.third.member_type
 
 350     assert_equal node.id, crm.third.member_id
 
 351     assert_equal relation_member3.relation_id, crm.third.relation.id
 
 354   def test_containing_relations
 
 356     relation_member1 = create(:relation_member, :member => node)
 
 357     relation_member2 = create(:relation_member, :member => node)
 
 358     relation_member3 = create(:relation_member, :member => node)
 
 359     cr = Node.find(node.id).containing_relations.order(:id)
 
 361     assert_equal 3, cr.size
 
 362     assert_equal relation_member1.relation.id, cr.first.id
 
 363     assert_equal relation_member2.relation.id, cr.second.id
 
 364     assert_equal relation_member3.relation.id, cr.third.id
 
 367   test "raises missing changeset exception when creating" do
 
 370     assert_raises OSM::APIChangesetMissingError do
 
 371       node.create_with_history(user)
 
 375   test "raises user-changeset mismatch exception when creating" do
 
 377     changeset = create(:changeset)
 
 378     node = Node.new(:changeset => changeset)
 
 379     assert_raises OSM::APIUserChangesetMismatchError do
 
 380       node.create_with_history(user)
 
 384   test "raises already closed changeset exception when creating" do
 
 386     changeset = create(:changeset, :closed, :user => user)
 
 387     node = Node.new(:changeset => changeset)
 
 388     assert_raises OSM::APIChangesetAlreadyClosedError do
 
 389       node.create_with_history(user)
 
 393   test "raises id precondition exception when updating" do
 
 395     node = Node.new(:id => 23)
 
 396     new_node = Node.new(:id => 42)
 
 397     assert_raises OSM::APIPreconditionFailedError do
 
 398       node.update_from(new_node, user)
 
 402   test "raises version mismatch exception when updating" do
 
 404     node = Node.new(:id => 42, :version => 7)
 
 405     new_node = Node.new(:id => 42, :version => 12)
 
 406     assert_raises OSM::APIVersionMismatchError do
 
 407       node.update_from(new_node, user)
 
 411   test "raises missing changeset exception when updating" do
 
 413     node = Node.new(:id => 42, :version => 12)
 
 414     new_node = Node.new(:id => 42, :version => 12)
 
 415     assert_raises OSM::APIChangesetMissingError do
 
 416       node.update_from(new_node, user)
 
 420   test "raises user-changeset mismatch exception when updating" do
 
 422     changeset = create(:changeset)
 
 423     node = Node.new(:id => 42, :version => 12)
 
 424     new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
 
 425     assert_raises OSM::APIUserChangesetMismatchError do
 
 426       node.update_from(new_node, user)
 
 430   test "raises already closed changeset exception when updating" do
 
 432     changeset = create(:changeset, :closed, :user => user)
 
 433     node = Node.new(:id => 42, :version => 12)
 
 434     new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
 
 435     assert_raises OSM::APIChangesetAlreadyClosedError do
 
 436       node.update_from(new_node, user)
 
 440   test "raises id precondition exception when deleting" do
 
 442     node = Node.new(:id => 23, :visible => true)
 
 443     new_node = Node.new(:id => 42, :visible => false)
 
 444     assert_raises OSM::APIPreconditionFailedError do
 
 445       node.delete_with_history!(new_node, user)