]> git.openstreetmap.org Git - rails.git/blob - test/unit/node_test.rb
node unit tests with inside limits now.
[rails.git] / test / unit / node_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class NodeTest < Test::Unit::TestCase
4   fixtures :current_nodes, :users, :current_node_tags,:nodes,  :node_tags
5   set_fixture_class :current_nodes => :Node
6   set_fixture_class :nodes => :OldNode
7   set_fixture_class :node_tags => :OldNodeTag
8   set_fixture_class :currenr_node_tags => :NodeTag
9     
10   def test_node_too_far_north
11           invalid_node_test(:node_too_far_north)
12   end
13   
14   def test_node_north_limit
15     valid_node_test(:node_north_limit)
16   end
17   
18   def test_node_too_far_south
19     invalid_node_test(:node_too_far_south)
20   end
21   
22   def test_node_south_limit
23     valid_node_test(:node_south_limit)
24   end
25   
26   def test_node_too_far_west
27     invalid_node_test(:node_too_far_west)
28   end
29   
30   def test_node_west_limit
31     valid_node_test(:node_west_limit)
32   end
33   
34   def test_node_too_far_east
35     invalid_node_test(:node_too_far_east)
36   end
37   
38   def test_node_east_limit
39     valid_node_test(:node_east_limit)
40   end
41   
42   def test_totally_wrong
43     invalid_node_test(:node_totally_wrong)
44   end
45   
46   # This helper method will check to make sure that a node is within the world, and
47   # has the the same lat, lon and timestamp than what was put into the db by 
48   # the fixture
49   def valid_node_test(nod)
50     node = current_nodes(nod)
51     assert_equal node.lat, current_nodes(nod).latitude.to_f/SCALE
52     assert_equal node.lon, current_nodes(nod).longitude.to_f/SCALE
53     assert_equal node.timestamp, current_nodes(nod).timestamp
54     assert_valid node
55   end
56   
57   # This helpermethod will check to make sure that a node is outwith the world, 
58   # and has the same lat, lon and timesamp than what was put into the db by the
59   # fixture
60   def invalid_node_test(nod)
61     node = current_nodes(nod)
62     assert_equal node.lat, current_nodes(nod).latitude.to_f/SCALE
63     assert_equal node.lon, current_nodes(nod).longitude.to_f/SCALE
64     assert_equal node.timestamp, current_nodes(nod).timestamp
65     assert_equal false, node.valid?
66   end
67   
68   # Check that you can create a node and store it
69   def test_create
70     node_template = Node.new(:latitude => 12.3456,
71                              :longitude => 65.4321,
72                              :user_id => users(:normal_user),
73                              :visible => 1, 
74                              :version => 1)
75     assert node_template.save_with_history!
76
77     node = Node.find(node_template.id)
78     assert_not_nil node
79     assert_equal node_template.latitude, node.latitude
80     assert_equal node_template.longitude, node.longitude
81     assert_equal node_template.user_id, node.user_id
82     assert_equal node_template.visible, node.visible
83     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
84
85     assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
86     old_node = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
87     assert_not_nil old_node
88     assert_equal node_template.latitude, old_node.latitude
89     assert_equal node_template.longitude, old_node.longitude
90     assert_equal node_template.user_id, old_node.user_id
91     assert_equal node_template.visible, old_node.visible
92     assert_equal node_template.tags, old_node.tags
93     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
94   end
95
96   def test_update
97     node_template = Node.find(current_nodes(:visible_node).id)
98     assert_not_nil node_template
99
100     assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
101     old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
102     assert_not_nil old_node_template
103
104     node_template.latitude = 12.3456
105     node_template.longitude = 65.4321
106     #node_template.tags = "updated=yes"
107     assert node_template.save_with_history!
108
109     node = Node.find(node_template.id)
110     assert_not_nil node
111     assert_equal node_template.latitude, node.latitude
112     assert_equal node_template.longitude, node.longitude
113     assert_equal node_template.user_id, node.user_id
114     assert_equal node_template.visible, node.visible
115     #assert_equal node_template.tags, node.tags
116     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
117
118     assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
119     assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
120     old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
121     assert_not_nil old_node
122     assert_equal node_template.latitude, old_node.latitude
123     assert_equal node_template.longitude, old_node.longitude
124     assert_equal node_template.user_id, old_node.user_id
125     assert_equal node_template.visible, old_node.visible
126     #assert_equal node_template.tags, old_node.tags
127     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
128   end
129
130   def test_delete
131     node_template = Node.find(current_nodes(:visible_node))
132     assert_not_nil node_template
133
134     assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
135     old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
136     assert_not_nil old_node_template
137
138     node_template.visible = 0
139     assert node_template.save_with_history!
140
141     node = Node.find(node_template.id)
142     assert_not_nil node
143     assert_equal node_template.latitude, node.latitude
144     assert_equal node_template.longitude, node.longitude
145     assert_equal node_template.user_id, node.user_id
146     assert_equal node_template.visible, node.visible
147     #assert_equal node_template.tags, node.tags
148     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
149
150     assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
151     assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
152     old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
153     assert_not_nil old_node
154     assert_equal node_template.latitude, old_node.latitude
155     assert_equal node_template.longitude, old_node.longitude
156     assert_equal node_template.user_id, old_node.user_id
157     assert_equal node_template.visible, old_node.visible
158     #assert_equal node_template.tags, old_node.tags
159     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
160   end
161 end