Adding validations for the length of node tags, and the presence of the id (and versi...
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Mon, 17 Nov 2008 18:53:09 +0000 (18:53 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Mon, 17 Nov 2008 18:53:09 +0000 (18:53 +0000)
app/models/node_tag.rb
app/models/old_node_tag.rb
test/fixtures/friends.yml [new file with mode: 0644]
test/unit/current_node_tag_test.rb [deleted file]
test/unit/diary_comment_test.rb [new file with mode: 0644]
test/unit/diary_entry_test.rb [new file with mode: 0644]
test/unit/friend_test.rb [new file with mode: 0644]
test/unit/node_tag_test.rb
test/unit/old_node_tag_test.rb [new file with mode: 0644]
test/unit/user_test.rb

index 9795ff49328b760c304ed587e6df65755006044e..de3d0629c4d29ce5769aa8f7ede0c65f020a9758 100644 (file)
@@ -2,4 +2,7 @@ class NodeTag < ActiveRecord::Base
   set_table_name 'current_node_tags'
 
   belongs_to :node, :foreign_key => 'id'
+  
+  validates_presence_of :id
+  validates_length_of :k, :v, :within => 0..255, :allow_blank => true
 end
index 26a6c92b490f52220ce1b60f068a4b800b2071b5..ef32a0613f3a9aa08df97d9b13d809c4a6f509a5 100644 (file)
@@ -3,5 +3,7 @@ class OldNodeTag < ActiveRecord::Base
 
   set_table_name 'node_tags'
 
+  validates_presence_of :id, :version
+  validates_length_of :k, :v, :within => 0..255, :allow_blank => true
 
 end
diff --git a/test/fixtures/friends.yml b/test/fixtures/friends.yml
new file mode 100644 (file)
index 0000000..782f1e3
--- /dev/null
@@ -0,0 +1,4 @@
+normal_user_with_second_user:
+  id: 1
+  user_id: 1
+  friend_user_id: 2
diff --git a/test/unit/current_node_tag_test.rb b/test/unit/current_node_tag_test.rb
deleted file mode 100644 (file)
index 143fa24..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-require File.dirname(__FILE__) + '/../test_helper'
-
-class CurrentNodeTagTest < Test::Unit::TestCase
-  fixtures :current_node_tags, :current_nodes
-  set_fixture_class :current_nodes => Node
-  set_fixture_class :current_node_tags => NodeTag
-  
-  def test_tag_count
-    assert_equal 6, NodeTag.count
-    node_tag_count(:visible_node, 1)
-    node_tag_count(:invisible_node, 1)
-    node_tag_count(:used_node_1, 1)
-    node_tag_count(:used_node_2, 1)
-    node_tag_count(:node_with_versions, 2)
-  end
-  
-  def node_tag_count (node, count)
-    nod = current_nodes(node)
-    assert_equal count, nod.node_tags.count
-  end
-  
-end
diff --git a/test/unit/diary_comment_test.rb b/test/unit/diary_comment_test.rb
new file mode 100644 (file)
index 0000000..d7f30a6
--- /dev/null
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class DiaryCommentTest < Test::Unit::TestCase
+  fixtures :diary_comments
+  
+  
+  def test_diary_comment_count
+    assert_equal 1, DiaryComment.count
+  end
+  
+end
diff --git a/test/unit/diary_entry_test.rb b/test/unit/diary_entry_test.rb
new file mode 100644 (file)
index 0000000..0e10f8a
--- /dev/null
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class DiaryEntryTest < Test::Unit::TestCase
+  fixtures :diary_entries
+  
+  
+  def test_diary_entry_count
+    assert_equal 2, DiaryEntry.count
+  end
+  
+end
diff --git a/test/unit/friend_test.rb b/test/unit/friend_test.rb
new file mode 100644 (file)
index 0000000..fd8b503
--- /dev/null
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class FriendTest < Test::Unit::TestCase
+  fixtures :friends
+  
+  
+  def test_friend_count
+    assert_equal 1, Friend.count
+  end
+  
+end
index 5e4d479536d472a1ac8a5f608b1839f4555c1ac5..fe6ebb166f2e949eade96120d3aa4a0a6fef43b7 100644 (file)
@@ -1,11 +1,71 @@
 require File.dirname(__FILE__) + '/../test_helper'
 
 class NodeTagTest < Test::Unit::TestCase
-  fixtures :current_node_tags
+  fixtures :current_node_tags, :current_nodes
+  set_fixture_class :current_nodes => Node
   set_fixture_class :current_node_tags => NodeTag
   
-  def test_node_tag_count
+  def test_tag_count
     assert_equal 6, NodeTag.count
+    node_tag_count(:visible_node, 1)
+    node_tag_count(:invisible_node, 1)
+    node_tag_count(:used_node_1, 1)
+    node_tag_count(:used_node_2, 1)
+    node_tag_count(:node_with_versions, 2)
   end
   
+  def node_tag_count (node, count)
+    nod = current_nodes(node)
+    assert_equal count, nod.node_tags.count
+  end
+  
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = NodeTag.new
+      tag.id = current_node_tags(:t1).id
+      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 = NodeTag.new
+      tag.id = current_node_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 = NodeTag.new
+      tag.id = current_node_tags(:t1).id
+      tag.k = i
+      tag.v = "v", "Key should be too long"
+      assert !tag.valid?
+      assert tag.errors.invalid?(:k)
+    end
+  end
+  
+  def test_length_value_invalid
+    ["k"*256].each do |i|
+      tag = NodeTag.new
+      tag.id = current_node_tags(:t1).id
+      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 = NodeTag.new
+    assert !tag.valid?, "Empty tag should be invalid"
+    assert tag.errors.invalid?(:id)
+  end
 end
diff --git a/test/unit/old_node_tag_test.rb b/test/unit/old_node_tag_test.rb
new file mode 100644 (file)
index 0000000..4a9bcbf
--- /dev/null
@@ -0,0 +1,66 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class OldNodeTest < Test::Unit::TestCase
+  set_fixture_class :nodes => OldNode
+  set_fixture_class :node_tags => OldNodeTag
+  fixtures  :users, :nodes, :node_tags
+  
+  def test_old_node_tag_count
+    assert_equal 8, OldNodeTag.count, "Unexpected number of fixtures loaded."
+  end
+  
+  def test_length_key_valid
+    key = "k"
+    (0..255).each do |i|
+      tag = OldNodeTag.new
+      tag.id = node_tags(:t1).id
+      tag.version = node_tags(:t1).version
+      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 = OldNodeTag.new
+      tag.id = node_tags(:t1).id
+      tag.version = node_tags(:t1).version
+      tag.k = "k"
+      tag.v = val*i
+      assert_valid tag
+    end
+  end
+  
+  def test_length_key_invalid
+    ["k"*256].each do |i|
+      tag = OldNodeTag.new
+      tag.id = node_tags(:t1).id
+      tag.version = node_tags(:t1).version
+      tag.k = i
+      tag.v = "v", "Key should be too long"
+      assert !tag.valid?
+      assert tag.errors.invalid?(:k)
+    end
+  end
+  
+  def test_length_value_invalid
+    ["k"*256].each do |i|
+      tag = OldNodeTag.new
+      tag.id = node_tags(:t1).id
+      tag.version = node_tags(:t1).version
+      tag.k = "k"
+      tag.v = i
+      assert !tag.valid?, "Value should be too long"
+      assert tag.errors.invalid?(:v)
+    end
+  end
+  
+  def test_empty_old_node_tag_invalid
+    tag = OldNodeTag.new
+    assert !tag.valid?, "Empty tag should be invalid"
+    assert tag.errors.invalid?(:id)
+    assert tag.errors.invalid?(:version)
+  end
+end
index 486344fee3c4bcec4f273b3cd9d484e5f679258d..c0df4b716984184aeaa49db5dca0dea810c0b85b 100644 (file)
@@ -96,7 +96,7 @@ class UserTest < Test::Unit::TestCase
   end
   
   def test_friend_with
-    assert_equal false, users(:normal_user).is_friends_with?(users(:second_user))
+    assert_equal true, users(:normal_user).is_friends_with?(users(:second_user))
     assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
     assert_equal false, users(:second_user).is_friends_with?(users(:normal_user))
     assert_equal false, users(:second_user).is_friends_with?(users(:inactive_user))
@@ -114,15 +114,15 @@ class UserTest < Test::Unit::TestCase
   end
   
   def test_friends_with
-    # make normal user a friend of second user
+    # normal user is a friend of second user
     # it should be a one way friend accossitation
-    assert_equal 0, Friend.count
+    assert_equal 1, Friend.count
     norm = users(:normal_user)
     sec = users(:second_user)
-    friend = Friend.new
-    friend.befriender = norm
-    friend.befriendee = sec
-    friend.save
+    #friend = Friend.new
+    #friend.befriender = norm
+    #friend.befriendee = sec
+    #friend.save
     assert_equal [sec], norm.nearby
     assert_equal 1, norm.nearby.size
     assert_equal 1, Friend.count
@@ -133,7 +133,7 @@ class UserTest < Test::Unit::TestCase
     assert_equal false, users(:second_user).is_friends_with?(users(:inactive_user))
     assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
     assert_equal false, users(:inactive_user).is_friends_with?(users(:second_user))
-    Friend.delete(friend)
-    assert_equal 0, Friend.count
+    #Friend.delete(friend)
+    #assert_equal 0, Friend.count
   end
 end