]> git.openstreetmap.org Git - rails.git/commitdiff
Validations on the way tags, with unit tests
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Tue, 18 Nov 2008 16:21:32 +0000 (16:21 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Tue, 18 Nov 2008 16:21:32 +0000 (16:21 +0000)
app/models/node_tag.rb
app/models/old_node_tag.rb
app/models/old_way_tag.rb
app/models/way_tag.rb
test/unit/node_tag_test.rb
test/unit/old_way_tag_test.rb [new file with mode: 0644]
test/unit/way_tag_test.rb

index de3d0629c4d29ce5769aa8f7ede0c65f020a9758..6b21e4b01422a6e18c017afc18730d136fd3e68f 100644 (file)
@@ -4,5 +4,5 @@ class NodeTag < ActiveRecord::Base
   belongs_to :node, :foreign_key => 'id'
   
   validates_presence_of :id
   belongs_to :node, :foreign_key => 'id'
   
   validates_presence_of :id
-  validates_length_of :k, :v, :within => 0..255, :allow_blank => true
+  validates_length_of :k, :v, :maximum => 255, :allow_blank => true
 end
 end
index ef32a0613f3a9aa08df97d9b13d809c4a6f509a5..392e3934ed4ada476794b61ddbd0e0a31cea7208 100644 (file)
@@ -4,6 +4,6 @@ class OldNodeTag < ActiveRecord::Base
   set_table_name 'node_tags'
 
   validates_presence_of :id, :version
   set_table_name 'node_tags'
 
   validates_presence_of :id, :version
-  validates_length_of :k, :v, :within => 0..255, :allow_blank => true
+  validates_length_of :k, :v, :maximum => 255, :allow_blank => true
 
 end
 
 end
index b02fd45b93ad8f23ba3aea7271bcef1ddfce5051..547fd177cabc263ab99994ec526ef5c84090544a 100644 (file)
@@ -3,4 +3,8 @@ class OldWayTag < ActiveRecord::Base
 
   set_table_name 'way_tags'
 
 
   set_table_name 'way_tags'
 
+  validates_presence_of :id
+  validates_length_of :k, :v, :maximum => 255, :allow_blank => true
+  validates_uniqueness_of :id, :scope => [:k, :version]
+  validates_numericality_of :id, :version, :only_integer => true
 end
 end
index 4548674d4b950607e2396ecc079da2e3951bba4c..fa9b4336177061287d00d4b153a9da6cef28c6a9 100644 (file)
@@ -6,4 +6,9 @@ class WayTag < ActiveRecord::Base
   # FIXME add a real multipart key to waytags so that we can do eager loadin
 
   belongs_to :way, :foreign_key => 'id'
   # FIXME add a real multipart key to waytags so that we can do eager loadin
 
   belongs_to :way, :foreign_key => 'id'
+  
+  validates_presence_of :id
+  validates_length_of :k, :v, :maximum => 255, :allow_blank => true
+  validates_uniqueness_of :id, :scope => :k
+  validates_numericality_of :id, :only_integer => true
 end
 end
index fe6ebb166f2e949eade96120d3aa4a0a6fef43b7..9d3da03b1ea40ffb9b6d97ce6e6fd05605732fdf 100644 (file)
@@ -46,8 +46,8 @@ class NodeTagTest < Test::Unit::TestCase
       tag = NodeTag.new
       tag.id = current_node_tags(:t1).id
       tag.k = i
       tag = NodeTag.new
       tag.id = current_node_tags(:t1).id
       tag.k = i
-      tag.v = "v", "Key should be too long"
-      assert !tag.valid?
+      tag.v = "v"
+      assert !tag.valid?, "Key should be too long"
       assert tag.errors.invalid?(:k)
     end
   end
       assert tag.errors.invalid?(:k)
     end
   end
diff --git a/test/unit/old_way_tag_test.rb b/test/unit/old_way_tag_test.rb
new file mode 100644 (file)
index 0000000..8210ef0
--- /dev/null
@@ -0,0 +1,76 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class WayTagTest < Test::Unit::TestCase
+  fixtures :way_tags
+  set_fixture_class :way_tags => OldWayTag
+  
+  def test_tag_count
+    assert_equal 3, OldWayTag.count
+  end
+  
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = OldWayTag.new
+      tag.id = way_tags(:t1).id
+      tag.version = 1
+      tag.k = key*i
+      tag.v = "v"
+      assert_valid tag
+    end
+  end
+  
+  def test_length_value_valid
+    val = "v"
+    (0..255).each do |i|
+      tag = OldWayTag.new
+      tag.id = way_tags(:t1).id
+      tag.version = 1
+      tag.k = "k"
+      tag.v = val*i
+      assert_valid tag
+    end
+  end
+  
+  def test_length_key_invalid
+    ["k"*256].each do |i|
+      tag = OldWayTag.new
+      tag.id = way_tags(:t1).id
+      tag.version = 1
+      tag.k = i
+      tag.v = "v"
+      assert !tag.valid?, "Key should be too long"
+      assert tag.errors.invalid?(:k)
+    end
+  end
+  
+  def test_length_value_invalid
+    ["k"*256].each do |i|
+      tag = OldWayTag.new
+      tag.id = way_tags(:t1).id
+      tag.version = 1
+      tag.k = "k"
+      tag.v = i
+      assert !tag.valid?, "Value should be too long"
+      assert tag.errors.invalid?(:v)
+    end
+  end
+  
+  def test_empty_node_tag_invalid
+    tag = OldNodeTag.new
+    assert !tag.valid?, "Empty tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
+  
+  def test_uniqueness
+    tag = OldWayTag.new
+    tag.id = way_tags(:t1).id
+    tag.version = way_tags(:t1).version
+    tag.k = way_tags(:t1).k
+    tag.v = way_tags(:t1).v
+    assert tag.new_record?
+    assert !tag.valid?
+    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
+    assert tag.new_record?
+  end
+end
index cd296749e9abf3d154895be8a5aac21a037157ef..44b96ffe3a7951ff6b8e9386a77ee61621e336d3 100644 (file)
@@ -8,4 +8,63 @@ class WayTagTest < Test::Unit::TestCase
     assert_equal 3, WayTag.count
   end
   
     assert_equal 3, WayTag.count
   end
   
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = WayTag.new
+      tag.id = current_way_tags(:t1).id
+      tag.k = key*i
+      tag.v = current_way_tags(:t1).v
+      assert_valid tag
+    end
+  end
+  
+  def test_length_value_valid
+    val = "v"
+    (0..255).each do |i|
+      tag = WayTag.new
+      tag.id = current_way_tags(:t1).id
+      tag.k = "k"
+      tag.v = val*i
+      assert_valid tag
+    end
+  end
+  
+  def test_length_key_invalid
+    ["k"*256].each do |i|
+      tag = WayTag.new
+      tag.id = current_way_tags(:t1).id
+      tag.k = i
+      tag.v = "v"
+      assert !tag.valid?, "Key #{i} should be too long"
+      assert tag.errors.invalid?(:k)
+    end
+  end
+  
+  def test_length_value_invalid
+    ["k"*256].each do |i|
+      tag = WayTag.new
+      tag.id = current_way_tags(:t1).id
+      tag.k = "k"
+      tag.v = i
+      assert !tag.valid?, "Value #{i} should be too long"
+    end
+  end
+  
+  def test_empty_tag_invalid
+    tag = WayTag.new
+    assert !tag.valid?, "Empty way tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
+  
+  def test_uniquess
+    tag = WayTag.new
+    tag.id = current_way_tags(:t1).id
+    tag.k = current_way_tags(:t1).k
+    tag.v = current_way_tags(:t1).v
+    assert tag.new_record?
+    assert !tag.valid?
+    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
+    assert tag.new_record?
+  end
 end
 end