Relation Tag testing. Also sort the belong_to/has_many for user/changeset/old_way.
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Tue, 18 Nov 2008 18:50:24 +0000 (18:50 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Tue, 18 Nov 2008 18:50:24 +0000 (18:50 +0000)
app/models/old_way_tag.rb
app/models/relation_tag.rb
app/models/user.rb
test/unit/old_relation_tag_test.rb [new file with mode: 0644]
test/unit/relation_tag_test.rb
test/unit/way_tag_test.rb

index 547fd177cabc263ab99994ec526ef5c84090544a..68003cbebad7151084f4cd9f8369d8b43094f7cd 100644 (file)
@@ -1,5 +1,5 @@
 class OldWayTag < ActiveRecord::Base
-  belongs_to :user
+  belongs_to :old_way
 
   set_table_name 'way_tags'
 
index 939165ebd30f9cc136a12d77f4b013165bf0be27..812b2ec3592f7f866a28b27b79259839b8658ad6 100644 (file)
@@ -3,4 +3,8 @@ class RelationTag < ActiveRecord::Base
 
   belongs_to :relation, :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 80faf68e9137c7ba60d2f75ef6073ed2f598e7d3..ecf41fd18fb827d4f56aee783416db627205f5f5 100644 (file)
@@ -9,6 +9,7 @@ class User < ActiveRecord::Base
   has_many :friends, :include => :befriendee, :conditions => ["users.visible = ?", true]
   has_many :tokens, :class_name => "UserToken"
   has_many :preferences, :class_name => "UserPreference"
+  has_many :changesets
 
   validates_presence_of :email, :display_name
   validates_confirmation_of :email, :message => 'Email addresses must match'
diff --git a/test/unit/old_relation_tag_test.rb b/test/unit/old_relation_tag_test.rb
new file mode 100644 (file)
index 0000000..309e2fe
--- /dev/null
@@ -0,0 +1,76 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class RelationTagTest < Test::Unit::TestCase
+  fixtures :relation_tags
+  set_fixture_class :relation_tags => OldRelationTag
+  
+  def test_tag_count
+    assert_equal 3, OldRlationTag.count
+  end
+  
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = OldRelationTag.new
+      tag.id = relation_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 = OldRelationTag.new
+      tag.id = relation_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 = OldRelationTag.new
+      tag.id = relation_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 = OldRelationTag.new
+      tag.id = relation_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 = OldRelationTag.new
+    assert !tag.valid?, "Empty tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
+  
+  def test_uniqueness
+    tag = OldRelationTag.new
+    tag.id = relation_tags(:t1).id
+    tag.version = relation_tags(:t1).version
+    tag.k = relation_tags(:t1).k
+    tag.v = relation_tags(:t1).v
+    assert tag.new_record?
+    assert !tag.valid?
+    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
+    assert tag.new_record?
+  end
+end
index 38c8af22b08895ecdbabfb632bf9f30d5d4fa749..f93e689ca59b9fb103b2121dd8d8b59e40e28203 100644 (file)
@@ -8,4 +8,64 @@ class RelationTagTest < Test::Unit::TestCase
     assert_equal 3, RelationTag.count
   end
   
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = RelationTag.new
+      tag.id = 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 = RelationTag.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 |i|
+      tag = RelationTag.new
+      tag.id = 1
+      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
+    ["v"*256].each do |i|
+      tag = RelationTag.new
+      tag.id = 1
+      tag.k = "k"
+      tag.v = i
+      assert !tag.valid?, "Value #{i} should be too long"
+      assert tag.errors.invalid?(:v)
+    end
+  end
+  
+  def test_empty_tag_invalid
+    tag = RelationTag.new
+    assert !tag.valid?, "Empty relation tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
+  
+  def test_uniquness
+    tag = RelationTag.new
+    tag.id = current_relation_tags(:t1).id
+    tag.k = current_relation_tags(:t1).k
+    tag.v = current_relation_tags(:t1).v
+    assert tag.new_record?
+    assert !tag.valid?
+    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
+    assert tag.new_record?
+  end
 end
index 44b96ffe3a7951ff6b8e9386a77ee61621e336d3..fc1c8efa5277fa0f9c2afc3e9604d31855a3b594 100644 (file)
@@ -42,7 +42,7 @@ class WayTagTest < Test::Unit::TestCase
   end
   
   def test_length_value_invalid
-    ["k"*256].each do |i|
+    ["v"*256].each do |i|
       tag = WayTag.new
       tag.id = current_way_tags(:t1).id
       tag.k = "k"
@@ -57,7 +57,7 @@ class WayTagTest < Test::Unit::TestCase
     assert tag.errors.invalid?(:id)
   end
   
-  def test_uniquess
+  def test_uniquness
     tag = WayTag.new
     tag.id = current_way_tags(:t1).id
     tag.k = current_way_tags(:t1).k