]> git.openstreetmap.org Git - rails.git/blob - test/models/old_node_test.rb
12d34473020ea392c5c80906ad45a0147c7abe3a
[rails.git] / test / models / old_node_test.rb
1 require "test_helper"
2
3 class OldNodeTest < ActiveSupport::TestCase
4   def test_node_too_far_north
5     node = build(:old_node, :latitude => 90.01 * OldNode::SCALE)
6     node.validate
7     assert node.errors.full_messages.include?("Node is not in the world")
8   end
9
10   def test_node_north_limit
11     node = build(:old_node, :latitude => 90 * OldNode::SCALE)
12     node.validate
13     assert_equal false, node.errors.full_messages.include?("Node is not in the world")
14   end
15
16   def test_node_too_far_south
17     node = build(:old_node, :latitude => -90.01 * OldNode::SCALE)
18     node.validate
19     assert node.errors.full_messages.include?("Node is not in the world")
20   end
21
22   def test_node_south_limit
23     node = build(:old_node, :latitude => -90 * OldNode::SCALE)
24     node.validate
25     assert_equal false, node.errors.full_messages.include?("Node is not in the world")
26   end
27
28   def test_node_too_far_west
29     node = build(:old_node, :longitude => -180.01 * OldNode::SCALE)
30     node.validate
31     assert node.errors.full_messages.include?("Node is not in the world")
32   end
33
34   def test_node_west_limit
35     node = build(:old_node, :longitude => -180 * OldNode::SCALE)
36     node.validate
37     assert_equal false, node.errors.full_messages.include?("Node is not in the world")
38   end
39
40   def test_node_too_far_east
41     node = build(:old_node, :longitude => 180.01 * OldNode::SCALE)
42     node.validate
43     assert node.errors.full_messages.include?("Node is not in the world")
44   end
45
46   def test_node_east_limit
47     node = build(:old_node, :longitude => 180 * OldNode::SCALE)
48     node.validate
49     assert_equal false, node.errors.full_messages.include?("Node is not in the world")
50   end
51
52   def test_totally_wrong
53     node = build(:old_node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
54     node.validate
55     assert node.errors.full_messages.include?("Node is not in the world")
56   end
57
58   def test_lat_lon
59     node = build(:old_node, :latitude => 12.345 * OldNode::SCALE, :longitude => 34.567 * OldNode::SCALE)
60
61     assert_in_delta 12.345, node.lat, 0.0000001
62     assert_in_delta 34.567, node.lon, 0.0000001
63
64     node.lat = 54.321
65     node.lon = 76.543
66
67     assert_in_delta 54.321 * OldNode::SCALE, node.latitude, 0.000001
68     assert_in_delta 76.543 * OldNode::SCALE, node.longitude, 0.000001
69   end
70
71   # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
72   def test_lat_lon_xml_format
73     old_node = build(:old_node, :latitude => 0.00004 * OldNode::SCALE, :longitude => 0.00008 * OldNode::SCALE)
74
75     assert_match(/lat="0.0000400"/, old_node.to_xml.to_s)
76     assert_match(/lon="0.0000800"/, old_node.to_xml.to_s)
77   end
78
79   def test_node_tags
80     node_v1 = create(:old_node, :version => 1)
81     node_v2 = create(:old_node, :node_id => node_v1.node_id, :version => 2)
82     node_v3 = create(:old_node, :node_id => node_v1.node_id, :version => 3)
83     node_v4 = create(:old_node, :node_id => node_v1.node_id, :version => 4)
84     taglist_v3 = create_list(:old_node_tag, 3, :old_node => node_v3)
85     taglist_v4 = create_list(:old_node_tag, 2, :old_node => node_v4)
86
87     node = node_v1
88     tags = OldNode.find(node.id).old_tags.order(:k)
89     assert_equal 0, tags.count
90
91     node = node_v2
92     tags = OldNode.find(node.id).old_tags.order(:k)
93     assert_equal 0, tags.count
94
95     node = node_v3
96     tags = OldNode.find(node.id).old_tags.order(:k)
97     assert_equal taglist_v3.count, tags.count
98     taglist_v3.sort_by!(&:k).each_index do |i|
99       assert_equal taglist_v3[i].k, tags[i].k
100       assert_equal taglist_v3[i].v, tags[i].v
101     end
102
103     node = node_v4
104     tags = OldNode.find(node.id).old_tags.order(:k)
105     assert_equal taglist_v4.count, tags.count
106     taglist_v4.sort_by!(&:k).each_index do |i|
107       assert_equal taglist_v4[i].k, tags[i].k
108       assert_equal taglist_v4[i].v, tags[i].v
109     end
110   end
111
112   def test_tags
113     node_v1 = create(:old_node, :version => 1)
114     node_v2 = create(:old_node, :node_id => node_v1.node_id, :version => 2)
115     node_v3 = create(:old_node, :node_id => node_v1.node_id, :version => 3)
116     node_v4 = create(:old_node, :node_id => node_v1.node_id, :version => 4)
117     taglist_v3 = create_list(:old_node_tag, 3, :old_node => node_v3)
118     taglist_v4 = create_list(:old_node_tag, 2, :old_node => node_v4)
119
120     node = node_v1
121     tags = OldNode.find(node.id).tags
122     assert_equal 0, tags.size
123
124     node = node_v2
125     tags = OldNode.find(node.id).tags
126     assert_equal 0, tags.size
127
128     node = node_v3
129     tags = OldNode.find(node.id).tags
130     assert_equal taglist_v3.count, tags.count
131     taglist_v3.each do |tag|
132       assert_equal tag.v, tags[tag.k]
133     end
134
135     node = node_v4
136     tags = OldNode.find(node.id).tags
137     assert_equal taglist_v4.count, tags.count
138     taglist_v4.each do |tag|
139       assert_equal tag.v, tags[tag.k]
140     end
141   end
142 end