Checking foreign key constraints on message recipients and senders
[rails.git] / test / unit / message_test.rb
index 28c7605ffb979326e57589c5969e39908859a607..e95c698c9c787a0a8565caedab5bfb457e9d4358 100644 (file)
@@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper'
 class MessageTest < Test::Unit::TestCase
   fixtures :messages, :users
 
+  EURO = "\xe2\x82\xac" #euro symbol
+
   # This needs to be updated when new fixtures are added
   # or removed.
   def test_check_message_count
@@ -20,9 +22,46 @@ class MessageTest < Test::Unit::TestCase
   
   def test_validating_msgs
     message = messages(:one)
-    assert_equal true, message.valid?
+    assert message.valid?
     massage = messages(:two)
-    assert_equal true, message.valid?
+    assert message.valid?
   end
   
+  def test_invalid_send_recipient
+    message = messages(:one)
+    message.sender = nil
+    message.recipient = nil
+    assert !message.valid?
+
+    assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
+    message.from_user_id = 0
+    message.to_user_id = 0
+    assert_raise(ActiveRecord::RecordInvalid) {message.save!}
+  end
+
+  def test_utf8_roundtrip
+    (1..255).each do |i|
+      assert_message_ok('c', i)
+      assert_message_ok(EURO, i)
+    end
+  end
+
+  def test_length_oversize
+    assert_raise(ActiveRecord::RecordInvalid) { make_message('c', 256).save! }
+    assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
+  end
+
+  def make_message(char, count)
+    message = messages(:one)
+    message.title = char * count
+    return message
+  end
+
+  def assert_message_ok(char, count)
+    message = make_message(char, count)
+    assert message.save!
+    response = message.class.find(message.id) # stand by for some ├╝ber-generalisation...
+    assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length*count} bytes) fails"
+  end
+
 end