2005-10-19 22:53:55 +00:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/topic'
|
2004-11-24 01:04:44 +00:00
|
|
|
require 'fixtures/reply'
|
|
|
|
require 'fixtures/company'
|
2005-05-19 16:39:50 +00:00
|
|
|
require 'fixtures/developer'
|
2005-01-11 00:45:26 +00:00
|
|
|
require 'fixtures/project'
|
2004-11-24 01:04:44 +00:00
|
|
|
require 'fixtures/default'
|
|
|
|
require 'fixtures/auto_id'
|
|
|
|
require 'fixtures/column_name'
|
2005-10-12 19:59:13 +00:00
|
|
|
require 'fixtures/subscriber'
|
|
|
|
require 'fixtures/keyboard'
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
class Category < ActiveRecord::Base; end
|
|
|
|
class Smarts < ActiveRecord::Base; end
|
|
|
|
class CreditCard < ActiveRecord::Base; end
|
|
|
|
class MasterCreditCard < ActiveRecord::Base; end
|
2005-04-30 15:45:15 +00:00
|
|
|
class Post < ActiveRecord::Base; end
|
2005-04-30 15:49:28 +00:00
|
|
|
class Computer < ActiveRecord::Base; end
|
2005-10-28 07:40:28 +00:00
|
|
|
class NonExistentTable < ActiveRecord::Base; end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
class LoosePerson < ActiveRecord::Base
|
|
|
|
attr_protected :credit_rating, :administrator
|
|
|
|
end
|
|
|
|
|
2005-04-30 13:24:43 +00:00
|
|
|
class LooseDescendant < LoosePerson
|
|
|
|
attr_protected :phone_number
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
class TightPerson < ActiveRecord::Base
|
|
|
|
attr_accessible :name, :address
|
|
|
|
end
|
|
|
|
|
2005-04-30 13:24:43 +00:00
|
|
|
class TightDescendant < TightPerson
|
2004-11-24 01:04:44 +00:00
|
|
|
attr_accessible :phone_number
|
|
|
|
end
|
|
|
|
|
|
|
|
class Booleantest < ActiveRecord::Base; end
|
|
|
|
|
2005-06-28 17:15:01 +00:00
|
|
|
class Task < ActiveRecord::Base
|
|
|
|
attr_protected :starting
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
class BasicsTest < Test::Unit::TestCase
|
2005-05-19 16:39:50 +00:00
|
|
|
fixtures :topics, :companies, :developers, :projects, :computers
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-10-28 07:40:28 +00:00
|
|
|
def test_table_exists
|
|
|
|
assert !NonExistentTable.table_exists?
|
|
|
|
assert Topic.table_exists?
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
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)
|
2005-06-10 14:58:02 +00:00
|
|
|
assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_integers_as_nil
|
2005-10-06 17:13:24 +00:00
|
|
|
test = AutoId.create('value' => '')
|
|
|
|
assert_nil AutoId.find(test.id).value
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2005-01-11 00:45:26 +00:00
|
|
|
|
2005-10-27 08:18:41 +00:00
|
|
|
def test_case_sensitive_attributes_hash
|
|
|
|
# DB2 is not case-sensitive
|
|
|
|
return true if current_adapter?(:DB2Adapter)
|
|
|
|
|
2005-07-03 08:52:59 +00:00
|
|
|
assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
|
2005-01-11 00:45:26 +00:00
|
|
|
end
|
2005-04-02 08:36:32 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_create
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "New Topic"
|
|
|
|
topic.save
|
2005-04-02 08:36:32 +00:00
|
|
|
topicReloaded = Topic.find(topic.id)
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal("New Topic", topicReloaded.title)
|
|
|
|
end
|
2005-01-25 12:45:01 +00:00
|
|
|
|
|
|
|
def test_create_many
|
|
|
|
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
|
|
|
|
assert_equal 2, topics.size
|
|
|
|
assert_equal "first", topics.first.title
|
|
|
|
end
|
2005-01-24 11:20:47 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_create_through_factory
|
|
|
|
topic = Topic.create("title" => "New Topic")
|
|
|
|
topicReloaded = Topic.find(topic.id)
|
|
|
|
assert_equal(topic, topicReloaded)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "Another New Topic"
|
2005-01-01 19:22:16 +00:00
|
|
|
topic.written_on = "2003-12-12 23:23:00"
|
2004-11-24 01:04:44 +00:00
|
|
|
topic.save
|
2005-04-02 08:36:32 +00:00
|
|
|
topicReloaded = Topic.find(topic.id)
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal("Another New Topic", topicReloaded.title)
|
|
|
|
|
|
|
|
topicReloaded.title = "Updated topic"
|
|
|
|
topicReloaded.save
|
|
|
|
|
2005-04-02 08:36:32 +00:00
|
|
|
topicReloadedAgain = Topic.find(topic.id)
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
assert_equal("Updated topic", topicReloadedAgain.title)
|
|
|
|
end
|
|
|
|
|
2005-01-24 11:20:47 +00:00
|
|
|
def test_update_columns_not_equal_attributes
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "Still another topic"
|
|
|
|
topic.save
|
|
|
|
|
2005-04-02 08:36:32 +00:00
|
|
|
topicReloaded = Topic.find(topic.id)
|
2005-01-24 11:20:47 +00:00
|
|
|
topicReloaded.title = "A New Topic"
|
|
|
|
topicReloaded.send :write_attribute, 'does_not_exist', 'test'
|
|
|
|
assert_nothing_raised { topicReloaded.save }
|
|
|
|
end
|
2005-07-03 09:51:24 +00:00
|
|
|
|
|
|
|
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
|
2005-01-24 11:20:47 +00:00
|
|
|
|
2005-09-02 08:31:11 +00:00
|
|
|
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
|
|
|
|
|
2005-07-05 12:38:06 +00:00
|
|
|
def test_read_attribute_when_false
|
|
|
|
topic = topics(:first)
|
|
|
|
topic.approved = false
|
2005-10-06 17:13:24 +00:00
|
|
|
assert !topic.approved?, "approved should be false"
|
2005-07-05 12:38:06 +00:00
|
|
|
end
|
|
|
|
|
2005-10-07 00:53:05 +00:00
|
|
|
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))
|
|
|
|
else
|
|
|
|
[Topic, Firm, Client].each {|klass| assert_equal klass.read_methods, {}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-22 16:43:39 +00:00
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_preserving_date_objects
|
|
|
|
# SQL Server doesn't have a separate column type just for dates, so all are returned as time
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter)
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
assert_kind_of(
|
|
|
|
Date, Topic.find(1).last_read,
|
|
|
|
"The last_read attribute should be of the Date class"
|
|
|
|
)
|
2005-10-27 08:18:41 +00:00
|
|
|
end
|
2004-12-01 13:18:51 +00:00
|
|
|
|
2005-10-27 08:18:41 +00:00
|
|
|
def test_preserving_time_objects
|
2004-12-01 13:18:51 +00:00
|
|
|
assert_kind_of(
|
|
|
|
Time, Topic.find(1).bonus_time,
|
|
|
|
"The bonus_time attribute should be of the Time class"
|
|
|
|
)
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
assert_kind_of(
|
|
|
|
Time, Topic.find(1).written_on,
|
|
|
|
"The written_on attribute should be of the Time class"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "Yet Another New Topic"
|
2005-01-01 19:22:16 +00:00
|
|
|
topic.written_on = "2003-12-12 23:23:00"
|
2004-11-24 01:04:44 +00:00
|
|
|
topic.save
|
|
|
|
topic.destroy
|
2005-04-02 08:36:32 +00:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2005-08-14 08:58:53 +00:00
|
|
|
def test_destroy_returns_self
|
|
|
|
topic = Topic.new("title" => "Yet Another Title")
|
|
|
|
assert topic.save
|
|
|
|
assert_equal topic, topic.destroy, "destroy did not return destroyed object"
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_record_not_found_exception
|
2005-02-07 13:47:14 +00:00
|
|
|
assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2005-03-06 14:11:26 +00:00
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_load
|
2005-06-26 11:25:32 +00:00
|
|
|
topics = Topic.find(:all, :order => 'id')
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal(2, topics.size)
|
2005-06-10 14:58:02 +00:00
|
|
|
assert_equal(topics(:first).title, topics.first.title)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_condition
|
2005-06-26 11:25:32 +00:00
|
|
|
topics = Topic.find(:all, :conditions => "author_name = 'Mary'")
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
assert_equal(1, topics.size)
|
2005-06-10 14:58:02 +00:00
|
|
|
assert_equal(topics(:second).title, topics.first.title)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_table_name_guesses
|
|
|
|
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 "master_credit_cards", MasterCreditCard.table_name
|
|
|
|
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
2005-10-10 18:59:56 +00:00
|
|
|
[Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name}
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "category", Category.table_name
|
|
|
|
assert_equal "smarts", Smarts.table_name
|
|
|
|
assert_equal "credit_card", CreditCard.table_name
|
|
|
|
assert_equal "master_credit_card", MasterCreditCard.table_name
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
2005-10-10 18:59:56 +00:00
|
|
|
[Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name}
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.table_name_prefix = "test_"
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "test_categories", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_suffix = "_test"
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "test_categories_test", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_prefix = ""
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "categories_test", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_suffix = ""
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "categories", Category.table_name
|
|
|
|
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
ActiveRecord::Base.table_name_prefix = "test_"
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "test_category", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_suffix = "_test"
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "test_category_test", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_prefix = ""
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "category_test", Category.table_name
|
|
|
|
ActiveRecord::Base.table_name_suffix = ""
|
2005-10-10 18:59:56 +00:00
|
|
|
Category.reset_table_name
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "category", Category.table_name
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
2005-10-10 18:59:56 +00:00
|
|
|
[Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name}
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_all
|
2005-06-26 11:25:32 +00:00
|
|
|
assert_equal 2, Topic.count
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
Topic.destroy_all "author_name = 'Mary'"
|
2005-06-26 11:25:32 +00:00
|
|
|
assert_equal 1, Topic.count
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2005-01-25 12:45:01 +00:00
|
|
|
|
|
|
|
def test_destroy_many
|
2005-06-13 12:03:33 +00:00
|
|
|
assert_equal 3, Client.count
|
2005-01-25 12:45:01 +00:00
|
|
|
Client.destroy([2, 3])
|
2005-06-13 12:03:33 +00:00
|
|
|
assert_equal 1, Client.count
|
2005-01-25 12:45:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_many
|
|
|
|
Topic.delete([1, 2])
|
|
|
|
assert_equal 0, Topic.count
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_boolean_attributes
|
|
|
|
assert ! Topic.find(1).approved?
|
|
|
|
assert Topic.find(2).approved?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_increment_counter
|
|
|
|
Topic.increment_counter("replies_count", 1)
|
|
|
|
assert_equal 1, Topic.find(1).replies_count
|
|
|
|
|
|
|
|
Topic.increment_counter("replies_count", 1)
|
|
|
|
assert_equal 2, Topic.find(1).replies_count
|
|
|
|
end
|
|
|
|
|
|
|
|
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 0, Topic.find(1).replies_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_all
|
2005-07-01 17:20:04 +00:00
|
|
|
# The ADO library doesn't support the number of affected rows
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter)
|
|
|
|
|
2004-12-19 16:21:55 +00:00
|
|
|
assert_equal 2, Topic.update_all("content = 'bulk updated!'")
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "bulk updated!", Topic.find(1).content
|
|
|
|
assert_equal "bulk updated!", Topic.find(2).content
|
2005-01-24 13:06:12 +00:00
|
|
|
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
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2004-12-19 16:21:55 +00:00
|
|
|
|
2005-01-25 12:45:01 +00:00
|
|
|
def test_update_many
|
2005-07-17 09:52:00 +00:00
|
|
|
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
|
2005-01-25 12:45:01 +00:00
|
|
|
updated = Topic.update(topic_data.keys, topic_data.values)
|
2005-01-24 14:13:10 +00:00
|
|
|
|
|
|
|
assert_equal 2, updated.size
|
|
|
|
assert_equal "1 updated", Topic.find(1).content
|
|
|
|
assert_equal "2 updated", Topic.find(2).content
|
|
|
|
end
|
|
|
|
|
2004-12-19 16:21:55 +00:00
|
|
|
def test_delete_all
|
2005-07-01 17:20:04 +00:00
|
|
|
# The ADO library doesn't support the number of affected rows
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter)
|
|
|
|
|
2004-12-19 16:21:55 +00:00
|
|
|
assert_equal 2, Topic.delete_all
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_update_by_condition
|
2005-10-06 04:15:14 +00:00
|
|
|
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal "Have a nice day", Topic.find(1).content
|
|
|
|
assert_equal "bulk updated!", Topic.find(2).content
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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"
|
2005-04-02 08:36:32 +00:00
|
|
|
assert_raise(NoMethodError) { t.title2 }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_null_fields
|
|
|
|
assert_nil Topic.find(1).parent_id
|
|
|
|
assert_nil Topic.create("title" => "Hey you").parent_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_values
|
|
|
|
topic = Topic.new
|
2005-10-06 17:13:24 +00:00
|
|
|
assert topic.approved?
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_nil topic.written_on
|
2004-12-01 13:18:51 +00:00
|
|
|
assert_nil topic.bonus_time
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_nil topic.last_read
|
|
|
|
|
|
|
|
topic.save
|
|
|
|
|
|
|
|
topic = Topic.find(topic.id)
|
2005-10-06 17:13:24 +00:00
|
|
|
assert topic.approved?
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_nil topic.last_read
|
|
|
|
end
|
2004-12-28 17:30:17 +00:00
|
|
|
|
2005-10-27 08:18:41 +00:00
|
|
|
def test_utc_as_time_zone
|
|
|
|
# Oracle and SQLServer do not have a TIME datatype.
|
|
|
|
return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter)
|
|
|
|
|
|
|
|
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
|
2004-12-28 17:30:17 +00:00
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
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
|
|
|
|
assert_nil topic.approved
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_equality
|
|
|
|
assert_equal Topic.find(1), Topic.find(2).parent
|
|
|
|
end
|
|
|
|
|
2005-09-09 08:54:02 +00:00
|
|
|
def test_equality_of_new_records
|
|
|
|
assert_not_equal Topic.new, Topic.new
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_hashing
|
|
|
|
assert_equal [ Topic.find(1) ], [ Topic.find(2).parent ] & [ Topic.find(1) ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_new_record
|
|
|
|
client = Client.new
|
|
|
|
client.destroy
|
|
|
|
assert client.frozen?
|
|
|
|
end
|
|
|
|
|
2005-05-02 16:46:30 +00:00
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_update_attribute
|
|
|
|
assert !Topic.find(1).approved?
|
|
|
|
Topic.find(1).update_attribute("approved", true)
|
|
|
|
assert Topic.find(1).approved?
|
2005-01-06 02:36:33 +00:00
|
|
|
|
|
|
|
Topic.find(1).update_attribute(:approved, false)
|
|
|
|
assert !Topic.find(1).approved?
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mass_assignment_protection
|
|
|
|
firm = Firm.new
|
|
|
|
firm.attributes = { "name" => "Next Angle", "rating" => 5 }
|
|
|
|
assert_equal 1, firm.rating
|
|
|
|
end
|
2005-10-12 19:59:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-01-23 17:24:54 +00:00
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_mass_assignment_accessible
|
2005-10-16 03:45:39 +00:00
|
|
|
reply = Reply.new("title" => "hello", "content" => "world", "approved" => true)
|
2004-11-24 01:04:44 +00:00
|
|
|
reply.save
|
2005-10-06 17:13:24 +00:00
|
|
|
|
|
|
|
assert reply.approved?
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-10-06 17:13:24 +00:00
|
|
|
reply.approved = false
|
2004-11-24 01:04:44 +00:00
|
|
|
reply.save
|
|
|
|
|
2005-10-06 17:13:24 +00:00
|
|
|
assert !reply.approved?
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mass_assignment_protection_inheritance
|
2005-04-30 13:24:43 +00:00
|
|
|
assert_nil LoosePerson.accessible_attributes
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal [ :credit_rating, :administrator ], LoosePerson.protected_attributes
|
2005-04-30 13:24:43 +00:00
|
|
|
|
|
|
|
assert_nil LooseDescendant.accessible_attributes
|
|
|
|
assert_equal [ :credit_rating, :administrator, :phone_number ], LooseDescendant.protected_attributes
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_nil TightPerson.protected_attributes
|
2005-04-30 13:24:43 +00:00
|
|
|
assert_equal [ :name, :address ], TightPerson.accessible_attributes
|
|
|
|
|
|
|
|
assert_nil TightDescendant.protected_attributes
|
|
|
|
assert_equal [ :name, :address, :phone_number ], TightDescendant.accessible_attributes
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiparameter_attributes_on_date
|
|
|
|
# SQL Server doesn't have a separate column type just for dates, so all are returned as time
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter)
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
|
|
|
|
topic = Topic.find(1)
|
|
|
|
topic.attributes = attributes
|
2005-10-26 12:57:11 +00:00
|
|
|
# note that extra #to_date call allows test to pass for Oracle, which
|
|
|
|
# treats dates/times the same
|
|
|
|
assert_equal Date.new(2004, 6, 24).to_s, topic.last_read.to_date.to_s
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiparameter_attributes_on_date_with_empty_date
|
|
|
|
# SQL Server doesn't have a separate column type just for dates, so all are returned as time
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter)
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" }
|
|
|
|
topic = Topic.find(1)
|
|
|
|
topic.attributes = attributes
|
2005-10-26 12:57:11 +00:00
|
|
|
# note that extra #to_date call allows test to pass for Oracle, which
|
|
|
|
# treats dates/times the same
|
|
|
|
assert_equal Date.new(2004, 6, 1).to_s, topic.last_read.to_date.to_s
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2005-06-28 17:15:01 +00:00
|
|
|
def test_multiparameter_mass_assignment_protector
|
|
|
|
task = Task.new
|
2005-07-24 14:01:35 +00:00
|
|
|
time = Time.mktime(2000, 1, 1, 1)
|
2005-06-28 17:15:01 +00:00
|
|
|
task.starting = time
|
|
|
|
attributes = { "starting(1i)" => "2004", "starting(2i)" => "6", "starting(3i)" => "24" }
|
|
|
|
task.attributes = attributes
|
|
|
|
assert_equal time, task.starting
|
|
|
|
end
|
|
|
|
|
2004-12-01 13:18:51 +00:00
|
|
|
def test_attributes_on_dummy_time
|
2005-10-29 18:40:49 +00:00
|
|
|
# Oracle and SQL Server do not have a TIME datatype.
|
2005-10-27 08:18:41 +00:00
|
|
|
return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter)
|
2005-02-07 14:06:00 +00:00
|
|
|
|
2004-12-01 13:18:51 +00:00
|
|
|
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
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
def test_clone
|
|
|
|
topic = Topic.find(1)
|
2005-05-19 16:39:50 +00:00
|
|
|
cloned_topic = nil
|
|
|
|
assert_nothing_raised { cloned_topic = topic.clone }
|
2004-11-24 01:04:44 +00:00
|
|
|
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"]
|
2005-02-07 13:43:44 +00:00
|
|
|
|
|
|
|
cloned_topic.save
|
|
|
|
assert !cloned_topic.new_record?
|
|
|
|
assert cloned_topic.id != topic.id
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2005-05-19 16:39:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_clone_preserves_subtype
|
|
|
|
clone = nil
|
|
|
|
assert_nothing_raised { clone = Company.find(3).clone }
|
|
|
|
assert_kind_of Client, clone
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_bignum
|
|
|
|
company = Company.find(1)
|
|
|
|
company.rating = 2147483647
|
|
|
|
company.save
|
|
|
|
company = Company.find(1)
|
|
|
|
assert_equal 2147483647, company.rating
|
|
|
|
end
|
|
|
|
|
2005-09-27 23:37:57 +00:00
|
|
|
# TODO: extend defaults tests to other databases!
|
2005-10-27 08:18:41 +00:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2005-09-27 23:37:57 +00:00
|
|
|
def test_default
|
2004-11-24 01:04:44 +00:00
|
|
|
default = Default.new
|
|
|
|
|
2005-09-27 23:37:57 +00:00
|
|
|
# CURRENT_TIMESTAMP and NOW() timestamps
|
2004-11-24 01:04:44 +00:00
|
|
|
time_format = "%m/%d/%Y %H:%M"
|
2005-09-27 23:37:57 +00:00
|
|
|
now = Time.now.strftime(time_format)
|
|
|
|
assert_equal now, default.modified_time.strftime(time_format)
|
|
|
|
assert_equal now, default.modified_time_function.strftime(time_format)
|
|
|
|
|
|
|
|
# CURRENT_DATE and NOW() dates
|
|
|
|
today = Date.today
|
|
|
|
assert_equal today, default.modified_date
|
|
|
|
assert_equal today, default.modified_date_function
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
# 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
|
2005-10-09 00:52:25 +00:00
|
|
|
|
|
|
|
class Geometric < ActiveRecord::Base; end
|
|
|
|
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
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_auto_id
|
|
|
|
auto = AutoId.new
|
|
|
|
auto.save
|
|
|
|
assert (auto.id > 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def quote_column_name(name)
|
|
|
|
"<#{name}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_column_name_properly_quoted
|
|
|
|
col_record = ColumnName.new
|
|
|
|
col_record.references = 40
|
|
|
|
col_record.save
|
|
|
|
col_record.references = 41
|
|
|
|
col_record.save
|
|
|
|
c2 = ColumnName.find(col_record.id)
|
|
|
|
assert_equal(41, c2.references)
|
|
|
|
end
|
|
|
|
|
|
|
|
MyObject = Struct.new :attribute1, :attribute2
|
|
|
|
|
|
|
|
def test_serialized_attribute
|
|
|
|
myobj = MyObject.new('value1', 'value2')
|
|
|
|
topic = Topic.create("content" => myobj)
|
|
|
|
Topic.serialize("content", MyObject)
|
|
|
|
assert_equal(myobj, topic.content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialized_attribute_with_class_constraint
|
|
|
|
myobj = MyObject.new('value1', 'value2')
|
|
|
|
topic = Topic.create("content" => myobj)
|
|
|
|
Topic.serialize(:content, Hash)
|
|
|
|
|
2005-04-02 08:36:32 +00:00
|
|
|
assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
settings = { "color" => "blue" }
|
|
|
|
Topic.find(topic.id).update_attribute("content", settings)
|
|
|
|
assert_equal(settings, Topic.find(topic.id).content)
|
|
|
|
Topic.serialize(:content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quote
|
2005-09-27 09:25:17 +00:00
|
|
|
author_name = "\\ \001 ' \n \\n \""
|
|
|
|
topic = Topic.create('author_name' => author_name)
|
|
|
|
assert_equal author_name, Topic.find(topic.id).author_name
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2004-12-17 21:36:13 +00:00
|
|
|
|
|
|
|
def test_class_level_destroy
|
|
|
|
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
2005-04-02 08:36:32 +00:00
|
|
|
Topic.find(1).replies << should_be_destroyed_reply
|
2004-12-17 21:36:13 +00:00
|
|
|
|
|
|
|
Topic.destroy(1)
|
2005-04-02 08:36:32 +00:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
|
2004-12-17 21:36:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_level_delete
|
|
|
|
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
2005-04-02 08:36:32 +00:00
|
|
|
Topic.find(1).replies << should_be_destroyed_reply
|
2004-12-17 21:36:13 +00:00
|
|
|
|
|
|
|
Topic.delete(1)
|
2005-04-02 08:36:32 +00:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
2004-12-17 21:36:13 +00:00
|
|
|
assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
|
|
|
|
end
|
2005-01-06 02:31:35 +00:00
|
|
|
|
|
|
|
def test_increment_attribute
|
2005-06-10 14:58:02 +00:00
|
|
|
assert_equal 0, topics(:first).replies_count
|
|
|
|
topics(:first).increment! :replies_count
|
|
|
|
assert_equal 1, topics(:first, :reload).replies_count
|
2005-01-06 02:31:35 +00:00
|
|
|
|
2005-06-10 14:58:02 +00:00
|
|
|
topics(:first).increment(:replies_count).increment!(:replies_count)
|
|
|
|
assert_equal 3, topics(:first, :reload).replies_count
|
2005-01-06 02:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_increment_nil_attribute
|
2005-06-10 14:58:02 +00:00
|
|
|
assert_nil topics(:first).parent_id
|
|
|
|
topics(:first).increment! :parent_id
|
|
|
|
assert_equal 1, topics(:first).parent_id
|
2005-01-06 02:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_decrement_attribute
|
2005-06-10 14:58:02 +00:00
|
|
|
topics(:first).increment(:replies_count).increment!(:replies_count)
|
|
|
|
assert_equal 2, topics(:first).replies_count
|
2005-01-06 02:31:35 +00:00
|
|
|
|
2005-06-10 14:58:02 +00:00
|
|
|
topics(:first).decrement!(:replies_count)
|
|
|
|
assert_equal 1, topics(:first, :reload).replies_count
|
2005-01-06 02:31:35 +00:00
|
|
|
|
2005-06-10 14:58:02 +00:00
|
|
|
topics(:first).decrement(:replies_count).decrement!(:replies_count)
|
|
|
|
assert_equal -1, topics(:first, :reload).replies_count
|
2005-01-06 02:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_toggle_attribute
|
2005-06-10 14:58:02 +00:00
|
|
|
assert !topics(:first).approved?
|
|
|
|
topics(:first).toggle!(:approved)
|
|
|
|
assert topics(:first).approved?
|
2005-10-06 04:15:14 +00:00
|
|
|
topic = topics(:first)
|
|
|
|
topic.toggle(:approved)
|
|
|
|
assert !topic.approved?
|
|
|
|
topic.reload
|
|
|
|
assert topic.approved?
|
2005-01-06 02:31:35 +00:00
|
|
|
end
|
2005-01-10 23:49:57 +00:00
|
|
|
|
|
|
|
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
|
2005-02-07 14:26:57 +00:00
|
|
|
|
|
|
|
def test_define_attr_method_with_value
|
|
|
|
k = Class.new( ActiveRecord::Base )
|
2005-02-24 12:00:42 +00:00
|
|
|
k.send(:define_attr_method, :table_name, "foo")
|
2005-02-07 14:26:57 +00:00
|
|
|
assert_equal "foo", k.table_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_define_attr_method_with_block
|
|
|
|
k = Class.new( ActiveRecord::Base )
|
2005-02-24 12:00:42 +00:00
|
|
|
k.send(:define_attr_method, :primary_key) { "sys_" + original_primary_key }
|
2005-02-07 14:26:57 +00:00
|
|
|
assert_equal "sys_id", k.primary_key
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_set_table_name_with_block
|
|
|
|
k = Class.new( ActiveRecord::Base )
|
|
|
|
k.set_table_name { "ks" }
|
|
|
|
assert_equal "ks", k.table_name
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2005-04-30 15:45:15 +00:00
|
|
|
|
|
|
|
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.type = 'Post'"
|
|
|
|
res2 = res + 1
|
|
|
|
assert_nothing_raised do
|
|
|
|
res2 = Post.count("posts.type = 'Post'",
|
|
|
|
"LEFT JOIN comments ON posts.id=comments.post_id")
|
|
|
|
end
|
|
|
|
assert_equal res, res2
|
|
|
|
end
|
|
|
|
|
2005-06-25 10:56:20 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2005-10-07 00:53:05 +00:00
|
|
|
|
2005-10-14 23:28:35 +00:00
|
|
|
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
|
|
|
|
|
2005-10-26 13:20:02 +00:00
|
|
|
def test_constrain_conditions
|
|
|
|
developers = Developer.constrain(:conditions => 'salary > 90000') do
|
|
|
|
Developer.find(:all, :conditions => 'id < 5')
|
|
|
|
end
|
|
|
|
david = Developer.find(1)
|
|
|
|
assert !developers.include?(david) # David's salary is less than 90,000
|
|
|
|
assert_equal 3, developers.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constrain_limit_offset
|
|
|
|
developers = Developer.constrain(:limit => 3, :offset => 2) do
|
|
|
|
Developer.find(:all, :order => 'id')
|
|
|
|
end
|
|
|
|
david = Developer.find(1)
|
|
|
|
jamis = Developer.find(1)
|
|
|
|
assert !developers.include?(david) # David has id 1
|
|
|
|
assert !developers.include?(jamis) # Jamis has id 2
|
|
|
|
assert_equal 3, developers.size
|
|
|
|
|
|
|
|
# Now test without constraints to make sure we get the whole thing
|
|
|
|
developers = Developer.find(:all, :order => 'id')
|
|
|
|
assert_equal 10, developers.size
|
|
|
|
end
|
|
|
|
|
2005-10-16 02:30:04 +00:00
|
|
|
# FIXME: this test ought to run, but it needs to run sandboxed so that it
|
|
|
|
# doesn't b0rk the current test environment by undefing everything.
|
|
|
|
#
|
|
|
|
#def test_dev_mode_memory_leak
|
|
|
|
# counts = []
|
|
|
|
# 2.times do
|
|
|
|
# require_dependency 'fixtures/company'
|
|
|
|
# Firm.find(:first)
|
|
|
|
# Dependencies.clear
|
|
|
|
# ActiveRecord::Base.reset_subclasses
|
|
|
|
# Dependencies.remove_subclasses_for(ActiveRecord::Base)
|
|
|
|
#
|
|
|
|
# GC.start
|
|
|
|
#
|
|
|
|
# count = 0
|
|
|
|
# ObjectSpace.each_object(Proc) { count += 1 }
|
|
|
|
# counts << count
|
|
|
|
# end
|
|
|
|
# assert counts.last <= counts.first,
|
|
|
|
# "expected last count (#{counts.last}) to be <= first count (#{counts.first})"
|
|
|
|
#end
|
2005-10-15 18:21:27 +00:00
|
|
|
|
2005-10-07 00:53:05 +00:00
|
|
|
private
|
|
|
|
def assert_readers(model, exceptions)
|
|
|
|
expected_readers = model.column_names - (model.serialized_attributes.keys + exceptions + ['id'])
|
2005-11-02 17:30:59 +00:00
|
|
|
assert_equal expected_readers.sort, model.read_methods.to_a.sort
|
2005-10-07 00:53:05 +00:00
|
|
|
end
|
2005-10-29 18:40:49 +00:00
|
|
|
end
|