Changeset tag unit tests
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 19 Nov 2008 18:00:52 +0000 (18:00 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 19 Nov 2008 18:00:52 +0000 (18:00 +0000)
app/models/changeset_tag.rb
test/unit/changeset_tag_test.rb
test/unit/way_tag_test.rb

index 6298fbe778e8da9870c41f3f82976ff24287a6c0..6a414a0fcb8506d2c3118361341354c42e36359f 100644 (file)
@@ -1,5 +1,8 @@
 class ChangesetTag < ActiveRecord::Base
-
   belongs_to :changeset, :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
index 466ee040577fc923b066d961e081c0fdf475574f..e0201d53816c470fd64e27fb2901c9a0d03db566 100644 (file)
@@ -2,10 +2,69 @@ require File.dirname(__FILE__) + '/../test_helper'
 
 class ChangesetTagTest < Test::Unit::TestCase
   fixtures :changeset_tags
+
+  def test_changeset_tag_count
+    assert_equal 1, ChangesetTag.count
+  end
   
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = ChangesetTag.new
+      tag.id = 1
+      tag.k = key*i
+      tag.v = "v"
+      assert_valid tag
+    end
+  end
   
-  def test_changeset_tags_count
-    assert_equal 1, ChangesetTag.count
+  def test_length_value_valid
+    val = "v"
+    (0..255).each do |i|
+      tag = ChangesetTag.new
+      tag.id = 1
+      tag.k = "k"
+      tag.v = val*i
+      assert_valid tag
+    end
+  end
+  
+  def test_length_key_invalid
+    ["k"*256].each do |k|
+      tag = ChangesetTag.new
+      tag.id = 1
+      tag.k = k
+      tag.v = "v"
+      assert !tag.valid?, "Key #{k} should be too long"
+      assert tag.errors.invalid?(:k)
+    end
+  end
+  
+  def test_length_value_invalid
+    ["v"*256].each do |v|
+      tag = ChangesetTag.new
+      tag.id = 1
+      tag.k = "k"
+      tag.v = v
+      assert !tag.valid?, "Value #{v} should be too long"
+      assert tag.errors.invalid?(:v)
+    end
   end
   
+  def test_empty_tag_invalid
+    tag = ChangesetTag.new
+    assert !tag.valid?, "Empty tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
+  
+  def test_uniqueness
+    tag = ChangesetTag.new
+    tag.id = changeset_tags(:changeset_1_tag_1).id
+    tag.k = changeset_tags(:changeset_1_tag_1).k
+    tag.v = changeset_tags(:changeset_1_tag_1).v
+    assert tag.new_record?
+    assert !tag.valid?
+    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
+    assert tag.new_record?
+  end
 end
index fc1c8efa5277fa0f9c2afc3e9604d31855a3b594..b1a7d2256239d2bcb91969f4abd46f387c9f4797 100644 (file)
@@ -48,6 +48,7 @@ class WayTagTest < Test::Unit::TestCase
       tag.k = "k"
       tag.v = i
       assert !tag.valid?, "Value #{i} should be too long"
+      assert tag.errors.invalid?(:v)
     end
   end
   
@@ -57,7 +58,7 @@ class WayTagTest < Test::Unit::TestCase
     assert tag.errors.invalid?(:id)
   end
   
-  def test_uniquness
+  def test_uniqueness
     tag = WayTag.new
     tag.id = current_way_tags(:t1).id
     tag.k = current_way_tags(:t1).k