test/cases/base_test.rb in ibm_db-1.0.0 vs test/cases/base_test.rb in ibm_db-1.0.1

- old
+ new

@@ -74,11 +74,11 @@ attr_accessible :author_name attr_protected :content end class BasicsTest < ActiveRecord::TestCase - fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, :warehouse_things, :authors, :categorizations, :categories + fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, :warehouse_things, :authors, :categorizations, :categories, :posts def test_table_exists assert !NonExistentTable.table_exists? assert Topic.table_exists? end @@ -136,11 +136,11 @@ end if current_adapter?(:MysqlAdapter) def test_read_attributes_before_type_cast_on_boolean bool = Booleantest.create({ "value" => false }) - assert_equal 0, bool.attributes_before_type_cast["value"] + assert_equal "0", bool.reload.attributes_before_type_cast["value"] end end def test_read_attributes_before_type_cast_on_datetime developer = Developer.find(:first) @@ -426,13 +426,10 @@ assert_raises(NoMethodError) { topic.mumbo } assert_raises(NoMethodError) { topic.mumbo = 5 } end def test_preserving_date_objects - # SQL Server doesn't have a separate column type just for dates, so all are returned as time - return true if current_adapter?(:SQLServerAdapter) - if current_adapter?(:SybaseAdapter, :OracleAdapter) # Sybase ctlib does not (yet?) support the date type; use datetime instead. # Oracle treats all dates/times as Time. assert_kind_of( Time, Topic.find(1).last_read, @@ -469,11 +466,23 @@ topic = Topic.find(1) # This mutator is protected in the class definition topic.send(:approved=, true) assert topic.instance_variable_get("@custom_approved") end - + + def test_delete + topic = Topic.find(1) + assert_equal topic, topic.delete, 'topic.delete did not return self' + assert topic.frozen?, 'topic not frozen after delete' + assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) } + end + + def test_delete_doesnt_run_callbacks + Topic.find(1).delete + assert_not_nil Topic.find(2) + end + def test_destroy topic = Topic.find(1) assert_equal topic, topic.destroy, 'topic.destroy did not return self' assert topic.frozen?, 'topic not frozen after destroy' assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) } @@ -661,17 +670,28 @@ if current_adapter?(:MysqlAdapter) def test_update_all_with_order_and_limit assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC') end end - - def test_update_all_ignores_order_limit_from_association - author = Author.find(1) + + def test_update_all_ignores_order_without_limit_from_association + author = authors(:david) assert_nothing_raised do - assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all("body = 'bulk update!'") + assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ]) end end + + def test_update_all_with_order_and_limit_updates_subset_only + author = authors(:david) + assert_nothing_raised do + assert_equal 1, author.posts_sorted_by_id_limited.size + assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size + assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ]) + assert_equal "bulk update!", posts(:welcome).body + assert_not_equal "bulk update!", posts(:thinking).body + end + end def test_update_many topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } } updated = Topic.update(topic_data.keys, topic_data.values) @@ -751,13 +771,13 @@ assert_equal "X", test.test_char assert_equal "hello", test.test_string assert_equal 3, test.test_int end end - - # Oracle, SQLServer, and Sybase do not have a TIME datatype. - unless current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + + # Oracle, and Sybase do not have a TIME datatype. + unless current_adapter?(:OracleAdapter, :SybaseAdapter) def test_utc_as_time_zone Topic.default_timezone = :utc attributes = { "bonus_time" => "5:42:00AM" } topic = Topic.find(1) topic.attributes = attributes @@ -806,11 +826,25 @@ end def test_hashing assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ] end - + + def test_delete_new_record + client = Client.new + client.delete + assert client.frozen? + end + + def test_delete_record_with_associations + client = Client.find(3) + client.delete + assert client.frozen? + assert_kind_of Firm, client.firm + assert_raises(ActiveSupport::FrozenObjectError) { client.name = "something else" } + end + def test_destroy_new_record client = Client.new client.destroy assert client.frozen? end @@ -878,11 +912,11 @@ assert_equal 1, firm.rating end def test_mass_assignment_protection_against_class_attribute_writers [:logger, :configurations, :primary_key_prefix_type, :table_name_prefix, :table_name_suffix, :pluralize_table_names, :colorize_logging, - :default_timezone, :allow_concurrency, :schema_format, :verification_timeout, :lock_optimistically, :record_timestamps].each do |method| + :default_timezone, :schema_format, :lock_optimistically, :record_timestamps].each do |method| assert Task.respond_to?(method) assert Task.respond_to?("#{method}=") assert Task.new.respond_to?(method) assert !Task.new.respond_to?("#{method}=") end @@ -901,11 +935,19 @@ assert_nil subscriber.id keyboard = Keyboard.new(:id => 9, :name => 'nice try') assert_nil keyboard.id end - + + def test_mass_assigning_invalid_attribute + firm = Firm.new + + assert_raises(ActiveRecord::UnknownAttributeError) do + firm.attributes = { "id" => 5, "type" => "Client", "i_dont_even_exist" => 20 } + end + end + def test_mass_assignment_protection_on_defaults firm = Firm.new firm.attributes = { "id" => 5, "type" => "Client" } assert_nil firm.id assert_equal "Firm", firm[:type] @@ -1110,39 +1152,47 @@ customer.attributes = attributes assert_equal address, customer.address end def test_attributes_on_dummy_time - # Oracle, SQL Server, and Sybase do not have a TIME datatype. - return true if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) - + # Oracle, and Sybase do not have a TIME datatype. + return true if current_adapter?(:OracleAdapter, :SybaseAdapter) + attributes = { "bonus_time" => "5:42:00AM" } topic = Topic.find(1) topic.attributes = attributes assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time end def test_boolean + b_nil = Booleantest.create({ "value" => nil }) + nil_id = b_nil.id b_false = Booleantest.create({ "value" => false }) false_id = b_false.id b_true = Booleantest.create({ "value" => true }) true_id = b_true.id - + + b_nil = Booleantest.find(nil_id) + assert_nil b_nil.value b_false = Booleantest.find(false_id) assert !b_false.value? b_true = Booleantest.find(true_id) assert b_true.value? end def test_boolean_cast_from_string + b_blank = Booleantest.create({ "value" => "" }) + blank_id = b_blank.id b_false = Booleantest.create({ "value" => "0" }) false_id = b_false.id b_true = Booleantest.create({ "value" => "1" }) true_id = b_true.id - + + b_blank = Booleantest.find(blank_id) + assert_nil b_blank.value b_false = Booleantest.find(false_id) assert !b_false.value? b_true = Booleantest.find(true_id) assert b_true.value? end @@ -1381,10 +1431,16 @@ def test_serialized_time_attribute myobj = Time.local(2008,1,1,1,0) topic = Topic.create("content" => myobj).reload assert_equal(myobj, topic.content) end + + def test_serialized_string_attribute + myobj = "Yes" + topic = Topic.create("content" => myobj).reload + assert_equal(myobj, topic.content) + end def test_nil_serialized_attribute_with_class_constraint myobj = MyObject.new('value1', 'value2') topic = Topic.new assert_nil topic.content @@ -1416,19 +1472,21 @@ assert_equal author_name, Topic.find(topic.id).author_name end if RUBY_VERSION < '1.9' def test_quote_chars - str = 'The Narrator' - topic = Topic.create(:author_name => str) - assert_equal str, topic.author_name - - assert_kind_of ActiveSupport::Multibyte::Chars, str.chars - topic = Topic.find_by_author_name(str.chars) - - assert_kind_of Topic, topic - assert_equal str, topic.author_name, "The right topic should have been found by name even with name passed as Chars" + with_kcode('UTF8') do + str = 'The Narrator' + topic = Topic.create(:author_name => str) + assert_equal str, topic.author_name + + assert_kind_of ActiveSupport::Multibyte.proxy_class, str.mb_chars + topic = Topic.find_by_author_name(str.mb_chars) + + assert_kind_of Topic, topic + assert_equal str, topic.author_name, "The right topic should have been found by name even with name passed as Chars" + end end end def test_class_level_destroy should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world") @@ -1580,12 +1638,12 @@ res4 = Post.count_by_sql "SELECT COUNT(p.id) FROM posts p, comments co WHERE p.#{QUOTED_TYPE} = 'Post' AND p.id=co.post_id" res5 = nil assert_nothing_raised do res5 = Post.count(:conditions => "p.#{QUOTED_TYPE} = 'Post' AND p.id=co.post_id", - :joins => "p, comments co", - :select => "p.id") + :joins => "p, comments co", + :select => "p.id") end assert_equal res4, res5 unless current_adapter?(:SQLite2Adapter, :DeprecatedSQLiteAdapter) @@ -1817,12 +1875,12 @@ assert_equal "david@loudthinking.com", xml.elements["//author-email-address"].text assert_equal nil, xml.elements["//parent-id"].text assert_equal "integer", xml.elements["//parent-id"].attributes['type'] assert_equal "true", xml.elements["//parent-id"].attributes['nil'] - - if current_adapter?(:SybaseAdapter, :SQLServerAdapter, :OracleAdapter) + + if current_adapter?(:SybaseAdapter, :OracleAdapter) assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text assert_equal "datetime" , xml.elements["//last-read"].attributes['type'] else assert_equal "2004-04-15", xml.elements["//last-read"].text assert_equal "date" , xml.elements["//last-read"].attributes['type'] @@ -2017,6 +2075,20 @@ assert_no_match /Loud/, log.string assert_match /Quiet/, log.string ensure ActiveRecord::Base.logger = original_logger end + + private + def with_kcode(kcode) + if RUBY_VERSION < '1.9' + orig_kcode, $KCODE = $KCODE, kcode + begin + yield + ensure + $KCODE = orig_kcode + end + else + yield + end + end end