Namespace
Methods
Q
T
Constants
MyObject = Struct.new :attribute1, :attribute2
 
Instance Public methods
quote_column_name(name)
# File rails/activerecord/test/base_test.rb, line 1043
def quote_column_name(name)
  "<#{name}>"
end
test_abstract_class()
# File rails/activerecord/test/base_test.rb, line 1378
def test_abstract_class
  assert !ActiveRecord::Base.abstract_class?
  assert LoosePerson.abstract_class?
  assert !LooseDescendant.abstract_class?
end
test_array_content()
# File rails/activerecord/test/base_test.rb, line 102
def test_array_content
  topic = Topic.new
  topic.content = %w( one two three )
  topic.save

  assert_equal(%w( one two three ), Topic.find(topic.id).content)
end
test_array_to_xml_including_belongs_to_association()
# File rails/activerecord/test/base_test.rb, line 1505
def test_array_to_xml_including_belongs_to_association
  xml = [ companies(:first_client), companies(:second_client), companies(:another_client) ].to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
  assert xml.include?(companies(:first_client).to_xml(:indent => 0, :skip_instruct => true))
  assert xml.include?(companies(:second_client).firm.to_xml(:indent => 0, :skip_instruct => true))
  assert xml.include?(companies(:another_client).firm.to_xml(:indent => 0, :skip_instruct => true))
end
test_array_to_xml_including_has_many_association()
# File rails/activerecord/test/base_test.rb, line 1488
def test_array_to_xml_including_has_many_association
  xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies)
  assert xml.include?(%Q(<replies><reply>))
end
test_array_to_xml_including_has_one_association()
# File rails/activerecord/test/base_test.rb, line 1499
def test_array_to_xml_including_has_one_association
  xml = [ companies(:first_firm), companies(:rails_core) ].to_xml(:indent => 0, :skip_instruct => true, :include => :account)
  assert xml.include?(companies(:first_firm).account.to_xml(:indent => 0, :skip_instruct => true))
  assert xml.include?(companies(:rails_core).account.to_xml(:indent => 0, :skip_instruct => true))
end
test_array_to_xml_including_methods()
# File rails/activerecord/test/base_test.rb, line 1493
def test_array_to_xml_including_methods
  xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :methods => [ :topic_id ])
  assert xml.include?(%Q(<topic-id type="integer">#{topics(:first).topic_id}</topic-id>)), xml
  assert xml.include?(%Q(<topic-id type="integer">#{topics(:second).topic_id}</topic-id>)), xml
end
test_assert_queries()
# File rails/activerecord/test/base_test.rb, line 1437
def test_assert_queries
  query = lambda { ActiveRecord::Base.connection.execute 'select count(*) from developers' }
  assert_queries(2) { 2.times { query.call } }
  assert_queries 1, &query
  assert_no_queries { assert true }
end
test_attribute_keys_on_new_instance()
# File rails/activerecord/test/base_test.rb, line 541
def test_attribute_keys_on_new_instance
  t = Topic.new
  assert_equal nil, t.title, "The topics table has a title column, so it should be nil"
  assert_raise(NoMethodError) { t.title2 }
end
test_attribute_present()
# File rails/activerecord/test/base_test.rb, line 532
def test_attribute_present
  t = Topic.new
  t.title = "hello there!"
  t.written_on = Time.now
  assert t.attribute_present?("title")
  assert t.attribute_present?("written_on")
  assert !t.attribute_present?("content")
end
test_attributes_on_dummy_time()
# File rails/activerecord/test/base_test.rb, line 833
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)

  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
test_auto_id()
# File rails/activerecord/test/base_test.rb, line 1037
def test_auto_id
  auto = AutoId.new
  auto.save
  assert (auto.id > 0)
end
test_base_class()
# File rails/activerecord/test/base_test.rb, line 1384
def test_base_class
  assert_equal LoosePerson,     LoosePerson.base_class
  assert_equal LooseDescendant, LooseDescendant.base_class
  assert_equal TightPerson,     TightPerson.base_class
  assert_equal TightPerson,     TightDescendant.base_class

  assert_equal Post, Post.base_class
  assert_equal Post, SpecialPost.base_class
  assert_equal Post, StiPost.base_class
  assert_equal SubStiPost, SubStiPost.base_class
end
test_bignum()
# File rails/activerecord/test/base_test.rb, line 919
def test_bignum
  company = Company.find(1)
  company.rating = 2147483647
  company.save
  company = Company.find(1)
  assert_equal 2147483647, company.rating
end
test_boolean()
# File rails/activerecord/test/base_test.rb, line 845
def test_boolean
  b_false = Booleantest.create({ "value" => false })
  false_id = b_false.id
  b_true = Booleantest.create({ "value" => true })
  true_id = b_true.id

  b_false = Booleantest.find(false_id)
  assert !b_false.value?
  b_true = Booleantest.find(true_id)
  assert b_true.value?
end
test_boolean_attributes()
# File rails/activerecord/test/base_test.rb, line 477
def test_boolean_attributes
  assert ! Topic.find(1).approved?
  assert Topic.find(2).approved?
end
test_boolean_cast_from_string()
# File rails/activerecord/test/base_test.rb, line 857
def test_boolean_cast_from_string
  b_false = Booleantest.create({ "value" => "0" })
  false_id = b_false.id
  b_true = Booleantest.create({ "value" => "1" })
  true_id = b_true.id

  b_false = Booleantest.find(false_id)
  assert !b_false.value?
  b_true = Booleantest.find(true_id)
  assert b_true.value?    
end
test_case_sensitive_attributes_hash()
# File rails/activerecord/test/base_test.rb, line 137
def test_case_sensitive_attributes_hash
  # DB2 is not case-sensitive
  return true if current_adapter?(:DB2Adapter)

  assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
end
test_class_level_delete()
# File rails/activerecord/test/base_test.rb, line 1142
def test_class_level_delete
  should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
  Topic.find(1).replies << should_be_destroyed_reply

  Topic.delete(1)
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
  assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
end
test_class_level_destroy()
# File rails/activerecord/test/base_test.rb, line 1133
def test_class_level_destroy
  should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
  Topic.find(1).replies << should_be_destroyed_reply

  Topic.destroy(1)
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
  assert_raise(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
end
test_class_name()
# File rails/activerecord/test/base_test.rb, line 547
def test_class_name
  assert_equal "Firm", ActiveRecord::Base.class_name("firms")
  assert_equal "Category", ActiveRecord::Base.class_name("categories")
  assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")

  ActiveRecord::Base.pluralize_table_names = false
  assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
  ActiveRecord::Base.pluralize_table_names = true

  ActiveRecord::Base.table_name_prefix = "test_"
  assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
  ActiveRecord::Base.table_name_suffix = "_tests"
  assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
  ActiveRecord::Base.table_name_prefix = ""
  assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
  ActiveRecord::Base.table_name_suffix = ""
  assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
end
test_clear_association_cache_new_record()
# File rails/activerecord/test/base_test.rb, line 1296
def test_clear_association_cache_new_record
   firm            = Firm.new
   client_stored   = Client.find(3)
   client_new      = Client.new
   client_new.name = "The Joneses"
   clients         = [ client_stored, client_new ]
   
   firm.clients    << clients

   firm.clear_association_cache

   assert_equal    firm.clients.collect{ |x| x.name }.sort, clients.collect{ |x| x.name }.sort
end
test_clear_association_cache_stored()
# File rails/activerecord/test/base_test.rb, line 1288
def test_clear_association_cache_stored     
  firm = Firm.find(1)
  assert_kind_of Firm, firm

  firm.clear_association_cache
  assert_equal Firm.find(1).clients.collect{ |x| x.name }.sort, firm.clients.collect{ |x| x.name }.sort
end
test_clone()
# File rails/activerecord/test/base_test.rb, line 869
def test_clone
  topic = Topic.find(1)
  cloned_topic = nil
  assert_nothing_raised { cloned_topic = topic.clone }
  assert_equal topic.title, cloned_topic.title
  assert cloned_topic.new_record?

  # test if the attributes have been cloned
  topic.title = "a" 
  cloned_topic.title = "b" 
  assert_equal "a", topic.title
  assert_equal "b", cloned_topic.title

  # test if the attribute values have been cloned
  topic.title = {"a" => "b"}
  cloned_topic = topic.clone
  cloned_topic.title["a"] = "c" 
  assert_equal "b", topic.title["a"]

  cloned_topic.save
  assert !cloned_topic.new_record?
  assert cloned_topic.id != topic.id
end
test_clone_preserves_subtype()
# File rails/activerecord/test/base_test.rb, line 913
def test_clone_preserves_subtype
  clone = nil
  assert_nothing_raised { clone = Company.find(3).clone }
  assert_kind_of Client, clone
end
test_clone_with_aggregate_of_same_name_as_attribute()
# File rails/activerecord/test/base_test.rb, line 893
def test_clone_with_aggregate_of_same_name_as_attribute
  dev = DeveloperWithAggregate.find(1)
  assert_kind_of DeveloperSalary, dev.salary

  clone = nil
  assert_nothing_raised { clone = dev.clone }
  assert_kind_of DeveloperSalary, clone.salary
  assert_equal dev.salary.amount, clone.salary.amount
  assert clone.new_record?

  # test if the attributes have been cloned
  original_amount = clone.salary.amount
  dev.salary.amount = 1
  assert_equal original_amount, clone.salary.amount

  assert clone.save
  assert !clone.new_record?
  assert clone.id != dev.id
end
test_column_name_properly_quoted()
# File rails/activerecord/test/base_test.rb, line 1062
def test_column_name_properly_quoted
  col_record = ColumnName.new
  col_record.references = 40
  assert col_record.save
  col_record.references = 41
  assert col_record.save
  assert_not_nil c2 = ColumnName.find(col_record.id)
  assert_equal(41, c2.references)
end
test_count_with_join()
# File rails/activerecord/test/base_test.rb, line 1251
def test_count_with_join
  res = Post.count_by_sql "SELECT COUNT(*) FROM posts LEFT JOIN comments ON posts.id=comments.post_id WHERE posts.#{QUOTED_TYPE} = 'Post'"
  res2 = nil
  assert_deprecated 'count' do
    res2 = Post.count("posts.#{QUOTED_TYPE} = 'Post'",
                      "LEFT JOIN comments ON posts.id=comments.post_id")
  end
  assert_equal res, res2
  
  res3 = nil
  assert_nothing_raised do
    res3 = Post.count(:conditions => "posts.#{QUOTED_TYPE} = 'Post'",
                      :joins => "LEFT JOIN comments ON posts.id=comments.post_id")
  end
  assert_equal res, res3
  
  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")
  end

  assert_equal res4, res5 

  res6 = Post.count_by_sql "SELECT COUNT(DISTINCT p.id) FROM posts p, comments co WHERE p.#{QUOTED_TYPE} = 'Post' AND p.id=co.post_id"
  res7 = nil
  assert_nothing_raised do
    res7 = Post.count(:conditions => "p.#{QUOTED_TYPE} = 'Post' AND p.id=co.post_id",
                      :joins => "p, comments co",
                      :select => "p.id",
                      :distinct => true)
  end
  assert_equal res6, res7
end
test_create()
# File rails/activerecord/test/base_test.rb, line 144
def test_create
  topic = Topic.new
  topic.title = "New Topic"
  topic.save
  topic_reloaded = Topic.find(topic.id)
  assert_equal("New Topic", topic_reloaded.title)
end
test_create_columns_not_equal_attributes()
# File rails/activerecord/test/base_test.rb, line 194
def test_create_columns_not_equal_attributes
  topic = Topic.new
  topic.title = 'Another New Topic'
  topic.send :write_attribute, 'does_not_exist', 'test'
  assert_nothing_raised { topic.save }
end
test_create_many()
# File rails/activerecord/test/base_test.rb, line 188
def test_create_many
  topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
  assert_equal 2, topics.size
  assert_equal "first", topics.first.title
end
test_create_through_factory()
# File rails/activerecord/test/base_test.rb, line 201
def test_create_through_factory
  topic = Topic.create("title" => "New Topic")
  topicReloaded = Topic.find(topic.id)
  assert_equal(topic, topicReloaded)
end
test_customized_primary_key_remains_protected()
# File rails/activerecord/test/base_test.rb, line 722
def test_customized_primary_key_remains_protected
  subscriber = Subscriber.new(:nick => 'webster123', :name => 'nice try')
  assert_nil subscriber.id

  keyboard = Keyboard.new(:key_number => 9, :name => 'nice try')
  assert_nil keyboard.id
end
test_customized_primary_key_remains_protected_when_refered_to_as_id()
# File rails/activerecord/test/base_test.rb, line 730
def test_customized_primary_key_remains_protected_when_refered_to_as_id
  subscriber = Subscriber.new(:id => 'webster123', :name => 'nice try')
  assert_nil subscriber.id

  keyboard = Keyboard.new(:id => 9, :name => 'nice try')
  assert_nil keyboard.id
end
test_decrement_attribute()
# File rails/activerecord/test/base_test.rb, line 1166
def test_decrement_attribute
  topics(:first).increment(:replies_count).increment!(:replies_count)
  assert_equal 3, topics(:first).replies_count
  
  topics(:first).decrement!(:replies_count)
  assert_equal 2, topics(:first, :reload).replies_count

  topics(:first).decrement(:replies_count).decrement!(:replies_count)
  assert_equal 0, topics(:first, :reload).replies_count
end
test_decrement_counter()
# File rails/activerecord/test/base_test.rb, line 490
def test_decrement_counter
  Topic.decrement_counter("replies_count", 2)
  assert_equal -1, Topic.find(2).replies_count

  Topic.decrement_counter("replies_count", 2)
  assert_equal -2, Topic.find(2).replies_count
end
test_default()
# File rails/activerecord/test/base_test.rb, line 929
def test_default
  default = Default.new
  
  # fixed dates / times
  assert_equal Date.new(2004, 1, 1), default.fixed_date
  assert_equal Time.local(2004, 1,1,0,0,0,0), default.fixed_time
  
  # char types
  assert_equal 'Y', default.char1
  assert_equal 'a varchar field', default.char2
  assert_equal 'a text field', default.char3
end
test_default_values()
# File rails/activerecord/test/base_test.rb, line 571
def test_default_values
  topic = Topic.new
  assert topic.approved?
  assert_nil topic.written_on
  assert_nil topic.bonus_time
  assert_nil topic.last_read
  
  topic.save

  topic = Topic.find(topic.id)
  assert topic.approved?
  assert_nil topic.last_read

  # Oracle has some funky default handling, so it requires a bit of 
  # extra testing. See ticket #2788.
  if current_adapter?(:OracleAdapter)
    test = TestOracleDefault.new
    assert_equal "X", test.test_char
    assert_equal "hello", test.test_string
    assert_equal 3, test.test_int
  end
end
test_default_values_on_empty_strings()
# File rails/activerecord/test/base_test.rb, line 619
def test_default_values_on_empty_strings
  topic = Topic.new
  topic.approved  = nil
  topic.last_read = nil

  topic.save

  topic = Topic.find(topic.id)
  assert_nil topic.last_read

  # Sybase adapter does not allow nulls in boolean columns
  if current_adapter?(:SybaseAdapter)
    assert topic.approved == false
  else
    assert_nil topic.approved
  end
end
test_define_attr_method_with_block()
# File rails/activerecord/test/base_test.rb, line 1203
def test_define_attr_method_with_block
  k = Class.new( ActiveRecord::Base )
  k.send(:define_attr_method, :primary_key) { "sys_" + original_primary_key }
  assert_equal "sys_id", k.primary_key
end
test_define_attr_method_with_value()
# File rails/activerecord/test/base_test.rb, line 1197
def test_define_attr_method_with_value
  k = Class.new( ActiveRecord::Base )
  k.send(:define_attr_method, :table_name, "foo")
  assert_equal "foo", k.table_name
end
test_delete_all()
# File rails/activerecord/test/base_test.rb, line 519
def test_delete_all
  # The ADO library doesn't support the number of affected rows
  return true if current_adapter?(:SQLServerAdapter)

  assert_equal 2, Topic.delete_all
end
test_delete_many()
# File rails/activerecord/test/base_test.rb, line 472
def test_delete_many
  Topic.delete([1, 2])
  assert_equal 0, Topic.count
end
test_descends_from_active_record()
# File rails/activerecord/test/base_test.rb, line 1396
def test_descends_from_active_record
  # Tries to call Object.abstract_class?
  assert_raise(NoMethodError) do
    ActiveRecord::Base.descends_from_active_record?
  end

  # Abstract subclass of AR::Base.
  assert LoosePerson.descends_from_active_record?

  # Concrete subclass of an abstract class.
  assert LooseDescendant.descends_from_active_record?

  # Concrete subclass of AR::Base.
  assert TightPerson.descends_from_active_record?

  # Concrete subclass of a concrete class but has no type column.
  assert TightDescendant.descends_from_active_record?

  # Concrete subclass of AR::Base.
  assert Post.descends_from_active_record?

  # Abstract subclass of a concrete class which has a type column.
  # This is pathological, as you'll never have Sub < Abstract < Concrete.
  assert !StiPost.descends_from_active_record?

  # Concrete subclasses an abstract class which has a type column.
  assert !SubStiPost.descends_from_active_record?
end
test_destroy()
# File rails/activerecord/test/base_test.rb, line 357
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) }
end
test_destroy_all()
# File rails/activerecord/test/base_test.rb, line 459
def test_destroy_all
  assert_equal 2, Topic.count

  Topic.destroy_all "author_name = 'Mary'"
  assert_equal 1, Topic.count
end
test_destroy_many()
# File rails/activerecord/test/base_test.rb, line 466
def test_destroy_many
  assert_equal 3, Client.count
  Client.destroy([2, 3])
  assert_equal 1, Client.count
end
test_destroy_new_record()
# File rails/activerecord/test/base_test.rb, line 649
def test_destroy_new_record
  client = Client.new
  client.destroy
  assert client.frozen?
end
test_destroy_record_with_associations()
# File rails/activerecord/test/base_test.rb, line 655
def test_destroy_record_with_associations
  client = Client.find(3)
  client.destroy
  assert client.frozen?
  assert_kind_of Firm, client.firm
  assert_raises(TypeError) { client.name = "something else" }
end
test_equality()
# File rails/activerecord/test/base_test.rb, line 637
def test_equality
  assert_equal Topic.find(1), Topic.find(2).topic
end
test_equality_of_new_records()
# File rails/activerecord/test/base_test.rb, line 641
def test_equality_of_new_records
  assert_not_equal Topic.new, Topic.new
end
test_except_attributes()
# File rails/activerecord/test/base_test.rb, line 1544
def test_except_attributes
  assert_equal(
    %w( author_name type id approved replies_count bonus_time written_on content author_email_address parent_id last_read), 
    topics(:first).attributes(:except => :title).keys
  )

  assert_equal(
    %w( replies_count bonus_time written_on content author_email_address parent_id last_read), 
    topics(:first).attributes(:except => [ :title, :id, :type, :approved, :author_name ]).keys
  )
end
test_find_on_abstract_base_class_doesnt_use_type_condition()
# File rails/activerecord/test/base_test.rb, line 1425
def test_find_on_abstract_base_class_doesnt_use_type_condition
  old_class = LooseDescendant
  Object.send :remove_const, :LooseDescendant

  descendant = old_class.create!
  assert_not_nil LoosePerson.find(descendant.id), "Should have found instance of LooseDescendant when finding abstract LoosePerson: #{descendant.inspect}"
ensure
  unless Object.const_defined?(:LooseDescendant)
    Object.const_set :LooseDescendant, old_class
  end
end
test_geometric_content()
# File rails/activerecord/test/base_test.rb, line 943
def test_geometric_content

  # accepted format notes:
  # ()'s aren't required
  # values can be a mix of float or integer

  g = Geometric.new(
    :a_point        => '(5.0, 6.1)',
    #:a_line         => '((2.0, 3), (5.5, 7.0))' # line type is currently unsupported in postgresql
    :a_line_segment => '(2.0, 3), (5.5, 7.0)',
    :a_box          => '2.0, 3, 5.5, 7.0',
    :a_path         => '[(2.0, 3), (5.5, 7.0), (8.5, 11.0)]',  # [ ] is an open path
    :a_polygon      => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))',
    :a_circle       => '<(5.3, 10.4), 2>'
  )
    
  assert g.save

  # Reload and check that we have all the geometric attributes.
  h = Geometric.find(g.id)

  assert_equal '(5,6.1)', h.a_point
  assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
  assert_equal '(5.5,7),(2,3)', h.a_box   # reordered to store upper right corner then bottom left corner
  assert_equal '[(2,3),(5.5,7),(8.5,11)]', h.a_path
  assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
  assert_equal '<(5.3,10.4),2>', h.a_circle

  # use a geometric function to test for an open path
  objs = Geometric.find_by_sql ["select isopen(a_path) from geometrics where id = ?", g.id]
  assert_equal objs[0].isopen, 't'

  # test alternate formats when defining the geometric types
  
  g = Geometric.new(
    :a_point        => '5.0, 6.1',
    #:a_line         => '((2.0, 3), (5.5, 7.0))' # line type is currently unsupported in postgresql
    :a_line_segment => '((2.0, 3), (5.5, 7.0))',
    :a_box          => '(2.0, 3), (5.5, 7.0)',
    :a_path         => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))',  # ( ) is a closed path
    :a_polygon      => '2.0, 3, 5.5, 7.0, 8.5, 11.0',
    :a_circle       => '((5.3, 10.4), 2)'
  )

  assert g.save

  # Reload and check that we have all the geometric attributes.
  h = Geometric.find(g.id)
  
  assert_equal '(5,6.1)', h.a_point
  assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
  assert_equal '(5.5,7),(2,3)', h.a_box   # reordered to store upper right corner then bottom left corner
  assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_path
  assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
  assert_equal '<(5.3,10.4),2>', h.a_circle

  # use a geometric function to test for an closed path
  objs = Geometric.find_by_sql ["select isclosed(a_path) from geometrics where id = ?", g.id]
  assert_equal objs[0].isclosed, 't'
end
test_hash_content()
# File rails/activerecord/test/base_test.rb, line 110
def test_hash_content
  topic = Topic.new
  topic.content = { "one" => 1, "two" => 2 }
  topic.save

  assert_equal 2, Topic.find(topic.id).content["two"]
  
  topic.content["three"] = 3
  topic.save

  assert_equal 3, Topic.find(topic.id).content["three"]
end
test_hashes_not_mangled()
# File rails/activerecord/test/base_test.rb, line 177
def test_hashes_not_mangled
  new_topic = { :title => "New Topic" }
  new_topic_values = { :title => "AnotherTopic" }

  topic = Topic.new(new_topic)
  assert_equal new_topic[:title], topic.title

  topic.attributes= new_topic_values
  assert_equal new_topic_values[:title], topic.title
end
test_hashing()
# File rails/activerecord/test/base_test.rb, line 645
def test_hashing
  assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end
test_include_attributes()
# File rails/activerecord/test/base_test.rb, line 1556
def test_include_attributes
  assert_equal(%w( title ), topics(:first).attributes(:only => :title).keys)
  assert_equal(%w( title author_name type id approved ), topics(:first).attributes(:only => [ :title, :id, :type, :approved, :author_name ]).keys)
end
test_increment_attribute()
# File rails/activerecord/test/base_test.rb, line 1151
def test_increment_attribute
  assert_equal 1, topics(:first).replies_count
  topics(:first).increment! :replies_count
  assert_equal 2, topics(:first, :reload).replies_count
  
  topics(:first).increment(:replies_count).increment!(:replies_count)
  assert_equal 4, topics(:first, :reload).replies_count
end
test_increment_counter()
# File rails/activerecord/test/base_test.rb, line 482
def test_increment_counter
  Topic.increment_counter("replies_count", 1)
  assert_equal 2, Topic.find(1).replies_count

  Topic.increment_counter("replies_count", 1)
  assert_equal 3, Topic.find(1).replies_count
end
test_increment_nil_attribute()
# File rails/activerecord/test/base_test.rb, line 1160
def test_increment_nil_attribute
  assert_nil topics(:first).parent_id
  topics(:first).increment! :parent_id
  assert_equal 1, topics(:first).parent_id
end
test_initialize_with_attributes()
# File rails/activerecord/test/base_test.rb, line 368
def test_initialize_with_attributes
  topic = Topic.new({ 
    "title" => "initialized from attributes", "written_on" => "2003-12-12 23:23"
  })
  
  assert_equal("initialized from attributes", topic.title)
end
test_initialize_with_invalid_attribute()
# File rails/activerecord/test/base_test.rb, line 376
def test_initialize_with_invalid_attribute
  begin
    topic = Topic.new({ "title" => "test", 
      "last_read(1i)" => "2005", "last_read(2i)" => "2", "last_read(3i)" => "31"})
  rescue ActiveRecord::MultiparameterAssignmentErrors => ex
    assert_equal(1, ex.errors.size)
    assert_equal("last_read", ex.errors[0].attribute)
  end
end
test_integers_as_nil()
# File rails/activerecord/test/base_test.rb, line 73
def test_integers_as_nil
  test = AutoId.create('value' => '')
  assert_nil AutoId.find(test.id).value
end
test_interpolate_sql()
# File rails/activerecord/test/base_test.rb, line 1310
def test_interpolate_sql
  assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo@bar') }
  assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar) baz') }
  assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
end
test_load()
# File rails/activerecord/test/base_test.rb, line 386
def test_load
  topics = Topic.find(:all, :order => 'id')    
  assert_equal(2, topics.size)
  assert_equal(topics(:first).title, topics.first.title)
end
test_load_with_condition()
# File rails/activerecord/test/base_test.rb, line 392
def test_load_with_condition
  topics = Topic.find(:all, :conditions => "author_name = 'Mary'")
  
  assert_equal(1, topics.size)
  assert_equal(topics(:second).title, topics.first.title)
end
test_mass_assignment_accessible()
# File rails/activerecord/test/base_test.rb, line 745
def test_mass_assignment_accessible
  reply = Reply.new("title" => "hello", "content" => "world", "approved" => true)
  reply.save

  assert reply.approved?
  
  reply.approved = false
  reply.save

  assert !reply.approved?
end
test_mass_assignment_protection()
# File rails/activerecord/test/base_test.rb, line 706
def test_mass_assignment_protection
  firm = Firm.new
  firm.attributes = { "name" => "Next Angle", "rating" => 5 }
  assert_equal 1, firm.rating
end
test_mass_assignment_protection_against_class_attribute_writers()
# File rails/activerecord/test/base_test.rb, line 712
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, :generate_read_methods, :schema_format, :verification_timeout, :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
end
test_mass_assignment_protection_inheritance()
# File rails/activerecord/test/base_test.rb, line 757
def test_mass_assignment_protection_inheritance
  assert_nil LoosePerson.accessible_attributes
  assert_equal [ :credit_rating, :administrator ], LoosePerson.protected_attributes

  assert_nil LooseDescendant.accessible_attributes
  assert_equal [ :credit_rating, :administrator, :phone_number  ], LooseDescendant.protected_attributes

  assert_nil TightPerson.protected_attributes
  assert_equal [ :name, :address ], TightPerson.accessible_attributes

  assert_nil TightDescendant.protected_attributes
  assert_equal [ :name, :address, :phone_number  ], TightDescendant.accessible_attributes
end
test_mass_assignment_protection_on_defaults()
# File rails/activerecord/test/base_test.rb, line 738
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]
end
test_multiparameter_assignment_of_aggregation()
# File rails/activerecord/test/base_test.rb, line 825
def test_multiparameter_assignment_of_aggregation
  customer = Customer.new
  address = Address.new("The Street", "The City", "The Country")
  attributes = { "address(1)" => address.street, "address(2)" => address.city, "address(3)" => address.country }
  customer.attributes = attributes
  assert_equal address, customer.address
end
test_multiparameter_attributes_on_date()
# File rails/activerecord/test/base_test.rb, line 771
def test_multiparameter_attributes_on_date
  attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
  topic = Topic.find(1)
  topic.attributes = attributes
  # note that extra #to_date call allows test to pass for Oracle, which 
  # treats dates/times the same
  assert_date_from_db Date.new(2004, 6, 24), topic.last_read.to_date
end
test_multiparameter_attributes_on_date_with_all_empty()
# File rails/activerecord/test/base_test.rb, line 789
def test_multiparameter_attributes_on_date_with_all_empty
  attributes = { "last_read(1i)" => "", "last_read(2i)" => "", "last_read(3i)" => "" }
  topic = Topic.find(1)
  topic.attributes = attributes
  assert_nil topic.last_read
end
test_multiparameter_attributes_on_date_with_empty_date()
# File rails/activerecord/test/base_test.rb, line 780
def test_multiparameter_attributes_on_date_with_empty_date
  attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" }
  topic = Topic.find(1)
  topic.attributes = attributes
  # note that extra #to_date call allows test to pass for Oracle, which 
  # treats dates/times the same
  assert_date_from_db Date.new(2004, 6, 1), topic.last_read.to_date
end
test_multiparameter_attributes_on_time()
# File rails/activerecord/test/base_test.rb, line 796
def test_multiparameter_attributes_on_time
  attributes = { 
    "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", 
    "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  }
  topic = Topic.find(1)
  topic.attributes = attributes
  assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
end
test_multiparameter_attributes_on_time_with_empty_seconds()
# File rails/activerecord/test/base_test.rb, line 806
def test_multiparameter_attributes_on_time_with_empty_seconds
  attributes = { 
    "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", 
    "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => ""
  }
  topic = Topic.find(1)
  topic.attributes = attributes
  assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
end
test_multiparameter_mass_assignment_protector()
# File rails/activerecord/test/base_test.rb, line 816
def test_multiparameter_mass_assignment_protector
  task = Task.new
  time = Time.mktime(2000, 1, 1, 1)
  task.starting = time 
  attributes = { "starting(1i)" => "2004", "starting(2i)" => "6", "starting(3i)" => "24" }
  task.attributes = attributes
  assert_equal time, task.starting
end
test_nil_serialized_attribute_with_class_constraint()
# File rails/activerecord/test/base_test.rb, line 1089
def test_nil_serialized_attribute_with_class_constraint
  myobj = MyObject.new('value1', 'value2')
  topic = Topic.new
  assert_nil topic.content
end
test_non_attribute_access_and_assignment()
# File rails/activerecord/test/base_test.rb, line 313
def test_non_attribute_access_and_assignment
  topic = Topic.new
  assert !topic.respond_to?("mumbo")
  assert_raises(NoMethodError) { topic.mumbo }
  assert_raises(NoMethodError) { topic.mumbo = 5 }
end
test_null_fields()
# File rails/activerecord/test/base_test.rb, line 566
def test_null_fields
  assert_nil Topic.find(1).parent_id
  assert_nil Topic.create("title" => "Hey you").parent_id
end
test_numeric_fields()
# File rails/activerecord/test/base_test.rb, line 1009
def test_numeric_fields
  m = NumericData.new(
    :bank_balance => 1586.43,
    :big_bank_balance => BigDecimal("1000234000567.95"),
    :world_population => 6000000000,
    :my_house_population => 3
  )
  assert m.save

  m1 = NumericData.find(m.id)
  assert_not_nil m1

  # As with migration_test.rb, we should make world_population >= 2**62
  # to cover 64-bit platforms and test it is a Bignum, but the main thing
  # is that it's an Integer.
  assert_kind_of Integer, m1.world_population
  assert_equal 6000000000, m1.world_population

  assert_kind_of Fixnum, m1.my_house_population
  assert_equal 3, m1.my_house_population

  assert_kind_of BigDecimal, m1.bank_balance
  assert_equal BigDecimal("1586.43"), m1.bank_balance

  assert_kind_of BigDecimal, m1.big_bank_balance
  assert_equal BigDecimal("1000234000567.95"), m1.big_bank_balance
end
test_preserving_date_objects()
# File rails/activerecord/test/base_test.rb, line 320
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)
    # Sybase ctlib does not (yet?) support the date type; use datetime instead.
    assert_kind_of(
      Time, Topic.find(1).last_read, 
      "The last_read attribute should be of the Time class"
    )
  else
    assert_kind_of(
      Date, Topic.find(1).last_read, 
      "The last_read attribute should be of the Date class"
    )
  end
end
test_preserving_time_objects()
# File rails/activerecord/test/base_test.rb, line 338
def test_preserving_time_objects
  assert_kind_of(
    Time, Topic.find(1).bonus_time,
    "The bonus_time attribute should be of the Time class"
  )

  assert_kind_of(
    Time, Topic.find(1).written_on,
    "The written_on attribute should be of the Time class"
  )

  # For adapters which support microsecond resolution.
  if current_adapter?(:PostgreSQLAdapter)
    assert_equal 11, Topic.find(1).written_on.sec
    assert_equal 223300, Topic.find(1).written_on.usec
    assert_equal 9900, Topic.find(2).written_on.usec
  end
end
test_quote()
# File rails/activerecord/test/base_test.rb, line 1115
def test_quote
  author_name = "\\ \0001 ' \n \\n \""
  topic = Topic.create('author_name' => author_name)
  assert_equal author_name, Topic.find(topic.id).author_name
end
test_quote_chars()
# File rails/activerecord/test/base_test.rb, line 1121
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"
end
test_quote_keys()
# File rails/activerecord/test/base_test.rb, line 1047
def test_quote_keys
  ar = AutoId.new
  source = {"foo" => "bar", "baz" => "quux"}
  actual = ar.send(:quote_columns, self, source)
  inverted = actual.invert
  assert_equal("<foo>", inverted["bar"])
  assert_equal("<baz>", inverted["quux"])
end
test_quoting_arrays()
# File rails/activerecord/test/base_test.rb, line 1072
def test_quoting_arrays
  replies = Reply.find(:all, :conditions => [ "id IN (?)", topics(:first).replies.collect(&:id) ])
  assert_equal topics(:first).replies.size, replies.size

  replies = Reply.find(:all, :conditions => [ "id IN (?)", [] ])
  assert_equal 0, replies.size
end
test_read_attribute()
# File rails/activerecord/test/base_test.rb, line 243
def test_read_attribute
  topic = Topic.new
  topic.title = "Don't change the topic"
  assert_equal "Don't change the topic", topic.send(:read_attribute, "title")
  assert_equal "Don't change the topic", topic["title"]

  assert_equal "Don't change the topic", topic.send(:read_attribute, :title)
  assert_equal "Don't change the topic", topic[:title]
end
test_read_attribute_when_false()
# File rails/activerecord/test/base_test.rb, line 253
def test_read_attribute_when_false
  topic = topics(:first)
  topic.approved = false
  assert !topic.approved?, "approved should be false"
  topic.approved = "false"
  assert !topic.approved?, "approved should be false"
end
test_read_attribute_when_true()
# File rails/activerecord/test/base_test.rb, line 261
def test_read_attribute_when_true
  topic = topics(:first)
  topic.approved = true
  assert topic.approved?, "approved should be true"
  topic.approved = "true"
  assert topic.approved?, "approved should be true"
end
test_read_write_boolean_attribute()
# File rails/activerecord/test/base_test.rb, line 269
def test_read_write_boolean_attribute
  topic = Topic.new
  # puts ""
  # puts "New Topic"
  # puts topic.inspect
  topic.approved = "false"
  # puts "Expecting false"
  # puts topic.inspect
  assert !topic.approved?, "approved should be false"
  topic.approved = "false"
  # puts "Expecting false"
  # puts topic.inspect
  assert !topic.approved?, "approved should be false"
  topic.approved = "true"
  # puts "Expecting true"
  # puts topic.inspect
  assert topic.approved?, "approved should be true"
  topic.approved = "true"
  # puts "Expecting true"
  # puts topic.inspect
  assert topic.approved?, "approved should be true"
  # puts ""
end
test_reader_for_invalid_column_names()
# File rails/activerecord/test/base_test.rb, line 306
def test_reader_for_invalid_column_names
  # column names which aren't legal ruby ids
  topic = Topic.find(:first)
  topic.send(:define_read_method, "mumub-jumbo".to_sym, "mumub-jumbo", nil)
  assert !Topic.read_methods.include?("mumub-jumbo")
end
test_reader_generation()
# File rails/activerecord/test/base_test.rb, line 293
def test_reader_generation
  Topic.find(:first).title
  Firm.find(:first).name
  Client.find(:first).name
  if ActiveRecord::Base.generate_read_methods
    assert_readers(Topic,  %w(type replies_count))
    assert_readers(Firm,   %w(type))
    assert_readers(Client, %w(type ruby_type rating?))
  else
    [Topic, Firm, Client].each {|klass| assert_equal klass.read_methods, {}}
  end
end
test_record_not_found_exception()
# File rails/activerecord/test/base_test.rb, line 364
def test_record_not_found_exception
  assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
end
test_reload()
# File rails/activerecord/test/base_test.rb, line 1188
def test_reload
  t1 = Topic.find(1)
  t2 = Topic.find(1)
  t1.title = "something else"
  t1.save
  t2.reload
  assert_equal t1.title, t2.title
end
test_respond_to?()
# File rails/activerecord/test/base_test.rb, line 88
def test_respond_to?
  topic = Topic.find(1)
  assert topic.respond_to?("title")
  assert topic.respond_to?("title?")
  assert topic.respond_to?("title=")
  assert topic.respond_to?(:title)
  assert topic.respond_to?(:title?)
  assert topic.respond_to?(:title=)
  assert topic.respond_to?("author_name")
  assert topic.respond_to?("attribute_names")
  assert !topic.respond_to?("nothingness")
  assert !topic.respond_to?(:nothingness)
end
test_save!()
# File rails/activerecord/test/base_test.rb, line 152
def test_save!
  topic = Topic.new(:title => "New Topic")
  assert topic.save!
  
  reply = Reply.new
  assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
end
test_save_nil_string_attributes()
# File rails/activerecord/test/base_test.rb, line 169
def test_save_nil_string_attributes
  topic = Topic.find(1)
  topic.title = nil
  topic.save!
  topic.reload
  assert_nil topic.title
end
test_save_null_string_attributes()
# File rails/activerecord/test/base_test.rb, line 160
def test_save_null_string_attributes
  topic = Topic.find(1)
  topic.attributes = { "title" => "null", "author_name" => "null" }
  topic.save!
  topic.reload
  assert_equal("null", topic.title)
  assert_equal("null", topic.author_name)
end
test_scoped_find_conditions()
# File rails/activerecord/test/base_test.rb, line 1316
def test_scoped_find_conditions
  scoped_developers = Developer.with_scope(:find => { :conditions => 'salary > 90000' }) do
    Developer.find(:all, :conditions => 'id < 5')
  end
  assert !scoped_developers.include?(developers(:david)) # David's salary is less than 90,000
  assert_equal 3, scoped_developers.size
end
test_scoped_find_limit_offset()
# File rails/activerecord/test/base_test.rb, line 1324
def test_scoped_find_limit_offset
  scoped_developers = Developer.with_scope(:find => { :limit => 3, :offset => 2 }) do
    Developer.find(:all, :order => 'id')
  end    
  assert !scoped_developers.include?(developers(:david))
  assert !scoped_developers.include?(developers(:jamis))
  assert_equal 3, scoped_developers.size
  
  # Test without scoped find conditions to ensure we get the whole thing
  developers = Developer.find(:all, :order => 'id')
  assert_equal Developer.count, developers.size
end
test_scoped_find_limit_offset_including_has_many_association()
# File rails/activerecord/test/base_test.rb, line 1360
def test_scoped_find_limit_offset_including_has_many_association
  topics = Topic.with_scope(:find => {:limit => 1, :offset => 1, :include => :replies}) do
    Topic.find(:all, :order => "topics.id")
  end
  assert_equal 1, topics.size
  assert_equal 2, topics.first.id
end
test_scoped_find_order()
# File rails/activerecord/test/base_test.rb, line 1337
def test_scoped_find_order   
  # Test order in scope   
  scoped_developers = Developer.with_scope(:find => { :limit => 1, :order => 'salary DESC' }) do
    Developer.find(:all)
  end   
  assert_equal 'Jamis', scoped_developers.first.name
  assert scoped_developers.include?(developers(:jamis))
  # Test scope without order and order in find
  scoped_developers = Developer.with_scope(:find => { :limit => 1 }) do
    Developer.find(:all, :order => 'salary DESC')
  end   
  # Test scope order + find order, find has priority
  scoped_developers = Developer.with_scope(:find => { :limit => 3, :order => 'id DESC' }) do
    Developer.find(:all, :order => 'salary ASC')
  end
  assert scoped_developers.include?(developers(:poor_jamis))
  assert scoped_developers.include?(developers(:david))
  assert scoped_developers.include?(developers(:dev_10))
  # Test without scoped find conditions to ensure we get the right thing
  developers = Developer.find(:all, :order => 'id', :limit => 1)
  assert scoped_developers.include?(developers(:david))
end
test_scoped_find_order_including_has_many_association()
# File rails/activerecord/test/base_test.rb, line 1368
def test_scoped_find_order_including_has_many_association
  developers = Developer.with_scope(:find => { :order => 'developers.salary DESC', :include => :projects }) do
    Developer.find(:all)
  end
  assert developers.size >= 2
  for i in 1...developers.size
    assert developers[i-1].salary >= developers[i].salary
  end
end
test_serialized_attribute()
# File rails/activerecord/test/base_test.rb, line 1082
def test_serialized_attribute
  myobj = MyObject.new('value1', 'value2')
  topic = Topic.create("content" => myobj)  
  Topic.serialize("content", MyObject)
  assert_equal(myobj, topic.content)
end
test_serialized_attribute_with_class_constraint()
# File rails/activerecord/test/base_test.rb, line 1105
def test_serialized_attribute_with_class_constraint
  settings = { "color" => "blue" }
  Topic.serialize(:content, Hash)
  topic = Topic.new(:content => settings)
  assert topic.save
  assert_equal(settings, Topic.find(topic.id).content)
ensure
  Topic.serialize(:content)
end
test_set_attributes()
# File rails/activerecord/test/base_test.rb, line 64
def test_set_attributes
  topic = Topic.find(1)
  topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
  topic.save
  assert_equal("Budget", topic.title)
  assert_equal("Jason", topic.author_name)
  assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
end
test_set_attributes_with_block()
# File rails/activerecord/test/base_test.rb, line 78
def test_set_attributes_with_block
  topic = Topic.new do |t|
    t.title       = "Budget"
    t.author_name = "Jason"
  end

  assert_equal("Budget", topic.title)
  assert_equal("Jason", topic.author_name)
end
test_set_inheritance_column_with_block()
# File rails/activerecord/test/base_test.rb, line 1245
def test_set_inheritance_column_with_block
  k = Class.new( ActiveRecord::Base )
  k.set_inheritance_column { original_inheritance_column + "_id" }
  assert_equal "type_id", k.inheritance_column
end
test_set_inheritance_column_with_value()
# File rails/activerecord/test/base_test.rb, line 1237
def test_set_inheritance_column_with_value
  k = Class.new( ActiveRecord::Base )
  k.inheritance_column = "foo"
  assert_equal "foo", k.inheritance_column
  k.set_inheritance_column "bar"
  assert_equal "bar", k.inheritance_column
end
test_set_primary_key_with_block()
# File rails/activerecord/test/base_test.rb, line 1231
def test_set_primary_key_with_block
  k = Class.new( ActiveRecord::Base )
  k.set_primary_key { "sys_" + original_primary_key }
  assert_equal "sys_id", k.primary_key
end
test_set_primary_key_with_value()
# File rails/activerecord/test/base_test.rb, line 1223
def test_set_primary_key_with_value
  k = Class.new( ActiveRecord::Base )
  k.primary_key = "foo"
  assert_equal "foo", k.primary_key
  k.set_primary_key "bar"
  assert_equal "bar", k.primary_key
end
test_set_table_name_with_block()
# File rails/activerecord/test/base_test.rb, line 1217
def test_set_table_name_with_block
  k = Class.new( ActiveRecord::Base )
  k.set_table_name { "ks" }
  assert_equal "ks", k.table_name
end
test_set_table_name_with_value()
# File rails/activerecord/test/base_test.rb, line 1209
def test_set_table_name_with_value
  k = Class.new( ActiveRecord::Base )
  k.table_name = "foo"
  assert_equal "foo", k.table_name
  k.set_table_name "bar"
  assert_equal "bar", k.table_name
end
test_should_raise_exception_on_serialized_attribute_with_type_mismatch()
# File rails/activerecord/test/base_test.rb, line 1095
def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
  myobj = MyObject.new('value1', 'value2')
  topic = Topic.new(:content => myobj)
  assert topic.save
  Topic.serialize(:content, Hash)
  assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
ensure
  Topic.serialize(:content)
end
test_sql_injection_via_find()
# File rails/activerecord/test/base_test.rb, line 1056
def test_sql_injection_via_find
  assert_raises(ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid) do
    Topic.find("123456 OR id > 0")
  end
end
test_table_exists()
# File rails/activerecord/test/base_test.rb, line 59
def test_table_exists
  assert !NonExistentTable.table_exists?
  assert Topic.table_exists?
end
test_table_name_guesses()
# File rails/activerecord/test/base_test.rb, line 399
def test_table_name_guesses
  classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]

  assert_equal "topics", Topic.table_name

  assert_equal "categories", Category.table_name
  assert_equal "smarts", Smarts.table_name
  assert_equal "credit_cards", CreditCard.table_name
  assert_equal "credit_card_pin_numbers", CreditCard::PinNumber.table_name
  assert_equal "credit_card_pin_number_cvv_codes", CreditCard::PinNumber::CvvCode.table_name
  assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
  assert_equal "categories", CreditCard::Brand.table_name
  assert_equal "master_credit_cards", MasterCreditCard.table_name

  ActiveRecord::Base.pluralize_table_names = false
  classes.each(&:reset_table_name)

  assert_equal "category", Category.table_name
  assert_equal "smarts", Smarts.table_name
  assert_equal "credit_card", CreditCard.table_name
  assert_equal "credit_card_pin_number", CreditCard::PinNumber.table_name
  assert_equal "credit_card_pin_number_cvv_code", CreditCard::PinNumber::CvvCode.table_name
  assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
  assert_equal "category", CreditCard::Brand.table_name
  assert_equal "master_credit_card", MasterCreditCard.table_name

  ActiveRecord::Base.pluralize_table_names = true
  classes.each(&:reset_table_name)

  ActiveRecord::Base.table_name_prefix = "test_"
  Category.reset_table_name
  assert_equal "test_categories", Category.table_name
  ActiveRecord::Base.table_name_suffix = "_test"
  Category.reset_table_name
  assert_equal "test_categories_test", Category.table_name
  ActiveRecord::Base.table_name_prefix = ""
  Category.reset_table_name
  assert_equal "categories_test", Category.table_name
  ActiveRecord::Base.table_name_suffix = ""
  Category.reset_table_name
  assert_equal "categories", Category.table_name

  ActiveRecord::Base.pluralize_table_names = false
  ActiveRecord::Base.table_name_prefix = "test_"
  Category.reset_table_name
  assert_equal "test_category", Category.table_name
  ActiveRecord::Base.table_name_suffix = "_test"
  Category.reset_table_name
  assert_equal "test_category_test", Category.table_name
  ActiveRecord::Base.table_name_prefix = ""
  Category.reset_table_name
  assert_equal "category_test", Category.table_name
  ActiveRecord::Base.table_name_suffix = ""
  Category.reset_table_name
  assert_equal "category", Category.table_name

  ActiveRecord::Base.pluralize_table_names = true
  classes.each(&:reset_table_name)
end
test_to_param_should_return_string()
# File rails/activerecord/test/base_test.rb, line 1566
def test_to_param_should_return_string
  assert_kind_of String, Client.find(:first).to_param
end
test_to_xml()
# File rails/activerecord/test/base_test.rb, line 1444
def test_to_xml
  xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true)
  bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema
  written_on_in_current_timezone = topics(:first).written_on.xmlschema
  last_read_in_current_timezone = topics(:first).last_read.xmlschema
  assert_equal "<topic>", xml.first(7)
  assert xml.include?(%Q(<title>The First Topic</title>))
  assert xml.include?(%Q(<author-name>David</author-name>))
  assert xml.include?(%Q(<id type="integer">1</id>))
  assert xml.include?(%Q(<replies-count type="integer">1</replies-count>))
  assert xml.include?(%Q(<written-on type="datetime">#{written_on_in_current_timezone}</written-on>))
  assert xml.include?(%Q(<content>Have a nice day</content>))
  assert xml.include?(%Q(<author-email-address>david@loudthinking.com</author-email-address>))
  assert xml.match(%Q(<parent-id type="integer"></parent-id>))
  if current_adapter?(:SybaseAdapter, :SQLServerAdapter, :OracleAdapter)
    assert xml.include?(%Q(<last-read type="datetime">#{last_read_in_current_timezone}</last-read>))
  else
    assert xml.include?(%Q(<last-read type="date">2004-04-15</last-read>))
  end
  # Oracle and DB2 don't have true boolean or time-only fields
  unless current_adapter?(:OracleAdapter, :DB2Adapter)
    assert xml.include?(%Q(<approved type="boolean">false</approved>)), "Approved should be a boolean"
    assert xml.include?(%Q(<bonus-time type="datetime">#{bonus_time_in_current_timezone}</bonus-time>))
  end
end
test_to_xml_including_belongs_to_association()
# File rails/activerecord/test/base_test.rb, line 1512
def test_to_xml_including_belongs_to_association
  xml = companies(:first_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
  assert !xml.include?("<firm>")

  xml = companies(:second_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
  assert xml.include?("<firm>")
end
test_to_xml_including_has_many_association()
# File rails/activerecord/test/base_test.rb, line 1481
def test_to_xml_including_has_many_association
  xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count)
  assert_equal "<topic>", xml.first(7)
  assert xml.include?(%Q(<replies><reply>))
  assert xml.include?(%Q(<title>The Second Topic's of the day</title>))
end
test_to_xml_including_methods()
# File rails/activerecord/test/base_test.rb, line 1538
def test_to_xml_including_methods
  xml = Company.new.to_xml(:methods => :arbitrary_method, :skip_instruct => true)
  assert_equal "<company>", xml.first(9)
  assert xml.include?(%Q(<arbitrary-method>I am Jack's profound disappointment</arbitrary-method>))
end
test_to_xml_including_multiple_associations()
# File rails/activerecord/test/base_test.rb, line 1520
def test_to_xml_including_multiple_associations
  xml = companies(:first_firm).to_xml(:indent => 0, :skip_instruct => true, :include => [ :clients, :account ])
  assert_equal "<firm>", xml.first(6)
  assert xml.include?(%Q(<account>))
  assert xml.include?(%Q(<clients><client>))
end
test_to_xml_including_multiple_associations_with_options()
# File rails/activerecord/test/base_test.rb, line 1527
def test_to_xml_including_multiple_associations_with_options
  xml = companies(:first_firm).to_xml(
    :indent  => 0, :skip_instruct => true, 
    :include => { :clients => { :only => :name } }
  )
  
  assert_equal "<firm>", xml.first(6)
  assert xml.include?(%Q(<client><name>Summit</name></client>))
  assert xml.include?(%Q(<clients><client>))
end
test_to_xml_skipping_attributes()
# File rails/activerecord/test/base_test.rb, line 1470
def test_to_xml_skipping_attributes
  xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :replies_count])
  assert_equal "<topic>", xml.first(7)
  assert !xml.include?(%Q(<title>The First Topic</title>))
  assert xml.include?(%Q(<author-name>David</author-name>))    

  xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :author_name, :replies_count])
  assert !xml.include?(%Q(<title>The First Topic</title>))
  assert !xml.include?(%Q(<author-name>David</author-name>))    
end
test_toggle_attribute()
# File rails/activerecord/test/base_test.rb, line 1177
def test_toggle_attribute
  assert !topics(:first).approved?
  topics(:first).toggle!(:approved)
  assert topics(:first).approved?
  topic = topics(:first)
  topic.toggle(:approved)
  assert !topic.approved?
  topic.reload
  assert topic.approved?
end
test_type_name_with_module_should_handle_beginning()
# File rails/activerecord/test/base_test.rb, line 1561
def test_type_name_with_module_should_handle_beginning
  assert_equal 'ActiveRecord::Person', ActiveRecord::Base.send(:type_name_with_module, 'Person')
  assert_equal '::Person', ActiveRecord::Base.send(:type_name_with_module, '::Person')
end
test_update()
# File rails/activerecord/test/base_test.rb, line 207
def test_update
  topic = Topic.new
  topic.title = "Another New Topic"
  topic.written_on = "2003-12-12 23:23:00"
  topic.save
  topicReloaded = Topic.find(topic.id)
  assert_equal("Another New Topic", topicReloaded.title)

  topicReloaded.title = "Updated topic"
  topicReloaded.save
  
  topicReloadedAgain = Topic.find(topic.id)
  
  assert_equal("Updated topic", topicReloadedAgain.title)
end
test_update_all()
# File rails/activerecord/test/base_test.rb, line 498
def test_update_all
  # The ADO library doesn't support the number of affected rows
  return true if current_adapter?(:SQLServerAdapter)

  assert_equal 2, Topic.update_all("content = 'bulk updated!'")
  assert_equal "bulk updated!", Topic.find(1).content
  assert_equal "bulk updated!", Topic.find(2).content
  assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!'])
  assert_equal "bulk updated again!", Topic.find(1).content
  assert_equal "bulk updated again!", Topic.find(2).content
end
test_update_array_content()
# File rails/activerecord/test/base_test.rb, line 123
def test_update_array_content
  topic = Topic.new
  topic.content = %w( one two three )

  topic.content.push "four"
  assert_equal(%w( one two three four ), topic.content)

  topic.save
  
  topic = Topic.find(topic.id)
  topic.content << "five"
  assert_equal(%w( one two three four five ), topic.content)
end
test_update_attribute()
# File rails/activerecord/test/base_test.rb, line 663
def test_update_attribute
  assert !Topic.find(1).approved?
  Topic.find(1).update_attribute("approved", true)
  assert Topic.find(1).approved?

  Topic.find(1).update_attribute(:approved, false)
  assert !Topic.find(1).approved?
end
test_update_attributes()
# File rails/activerecord/test/base_test.rb, line 672
def test_update_attributes
  topic = Topic.find(1)
  assert !topic.approved?
  assert_equal "The First Topic", topic.title
  
  topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
  topic.reload
  assert topic.approved?
  assert_equal "The First Topic Updated", topic.title

  topic.update_attributes(:approved => false, :title => "The First Topic")
  topic.reload
  assert !topic.approved?
  assert_equal "The First Topic", topic.title
end
test_update_attributes!()
# File rails/activerecord/test/base_test.rb, line 688
def test_update_attributes!
  reply = Reply.find(2)
  assert_equal "The Second Topic's of the day", reply.title
  assert_equal "Have a nice day", reply.content
  
  reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening")
  reply.reload
  assert_equal "The Second Topic's of the day updated", reply.title
  assert_equal "Have a nice evening", reply.content
  
  reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day")
  reply.reload
  assert_equal "The Second Topic's of the day", reply.title
  assert_equal "Have a nice day", reply.content
  
  assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
end
test_update_by_condition()
# File rails/activerecord/test/base_test.rb, line 526
def test_update_by_condition
  Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
  assert_equal "Have a nice day", Topic.find(1).content
  assert_equal "bulk updated!", Topic.find(2).content
end
test_update_columns_not_equal_attributes()
# File rails/activerecord/test/base_test.rb, line 223
def test_update_columns_not_equal_attributes
  topic = Topic.new
  topic.title = "Still another topic"
  topic.save
  
  topicReloaded = Topic.find(topic.id)
  topicReloaded.title = "A New Topic"
  topicReloaded.send :write_attribute, 'does_not_exist', 'test'
  assert_nothing_raised { topicReloaded.save }
end
test_update_many()
# File rails/activerecord/test/base_test.rb, line 510
def test_update_many
  topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
  updated = Topic.update(topic_data.keys, topic_data.values)

  assert_equal 2, updated.size
  assert_equal "1 updated", Topic.find(1).content
  assert_equal "2 updated", Topic.find(2).content
end
test_utc_as_time_zone()
# File rails/activerecord/test/base_test.rb, line 596
def test_utc_as_time_zone
  Topic.default_timezone = :utc
  attributes = { "bonus_time" => "5:42:00AM" }
  topic = Topic.find(1)
  topic.attributes = attributes
  assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
  Topic.default_timezone = :local
end
test_utc_as_time_zone_and_new()
# File rails/activerecord/test/base_test.rb, line 605
def test_utc_as_time_zone_and_new
  Topic.default_timezone = :utc
  attributes = { "bonus_time(1i)"=>"2000",
                 "bonus_time(2i)"=>"1",
                 "bonus_time(3i)"=>"1",
                 "bonus_time(4i)"=>"10",
                 "bonus_time(5i)"=>"35",
                 "bonus_time(6i)"=>"50" }
  topic = Topic.new(attributes)
  assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
  Topic.default_timezone = :local
end
test_write_attribute()
# File rails/activerecord/test/base_test.rb, line 234
def test_write_attribute
  topic = Topic.new
  topic.send(:write_attribute, :title, "Still another topic")
  assert_equal "Still another topic", topic.title

  topic.send(:write_attribute, "title", "Still another topic: part 2")
  assert_equal "Still another topic: part 2", topic.title
end