2008-01-21 17:20:51 +00:00
require " cases/helper "
2008-03-28 20:21:20 +00:00
require 'models/author'
2008-01-18 07:31:37 +00:00
require 'models/topic'
require 'models/reply'
2008-03-28 20:21:20 +00:00
require 'models/category'
2008-01-18 07:31:37 +00:00
require 'models/company'
require 'models/customer'
require 'models/developer'
require 'models/project'
require 'models/default'
require 'models/auto_id'
require 'models/column_name'
require 'models/subscriber'
require 'models/keyboard'
require 'models/post'
2008-03-28 11:55:01 +00:00
require 'models/comment'
2008-01-18 07:31:37 +00:00
require 'models/minimalistic'
require 'models/warehouse_thing'
2008-12-22 15:18:43 +00:00
require 'models/parrot'
2007-11-14 18:59:05 +00:00
require 'rexml/document'
2004-11-24 01:04:44 +00:00
class Category < ActiveRecord :: Base ; end
2008-06-26 16:46:33 +00:00
class Categorization < ActiveRecord :: Base ; end
2004-11-24 01:04:44 +00:00
class Smarts < ActiveRecord :: Base ; end
2006-08-16 09:46:43 +00:00
class CreditCard < ActiveRecord :: Base
2006-08-25 15:25:08 +00:00
class PinNumber < ActiveRecord :: Base
class CvvCode < ActiveRecord :: Base ; end
class SubCvvCode < CvvCode ; end
end
class SubPinNumber < PinNumber ; end
class Brand < Category ; end
2006-08-16 09:46:43 +00:00
end
2004-11-24 01:04:44 +00:00
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
2006-03-01 16:01:53 +00:00
class TestOracleDefault < ActiveRecord :: Base ; end
2004-11-24 01:04:44 +00:00
class LoosePerson < ActiveRecord :: Base
2006-12-01 21:24:47 +00:00
self . table_name = 'people'
2006-03-16 02:46:01 +00:00
self . abstract_class = true
2006-12-01 21:24:47 +00:00
attr_protected :credit_rating , :administrator
2004-11-24 01:04:44 +00:00
end
2005-04-30 13:24:43 +00:00
class LooseDescendant < LoosePerson
attr_protected :phone_number
end
2007-11-28 20:13:17 +00:00
class LooseDescendantSecond < LoosePerson
attr_protected :phone_number
attr_protected :name
end
2004-11-24 01:04:44 +00:00
class TightPerson < ActiveRecord :: Base
2006-12-01 21:24:47 +00:00
self . table_name = 'people'
2004-11-24 01:04:44 +00:00
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
2007-09-30 07:09:44 +00:00
class ReadonlyTitlePost < Post
attr_readonly :title
end
2004-11-24 01:04:44 +00:00
class Booleantest < ActiveRecord :: Base ; end
2005-06-28 17:15:01 +00:00
class Task < ActiveRecord :: Base
attr_protected :starting
end
2008-01-18 07:30:42 +00:00
class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord :: Base
self . table_name = 'topics'
2007-05-29 20:35:46 +00:00
attr_accessible :author_name
attr_protected :content
end
2008-01-21 17:20:51 +00:00
class BasicsTest < ActiveRecord :: TestCase
2008-09-10 10:39:50 +00:00
fixtures :topics , :companies , :developers , :projects , :computers , :accounts , :minimalistics , 'warehouse-things' , :authors , :categorizations , :categories , :posts
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
2008-01-18 07:30:42 +00:00
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
2007-01-16 01:39:05 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-02-19 21:01:10 +00:00
def test_read_attributes_before_type_cast
category = Category . new ( { :name = > " Test categoty " , :type = > nil } )
2008-06-26 16:46:33 +00:00
category_attrs = { " name " = > " Test categoty " , " type " = > nil , " categorizations_count " = > nil }
2008-02-19 21:01:10 +00:00
assert_equal category_attrs , category . attributes_before_type_cast
end
2008-06-03 18:32:53 +00:00
2008-02-20 05:30:16 +00:00
if current_adapter? ( :MysqlAdapter )
def test_read_attributes_before_type_cast_on_boolean
bool = Booleantest . create ( { " value " = > false } )
2008-08-18 22:56:37 +00:00
assert_equal " 0 " , bool . reload . attributes_before_type_cast [ " value " ]
2008-02-20 05:30:16 +00:00
end
2008-02-19 21:01:10 +00:00
end
2008-06-03 18:32:53 +00:00
2008-02-19 21:01:10 +00:00
def test_read_attributes_before_type_cast_on_datetime
developer = Developer . find ( :first )
assert_equal developer . created_at . to_s ( :db ) , developer . attributes_before_type_cast [ " created_at " ]
end
2008-06-03 18:32:53 +00:00
2004-11-24 01:04:44 +00:00
def test_hash_content
topic = Topic . new
topic . content = { " one " = > 1 , " two " = > 2 }
topic . save
assert_equal 2 , Topic . find ( topic . id ) . content [ " two " ]
2008-01-18 07:30:42 +00:00
2008-03-31 01:10:04 +00:00
topic . content_will_change!
2004-11-24 01:04:44 +00:00
topic . content [ " three " ] = 3
topic . save
assert_equal 3 , Topic . find ( topic . id ) . content [ " three " ]
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
topic = Topic . find ( topic . id )
topic . content << " five "
assert_equal ( %w( one two three four five ) , topic . content )
end
2008-01-18 07:30:42 +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
2006-01-14 08:26:20 +00:00
topic_reloaded = Topic . find ( topic . id )
assert_equal ( " New Topic " , topic_reloaded . title )
end
2008-01-18 07:30:42 +00:00
2006-03-15 02:32:14 +00:00
def test_save!
topic = Topic . new ( :title = > " New Topic " )
assert topic . save!
2008-01-18 07:30:42 +00:00
2006-10-09 01:52:24 +00:00
reply = Reply . new
assert_raise ( ActiveRecord :: RecordInvalid ) { reply . save! }
end
2007-01-16 01:39:05 +00:00
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 )
2007-01-28 17:51:21 +00:00
end
def test_save_nil_string_attributes
topic = Topic . find ( 1 )
topic . title = nil
topic . save!
topic . reload
assert_nil topic . title
end
2007-10-06 00:49:58 +00:00
def test_save_for_record_with_only_primary_key
minimalistic = Minimalistic . new
assert_nothing_raised { minimalistic . save }
end
def test_save_for_record_with_only_primary_key_that_is_provided
assert_nothing_raised { Minimalistic . create! ( :id = > 2 ) }
end
2006-01-14 08:26:20 +00:00
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
2006-01-14 08:43:27 +00:00
assert_equal new_topic_values [ :title ] , topic . title
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
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 )
2008-06-03 18:32:53 +00:00
end
2008-05-01 04:14:32 +00:00
def test_create_through_factory_with_block
topic = Topic . create ( " title " = > " New Topic " ) do | t |
t . author_name = " David "
end
topicReloaded = Topic . find ( topic . id )
assert_equal ( " New Topic " , topic . title )
assert_equal ( " David " , topic . author_name )
end
def test_create_many_through_factory_with_block
topics = Topic . create ( [ { " title " = > " first " } , { " title " = > " second " } ] ) do | t |
t . author_name = " David "
end
assert_equal 2 , topics . size
topic1 , topic2 = Topic . find ( topics [ 0 ] . id ) , Topic . find ( topics [ 1 ] . id )
assert_equal " first " , topic1 . title
assert_equal " David " , topic1 . author_name
assert_equal " second " , topic2 . title
assert_equal " David " , topic2 . author_name
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2005-04-02 08:36:32 +00:00
topicReloadedAgain = Topic . find ( topic . id )
2008-01-18 07:30:42 +00:00
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
2008-01-18 07:30:42 +00:00
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
2007-10-06 00:49:58 +00:00
def test_update_for_record_with_only_primary_key
minimalistic = minimalistics ( :first )
assert_nothing_raised { minimalistic . save }
end
2008-01-18 07:30:42 +00:00
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-11-19 09:53:36 +00:00
topic . approved = " false "
assert ! topic . approved? , " approved should be false "
end
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
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
2005-11-19 10:00:14 +00:00
assert ! topic . approved? , " approved should be false "
2005-11-19 09:53:36 +00:00
topic . approved = " false "
# puts "Expecting false"
# puts topic.inspect
2005-11-19 10:00:14 +00:00
assert ! topic . approved? , " approved should be false "
2005-11-19 09:53:36 +00:00
topic . approved = " true "
# puts "Expecting true"
# puts topic.inspect
2005-11-19 10:00:14 +00:00
assert topic . approved? , " approved should be true "
2005-11-19 09:53:36 +00:00
topic . approved = " true "
# puts "Expecting true"
# puts topic.inspect
2005-11-19 10:00:14 +00:00
assert topic . approved? , " approved should be true "
2005-11-19 09:53:36 +00:00
# puts ""
2005-07-05 12:38:06 +00:00
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
def test_query_attribute_string
[ nil , " " , " " ] . each do | value |
assert_equal false , Topic . new ( :author_name = > value ) . author_name?
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
assert_equal true , Topic . new ( :author_name = > " Name " ) . author_name?
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
def test_query_attribute_number
[ nil , 0 , " 0 " ] . each do | value |
assert_equal false , Developer . new ( :salary = > value ) . salary?
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
assert_equal true , Developer . new ( :salary = > 1 ) . salary?
assert_equal true , Developer . new ( :salary = > " 1 " ) . salary?
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
def test_query_attribute_boolean
[ nil , " " , false , " false " , " f " , 0 ] . each do | value |
assert_equal false , Topic . new ( :approved = > value ) . approved?
end
2008-01-18 07:30:42 +00:00
2006-11-20 11:22:38 +00:00
[ true , " true " , " 1 " , 1 ] . each do | value |
assert_equal true , Topic . new ( :approved = > value ) . approved?
end
end
2006-12-29 17:24:35 +00:00
def test_query_attribute_with_custom_fields
object = Company . find_by_sql ( <<-SQL).first
SELECT c1 . * , c2 . ruby_type as string_value , c2 . rating as int_value
FROM companies c1 , companies c2
WHERE c1 . firm_id = c2 . id
AND c1 . id = 2
SQL
assert_equal " Firm " , object . string_value
assert object . string_value?
object . string_value = " "
assert ! object . string_value?
2006-12-29 19:05:53 +00:00
assert_equal 1 , object . int_value . to_i
2006-12-29 17:24:35 +00:00
assert object . int_value?
object . int_value = " 0 "
assert ! object . int_value?
end
2005-10-07 00:53:05 +00:00
2005-11-19 06:47:26 +00:00
def test_reader_for_invalid_column_names
2007-08-14 08:53:02 +00:00
Topic . send ( :define_read_method , " mumub-jumbo " . to_sym , " mumub-jumbo " , nil )
assert ! Topic . generated_methods . include? ( " mumub-jumbo " )
2005-11-19 06:47:26 +00:00
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
2007-03-06 09:39:50 +00:00
if current_adapter? ( :SybaseAdapter , :OracleAdapter )
2006-03-02 01:15:41 +00:00
# Sybase ctlib does not (yet?) support the date type; use datetime instead.
2007-03-06 09:39:50 +00:00
# Oracle treats all dates/times as Time.
2006-03-02 01:15:41 +00:00
assert_kind_of (
2008-01-18 07:30:42 +00:00
Time , Topic . find ( 1 ) . last_read ,
2006-03-02 01:15:41 +00:00
" The last_read attribute should be of the Time class "
)
else
assert_kind_of (
2008-01-18 07:30:42 +00:00
Date , Topic . find ( 1 ) . last_read ,
2006-03-02 01:15:41 +00:00
" The last_read attribute should be of the Date class "
)
end
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 "
)
2006-06-25 18:04:06 +00:00
# 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
2006-07-07 17:40:22 +00:00
assert_equal 9900 , Topic . find ( 2 ) . written_on . usec
2006-06-25 18:04:06 +00:00
end
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2007-09-17 09:29:02 +00:00
def test_custom_mutator
topic = Topic . find ( 1 )
# This mutator is protected in the class definition
topic . send ( :approved = , true )
assert topic . instance_variable_get ( " @custom_approved " )
end
2006-08-01 07:33:17 +00:00
2008-09-21 21:01:32 +00:00
def test_delete
topic = Topic . find ( 1 )
assert_equal topic , topic . delete , 'topic.delete did not return self'
assert topic . frozen? , 'topic not frozen after delete'
assert_raise ( ActiveRecord :: RecordNotFound ) { Topic . find ( topic . id ) }
end
def test_delete_doesnt_run_callbacks
Topic . find ( 1 ) . delete
assert_not_nil Topic . find ( 2 )
end
2004-11-24 01:04:44 +00:00
def test_destroy
2006-08-01 07:33:17 +00:00
topic = Topic . find ( 1 )
assert_equal topic , topic . destroy , 'topic.destroy did not return self'
assert topic . frozen? , 'topic not frozen after 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
2006-08-01 07:33:17 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_initialize_with_attributes
2008-01-18 07:30:42 +00:00
topic = Topic . new ( {
2004-11-24 01:04:44 +00:00
" title " = > " initialized from attributes " , " written_on " = > " 2003-12-12 23:23 "
} )
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
assert_equal ( " initialized from attributes " , topic . title )
end
2008-01-18 07:30:42 +00:00
2005-03-06 14:11:26 +00:00
def test_initialize_with_invalid_attribute
begin
2008-01-18 07:30:42 +00:00
topic = Topic . new ( { " title " = > " test " ,
2005-03-06 14:11:26 +00:00
" 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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_load
2008-01-18 07:30:42 +00:00
topics = Topic . find ( :all , :order = > 'id' )
2008-03-24 02:50:02 +00:00
assert_equal ( 4 , 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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_load_with_condition
2005-06-26 11:25:32 +00:00
topics = Topic . find ( :all , :conditions = > " author_name = 'Mary' " )
2008-01-18 07:30:42 +00:00
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
2006-08-25 15:25:08 +00:00
classes = [ Category , Smarts , CreditCard , CreditCard :: PinNumber , CreditCard :: PinNumber :: CvvCode , CreditCard :: SubPinNumber , CreditCard :: Brand , MasterCreditCard ]
2004-11-24 01:04:44 +00:00
assert_equal " topics " , Topic . table_name
2006-08-25 15:25:08 +00:00
2004-11-24 01:04:44 +00:00
assert_equal " categories " , Category . table_name
assert_equal " smarts " , Smarts . table_name
assert_equal " credit_cards " , CreditCard . table_name
2006-08-16 09:46:43 +00:00
assert_equal " credit_card_pin_numbers " , CreditCard :: PinNumber . table_name
2006-08-25 15:25:08 +00:00
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
2004-11-24 01:04:44 +00:00
assert_equal " master_credit_cards " , MasterCreditCard . table_name
ActiveRecord :: Base . pluralize_table_names = false
2006-08-25 15:25:08 +00:00
classes . each ( & :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
2006-08-16 09:46:43 +00:00
assert_equal " credit_card_pin_number " , CreditCard :: PinNumber . table_name
2006-08-25 15:25:08 +00:00
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
2004-11-24 01:04:44 +00:00
assert_equal " master_credit_card " , MasterCreditCard . table_name
2006-08-25 15:25:08 +00:00
2004-11-24 01:04:44 +00:00
ActiveRecord :: Base . pluralize_table_names = true
2006-08-25 15:25:08 +00:00
classes . each ( & :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
2006-08-25 15:25:08 +00:00
2004-11-24 01:04:44 +00:00
ActiveRecord :: Base . pluralize_table_names = true
2006-08-25 15:25:08 +00:00
classes . each ( & :reset_table_name )
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_destroy_all
2008-03-24 02:50:02 +00:00
original_count = Topic . count
topics_by_mary = Topic . count ( :conditions = > mary = " author_name = 'Mary' " )
2008-06-03 18:32:53 +00:00
2008-03-24 02:50:02 +00:00
Topic . destroy_all mary
assert_equal original_count - topics_by_mary , 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
2008-03-24 02:50:02 +00:00
original_count = Topic . count
Topic . delete ( deleting = [ 1 , 2 ] )
assert_equal original_count - deleting . size , Topic . count
2005-01-25 12:45:01 +00:00
end
2004-11-24 01:04:44 +00:00
def test_boolean_attributes
assert ! Topic . find ( 1 ) . approved?
assert Topic . find ( 2 ) . approved?
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_increment_counter
Topic . increment_counter ( " replies_count " , 1 )
2006-06-03 00:01:08 +00:00
assert_equal 2 , Topic . find ( 1 ) . replies_count
2004-11-24 01:04:44 +00:00
Topic . increment_counter ( " replies_count " , 1 )
2006-06-03 00:01:08 +00:00
assert_equal 3 , Topic . find ( 1 ) . replies_count
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_decrement_counter
Topic . decrement_counter ( " replies_count " , 2 )
2006-06-03 00:01:08 +00:00
assert_equal - 1 , Topic . find ( 2 ) . replies_count
2004-11-24 01:04:44 +00:00
Topic . decrement_counter ( " replies_count " , 2 )
2006-06-03 00:01:08 +00:00
assert_equal - 2 , Topic . find ( 2 ) . replies_count
2004-11-24 01:04:44 +00:00
end
2005-10-27 08:18:41 +00:00
2008-06-26 16:46:33 +00:00
def test_update_counter
2008-08-26 07:02:22 +00:00
category = categories ( :general )
2008-06-26 16:46:33 +00:00
assert_nil category . categorizations_count
assert_equal 2 , category . categorizations . count
Category . update_counters ( category . id , " categorizations_count " = > category . categorizations . count )
category . reload
assert_not_nil category . categorizations_count
assert_equal 2 , category . categorizations_count
Category . update_counters ( category . id , " categorizations_count " = > category . categorizations . count )
category . reload
assert_not_nil category . categorizations_count
assert_equal 4 , category . categorizations_count
end
2007-10-18 23:47:59 +00:00
def test_update_all
2008-03-24 02:50:02 +00:00
assert_equal Topic . count , Topic . update_all ( " content = 'bulk updated!' " )
2007-10-18 23:47:59 +00:00
assert_equal " bulk updated! " , Topic . find ( 1 ) . content
assert_equal " bulk updated! " , Topic . find ( 2 ) . content
2007-01-28 15:12:54 +00:00
2008-03-24 02:50:02 +00:00
assert_equal Topic . count , Topic . update_all ( [ 'content = ?' , 'bulk updated again!' ] )
2007-10-18 23:47:59 +00:00
assert_equal " bulk updated again! " , Topic . find ( 1 ) . content
assert_equal " bulk updated again! " , Topic . find ( 2 ) . content
2008-03-24 02:50:02 +00:00
assert_equal Topic . count , Topic . update_all ( [ 'content = ?' , nil ] )
2007-10-18 23:47:59 +00:00
assert_nil Topic . find ( 1 ) . content
end
def test_update_all_with_hash
assert_not_nil Topic . find ( 1 ) . last_read
2008-03-24 02:50:02 +00:00
assert_equal Topic . count , Topic . update_all ( :content = > 'bulk updated with hash!' , :last_read = > nil )
2007-10-18 23:47:59 +00:00
assert_equal " bulk updated with hash! " , Topic . find ( 1 ) . content
assert_equal " bulk updated with hash! " , Topic . find ( 2 ) . content
assert_nil Topic . find ( 1 ) . last_read
assert_nil Topic . find ( 2 ) . last_read
2004-11-24 01:04:44 +00:00
end
2004-12-19 16:21:55 +00:00
2008-01-05 14:58:28 +00:00
def test_update_all_with_non_standard_table_name
assert_equal 1 , WarehouseThing . update_all ( [ 'value = ?' , 0 ] , [ 'id = ?' , 1 ] )
assert_equal 0 , WarehouseThing . find ( 1 ) . value
end
2007-03-17 15:48:47 +00:00
if current_adapter? ( :MysqlAdapter )
def test_update_all_with_order_and_limit
assert_equal 1 , Topic . update_all ( " content = 'bulk updated!' " , nil , :limit = > 1 , :order = > 'id DESC' )
end
end
2008-09-10 10:39:50 +00:00
def test_update_all_ignores_order_without_limit_from_association
author = authors ( :david )
2008-01-04 03:19:50 +00:00
assert_nothing_raised do
2008-09-10 10:39:50 +00:00
assert_equal author . posts_with_comments_and_categories . length , author . posts_with_comments_and_categories . update_all ( [ " body = ? " , " bulk update! " ] )
end
end
def test_update_all_with_order_and_limit_updates_subset_only
author = authors ( :david )
assert_nothing_raised do
assert_equal 1 , author . posts_sorted_by_id_limited . size
assert_equal 2 , author . posts_sorted_by_id_limited . find ( :all , :limit = > 2 ) . size
assert_equal 1 , author . posts_sorted_by_id_limited . update_all ( [ " body = ? " , " bulk update! " ] )
assert_equal " bulk update! " , posts ( :welcome ) . body
assert_not_equal " bulk update! " , posts ( :thinking ) . body
2008-01-04 03:19:50 +00:00
end
end
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
2008-03-24 02:50:02 +00:00
assert Topic . count > 0
2008-06-03 18:32:53 +00:00
2008-03-24 02:50:02 +00:00
assert_equal Topic . count , Topic . delete_all
2004-12-19 16:21:55 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_null_fields
assert_nil Topic . find ( 1 ) . parent_id
assert_nil Topic . create ( " title " = > " Hey you " ) . parent_id
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
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
2005-11-14 04:51:54 +00:00
2008-01-18 07:30:42 +00:00
# Oracle has some funky default handling, so it requires a bit of
2005-11-14 04:51:54 +00:00
# extra testing. See ticket #2788.
2006-03-01 16:01:53 +00:00
if current_adapter? ( :OracleAdapter )
test = TestOracleDefault . new
2005-11-14 04:51:54 +00:00
assert_equal " X " , test . test_char
assert_equal " hello " , test . test_string
assert_equal 3 , test . test_int
end
2004-11-24 01:04:44 +00:00
end
2004-12-28 17:30:17 +00:00
2008-11-19 16:09:44 +00:00
# Oracle, and Sybase do not have a TIME datatype.
unless current_adapter? ( :OracleAdapter , :SybaseAdapter )
2006-09-15 07:02:05 +00:00
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
2006-08-05 20:50:26 +00:00
2006-09-15 07:02:05 +00:00
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
2006-08-05 20:50:26 +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
2006-03-18 03:02:32 +00:00
# Sybase adapter does not allow nulls in boolean columns
if current_adapter? ( :SybaseAdapter )
assert topic . approved == false
else
assert_nil topic . approved
end
2004-11-24 01:04:44 +00:00
end
2005-11-16 08:16:54 +00:00
2004-11-24 01:04:44 +00:00
def test_equality
2006-03-05 18:43:56 +00:00
assert_equal Topic . find ( 1 ) , Topic . find ( 2 ) . topic
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2005-09-09 08:54:02 +00:00
def test_equality_of_new_records
assert_not_equal Topic . new , Topic . new
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_hashing
2006-03-05 18:43:56 +00:00
assert_equal [ Topic . find ( 1 ) ] , [ Topic . find ( 2 ) . topic ] & [ Topic . find ( 1 ) ]
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2008-09-21 21:01:32 +00:00
def test_delete_new_record
client = Client . new
client . delete
assert client . frozen?
end
def test_delete_record_with_associations
client = Client . find ( 3 )
client . delete
assert client . frozen?
assert_kind_of Firm , client . firm
assert_raises ( ActiveSupport :: FrozenObjectError ) { client . name = " something else " }
end
2004-11-24 01:04:44 +00:00
def test_destroy_new_record
client = Client . new
client . destroy
assert client . frozen?
end
2008-01-18 07:30:42 +00:00
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
2007-12-29 19:43:07 +00:00
assert_raises ( ActiveSupport :: FrozenObjectError ) { client . name = " something else " }
2005-05-02 16:46:30 +00:00
end
2008-01-18 07:30:42 +00:00
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
2008-01-18 07:30:42 +00:00
2006-10-09 01:52:24 +00:00
def test_update_attributes
topic = Topic . find ( 1 )
assert ! topic . approved?
assert_equal " The First Topic " , topic . title
2008-01-18 07:30:42 +00:00
2006-10-09 01:52:24 +00:00
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
2008-01-18 07:30:42 +00:00
2006-10-09 01:52:24 +00:00
def test_update_attributes!
reply = Reply . find ( 2 )
2008-03-24 02:50:02 +00:00
assert_equal " The Second Topic of the day " , reply . title
2006-10-09 01:52:24 +00:00
assert_equal " Have a nice day " , reply . content
2008-01-18 07:30:42 +00:00
2008-03-24 02:50:02 +00:00
reply . update_attributes! ( " title " = > " The Second Topic of the day updated " , " content " = > " Have a nice evening " )
2006-10-09 01:52:24 +00:00
reply . reload
2008-03-24 02:50:02 +00:00
assert_equal " The Second Topic of the day updated " , reply . title
2006-10-09 01:52:24 +00:00
assert_equal " Have a nice evening " , reply . content
2008-01-18 07:30:42 +00:00
2008-03-24 02:50:02 +00:00
reply . update_attributes! ( :title = > " The Second Topic of the day " , :content = > " Have a nice day " )
2006-10-09 01:52:24 +00:00
reply . reload
2008-03-24 02:50:02 +00:00
assert_equal " The Second Topic of the day " , reply . title
2006-10-09 01:52:24 +00:00
assert_equal " Have a nice day " , reply . content
2008-01-18 07:30:42 +00:00
2006-10-09 01:52:24 +00:00
assert_raise ( ActiveRecord :: RecordInvalid ) { reply . update_attributes! ( :title = > nil , :content = > " Have a nice evening " ) }
end
2008-01-18 07:30:42 +00:00
2007-05-29 20:35:46 +00:00
def test_mass_assignment_should_raise_exception_if_accessible_and_protected_attribute_writers_are_both_used
topic = TopicWithProtectedContentAndAccessibleAuthorName . new
assert_raises ( RuntimeError ) { topic . attributes = { " author_name " = > " me " } }
assert_raises ( RuntimeError ) { topic . attributes = { " content " = > " stuff " } }
end
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_mass_assignment_protection
firm = Firm . new
firm . attributes = { " name " = > " Next Angle " , " rating " = > 5 }
assert_equal 1 , firm . rating
end
2008-01-18 07:30:42 +00:00
2007-01-28 01:31:31 +00:00
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 ,
2008-09-04 12:03:19 +00:00
:default_timezone , :schema_format , :lock_optimistically , :record_timestamps ] . each do | method |
2007-01-28 01:31:31 +00:00
assert Task . respond_to? ( method )
assert Task . respond_to? ( " #{ method } = " )
assert Task . new . respond_to? ( method )
assert ! Task . new . respond_to? ( " #{ method } = " )
end
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
2007-09-28 14:18:47 +00:00
def test_customized_primary_key_remains_protected_when_referred_to_as_id
2005-10-12 19:59:13 +00:00
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
2008-01-18 07:30:42 +00:00
2008-09-08 16:45:26 +00:00
def test_mass_assigning_invalid_attribute
firm = Firm . new
assert_raises ( ActiveRecord :: UnknownAttributeError ) do
firm . attributes = { " id " = > 5 , " type " = > " Client " , " i_dont_even_exist " = > 20 }
end
end
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
2008-01-18 07:30:42 +00:00
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?
2008-01-18 07:30:42 +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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_mass_assignment_protection_inheritance
2005-04-30 13:24:43 +00:00
assert_nil LoosePerson . accessible_attributes
2007-11-28 20:13:17 +00:00
assert_equal Set . new ( [ 'credit_rating' , 'administrator' ] ) , LoosePerson . protected_attributes
2005-04-30 13:24:43 +00:00
assert_nil LooseDescendant . accessible_attributes
2007-11-28 20:13:17 +00:00
assert_equal Set . new ( [ 'credit_rating' , 'administrator' , 'phone_number' ] ) , LooseDescendant . protected_attributes
assert_nil LooseDescendantSecond . accessible_attributes
assert_equal Set . new ( [ 'credit_rating' , 'administrator' , 'phone_number' , 'name' ] ) , LooseDescendantSecond . protected_attributes , 'Running attr_protected twice in one class should merge the protections'
2005-04-30 13:24:43 +00:00
2004-11-24 01:04:44 +00:00
assert_nil TightPerson . protected_attributes
2007-11-28 20:13:17 +00:00
assert_equal Set . new ( [ 'name' , 'address' ] ) , TightPerson . accessible_attributes
2005-04-30 13:24:43 +00:00
assert_nil TightDescendant . protected_attributes
2007-11-28 20:13:17 +00:00
assert_equal Set . new ( [ 'name' , 'address' , 'phone_number' ] ) , TightDescendant . accessible_attributes
2004-11-24 01:04:44 +00:00
end
2008-01-18 07:30:42 +00:00
2007-09-30 07:09:44 +00:00
def test_readonly_attributes
2008-02-19 21:01:10 +00:00
assert_equal Set . new ( [ 'title' , 'comments_count' ] ) , ReadonlyTitlePost . readonly_attributes
2008-01-18 07:30:42 +00:00
2007-09-30 07:09:44 +00:00
post = ReadonlyTitlePost . create ( :title = > " cannot change this " , :body = > " changeable " )
post . reload
assert_equal " cannot change this " , post . title
2008-01-18 07:30:42 +00:00
2007-09-30 07:09:44 +00:00
post . update_attributes ( :title = > " try to change " , :body = > " changed " )
post . reload
assert_equal " cannot change this " , post . title
assert_equal " changed " , post . body
end
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
# note that extra #to_date call allows test to pass for Oracle, which
2005-10-26 12:57:11 +00:00
# treats dates/times the same
2006-01-21 23:20:00 +00:00
assert_date_from_db Date . new ( 2004 , 6 , 24 ) , topic . last_read . to_date
2004-11-24 01:04:44 +00:00
end
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
2008-01-18 07:30:42 +00:00
# note that extra #to_date call allows test to pass for Oracle, which
2005-10-26 12:57:11 +00:00
# treats dates/times the same
2006-01-21 23:20:00 +00:00
assert_date_from_db Date . new ( 2004 , 6 , 1 ) , topic . last_read . to_date
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
2008-01-18 07:30:42 +00:00
attributes = {
" written_on(1i) " = > " 2004 " , " written_on(2i) " = > " 6 " , " written_on(3i) " = > " 24 " ,
2004-11-24 01:04:44 +00:00
" 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
2008-06-03 18:32:53 +00:00
2008-02-10 22:46:31 +00:00
def test_multiparameter_attributes_on_time_with_old_date
attributes = {
" written_on(1i) " = > " 1850 " , " 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
# testing against to_s(:db) representation because either a Time or a DateTime might be returned, depending on platform
assert_equal " 1850-06-24 16:24:00 " , topic . written_on . to_s ( :db )
end
2004-11-24 01:04:44 +00:00
2008-02-06 06:43:02 +00:00
def test_multiparameter_attributes_on_time_with_utc
ActiveRecord :: Base . default_timezone = :utc
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 . utc ( 2004 , 6 , 24 , 16 , 24 , 0 ) , topic . written_on
ensure
ActiveRecord :: Base . default_timezone = :local
end
2008-03-30 23:01:06 +00:00
def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes
ActiveRecord :: Base . time_zone_aware_attributes = true
ActiveRecord :: Base . default_timezone = :utc
2008-06-03 18:32:53 +00:00
Time . zone = ActiveSupport :: TimeZone [ - 28800 ]
2008-03-30 23:01:06 +00:00
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 . utc ( 2004 , 6 , 24 , 23 , 24 , 0 ) , topic . written_on
assert_equal Time . utc ( 2004 , 6 , 24 , 16 , 24 , 0 ) , topic . written_on . time
assert_equal Time . zone , topic . written_on . time_zone
ensure
ActiveRecord :: Base . time_zone_aware_attributes = false
ActiveRecord :: Base . default_timezone = :local
Time . zone = nil
2008-02-06 06:43:02 +00:00
end
2008-02-06 22:26:47 +00:00
2008-02-16 20:47:01 +00:00
def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes_false
ActiveRecord :: Base . time_zone_aware_attributes = false
2008-06-03 18:32:53 +00:00
Time . zone = ActiveSupport :: TimeZone [ - 28800 ]
2008-02-16 20:47:01 +00:00
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
assert_equal false , topic . written_on . respond_to? ( :time_zone )
ensure
Time . zone = nil
end
2008-02-06 06:43:02 +00:00
def test_multiparameter_attributes_on_time_with_skip_time_zone_conversion_for_attributes
ActiveRecord :: Base . time_zone_aware_attributes = true
ActiveRecord :: Base . default_timezone = :utc
2008-06-03 18:32:53 +00:00
Time . zone = ActiveSupport :: TimeZone [ - 28800 ]
2008-02-06 06:43:02 +00:00
Topic . skip_time_zone_conversion_for_attributes = [ :written_on ]
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 . utc ( 2004 , 6 , 24 , 16 , 24 , 0 ) , topic . written_on
assert_equal false , topic . written_on . respond_to? ( :time_zone )
ensure
ActiveRecord :: Base . time_zone_aware_attributes = false
ActiveRecord :: Base . default_timezone = :local
Time . zone = nil
Topic . skip_time_zone_conversion_for_attributes = [ ]
end
2008-09-14 23:16:50 +00:00
def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion
ActiveRecord :: Base . time_zone_aware_attributes = true
ActiveRecord :: Base . default_timezone = :utc
Time . zone = ActiveSupport :: TimeZone [ - 28800 ]
attributes = {
" bonus_time(1i) " = > " 2000 " , " bonus_time(2i) " = > " 1 " , " bonus_time(3i) " = > " 1 " ,
" bonus_time(4i) " = > " 16 " , " bonus_time(5i) " = > " 24 "
}
topic = Topic . find ( 1 )
topic . attributes = attributes
assert_equal Time . utc ( 2000 , 1 , 1 , 16 , 24 , 0 ) , topic . bonus_time
assert topic . bonus_time . utc?
ensure
ActiveRecord :: Base . time_zone_aware_attributes = false
ActiveRecord :: Base . default_timezone = :local
Time . zone = nil
end
2008-02-06 06:43:02 +00:00
2004-11-24 01:04:44 +00:00
def test_multiparameter_attributes_on_time_with_empty_seconds
2008-01-18 07:30:42 +00:00
attributes = {
" written_on(1i) " = > " 2004 " , " written_on(2i) " = > " 6 " , " written_on(3i) " = > " 24 " ,
2004-11-24 01:04:44 +00:00
" 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 )
2008-01-18 07:30:42 +00:00
task . starting = time
2005-06-28 17:15:01 +00:00
attributes = { " starting(1i) " = > " 2004 " , " starting(2i) " = > " 6 " , " starting(3i) " = > " 24 " }
task . attributes = attributes
assert_equal time , task . starting
end
2008-01-18 07:30:42 +00:00
2006-04-06 16:16:29 +00:00
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
2005-06-28 17:15:01 +00:00
2004-12-01 13:18:51 +00:00
def test_attributes_on_dummy_time
2008-11-19 16:09:44 +00:00
# Oracle, and Sybase do not have a TIME datatype.
return true if current_adapter? ( :OracleAdapter , :SybaseAdapter )
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
2008-08-18 22:56:37 +00:00
b_nil = Booleantest . create ( { " value " = > nil } )
nil_id = b_nil . id
2004-11-24 01:04:44 +00:00
b_false = Booleantest . create ( { " value " = > false } )
false_id = b_false . id
b_true = Booleantest . create ( { " value " = > true } )
true_id = b_true . id
2008-08-18 22:56:37 +00:00
b_nil = Booleantest . find ( nil_id )
assert_nil b_nil . value
2004-11-24 01:04:44 +00:00
b_false = Booleantest . find ( false_id )
assert ! b_false . value?
b_true = Booleantest . find ( true_id )
assert b_true . value?
end
2006-03-18 21:27:40 +00:00
def test_boolean_cast_from_string
2008-08-18 22:56:37 +00:00
b_blank = Booleantest . create ( { " value " = > " " } )
blank_id = b_blank . id
2006-03-19 17:06:53 +00:00
b_false = Booleantest . create ( { " value " = > " 0 " } )
2006-03-18 21:27:40 +00:00
false_id = b_false . id
2006-03-19 17:06:53 +00:00
b_true = Booleantest . create ( { " value " = > " 1 " } )
2006-03-18 21:27:40 +00:00
true_id = b_true . id
2008-08-18 22:56:37 +00:00
b_blank = Booleantest . find ( blank_id )
assert_nil b_blank . value
2006-03-18 21:27:40 +00:00
b_false = Booleantest . find ( false_id )
assert ! b_false . value?
b_true = Booleantest . find ( true_id )
2008-01-18 07:30:42 +00:00
assert b_true . value?
2006-03-18 21:27:40 +00:00
end
2008-01-18 07:30:42 +00:00
2008-12-27 13:26:13 +00:00
def test_new_record_returns_boolean
assert_equal Topic . new . new_record? , true
assert_equal Topic . find ( 1 ) . new_record? , false
end
2004-11-24 01:04:44 +00:00
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
2006-09-05 18:54:24 +00:00
assert cloned_topic . new_record?
2004-11-24 01:04:44 +00:00
# test if the attributes have been cloned
2008-01-18 07:30:42 +00:00
topic . title = " a "
cloned_topic . title = " b "
2004-11-24 01:04:44 +00:00
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
2008-01-18 07:30:42 +00:00
cloned_topic . title [ " a " ] = " c "
2004-11-24 01:04:44 +00:00
assert_equal " b " , topic . title [ " a " ]
2005-02-07 13:43:44 +00:00
2007-10-08 06:21:38 +00:00
#test if attributes set as part of after_initialize are cloned correctly
assert_equal topic . author_email_address , cloned_topic . author_email_address
# test if saved clone object differs from original
2005-02-07 13:43:44 +00:00
cloned_topic . save
2006-09-05 18:54:24 +00:00
assert ! cloned_topic . new_record?
2005-02-07 13:43:44 +00:00
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
2006-09-05 18:54:24 +00:00
assert clone . new_record?
2005-05-19 16:39:50 +00:00
# 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
2006-09-05 18:54:24 +00:00
assert ! clone . new_record?
2005-05-19 16:39:50 +00:00
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
2008-01-18 07:30:42 +00:00
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
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
# 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>'
)
2008-01-18 07:30:42 +00:00
2005-10-09 00:52:25 +00:00
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
2008-01-18 07:30:42 +00:00
2005-10-09 00:52:25 +00:00
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 )
2008-01-18 07:30:42 +00:00
2005-10-09 00:52:25 +00:00
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
2006-07-08 20:35:56 +00:00
class NumericData < ActiveRecord :: Base
self . table_name = 'numeric_data'
end
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
2004-11-24 01:04:44 +00:00
def test_auto_id
auto = AutoId . new
auto . save
assert ( auto . id > 0 )
end
2006-07-08 20:35:56 +00:00
2004-11-24 01:04:44 +00:00
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
2006-07-27 18:29:49 +00:00
def test_sql_injection_via_find
2007-01-05 00:12:06 +00:00
assert_raises ( ActiveRecord :: RecordNotFound , ActiveRecord :: StatementInvalid ) do
2006-07-27 18:29:49 +00:00
Topic . find ( " 123456 OR id > 0 " )
end
end
2004-11-24 01:04:44 +00:00
def test_column_name_properly_quoted
col_record = ColumnName . new
col_record . references = 40
2005-11-12 11:59:54 +00:00
assert col_record . save
2004-11-24 01:04:44 +00:00
col_record . references = 41
2005-11-12 11:59:54 +00:00
assert col_record . save
assert_not_nil c2 = ColumnName . find ( col_record . id )
2004-11-24 01:04:44 +00:00
assert_equal ( 41 , c2 . references )
end
2006-05-31 23:25:36 +00:00
def test_quoting_arrays
2006-06-01 00:43:02 +00:00
replies = Reply . find ( :all , :conditions = > [ " id IN (?) " , topics ( :first ) . replies . collect ( & :id ) ] )
2006-05-31 23:25:36 +00:00
assert_equal topics ( :first ) . replies . size , replies . size
2006-06-01 00:43:02 +00:00
replies = Reply . find ( :all , :conditions = > [ " id IN (?) " , [ ] ] )
2006-05-31 23:25:36 +00:00
assert_equal 0 , replies . size
end
2004-11-24 01:04:44 +00:00
MyObject = Struct . new :attribute1 , :attribute2
2008-01-18 07:30:42 +00:00
2004-11-24 01:04:44 +00:00
def test_serialized_attribute
myobj = MyObject . new ( 'value1' , 'value2' )
2008-01-18 07:30:42 +00:00
topic = Topic . create ( " content " = > myobj )
2004-11-24 01:04:44 +00:00
Topic . serialize ( " content " , MyObject )
assert_equal ( myobj , topic . content )
end
2008-08-13 03:18:01 +00:00
def test_serialized_time_attribute
myobj = Time . local ( 2008 , 1 , 1 , 1 , 0 )
topic = Topic . create ( " content " = > myobj ) . reload
assert_equal ( myobj , topic . content )
end
2008-10-27 16:16:45 +00:00
def test_serialized_string_attribute
myobj = " Yes "
topic = Topic . create ( " content " = > myobj ) . reload
assert_equal ( myobj , topic . content )
end
2008-08-13 03:18:01 +00:00
2007-05-28 15:51:53 +00:00
def test_nil_serialized_attribute_with_class_constraint
2004-11-24 01:04:44 +00:00
myobj = MyObject . new ( 'value1' , 'value2' )
2007-05-28 15:51:53 +00:00
topic = Topic . new
assert_nil topic . content
end
2004-11-24 01:04:44 +00:00
2007-05-28 15:51:53 +00:00
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 )
2005-04-02 08:36:32 +00:00
assert_raise ( ActiveRecord :: SerializationTypeMismatch ) { Topic . find ( topic . id ) . content }
2007-05-28 15:51:53 +00:00
ensure
Topic . serialize ( :content )
end
2004-11-24 01:04:44 +00:00
2007-05-28 15:51:53 +00:00
def test_serialized_attribute_with_class_constraint
2004-11-24 01:04:44 +00:00
settings = { " color " = > " blue " }
2007-05-28 15:51:53 +00:00
Topic . serialize ( :content , Hash )
topic = Topic . new ( :content = > settings )
assert topic . save
2004-11-24 01:04:44 +00:00
assert_equal ( settings , Topic . find ( topic . id ) . content )
2007-05-28 15:51:53 +00:00
ensure
2004-11-24 01:04:44 +00:00
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
2008-01-04 03:19:20 +00:00
if RUBY_VERSION < '1.9'
def test_quote_chars
2008-09-21 15:30:16 +00:00
with_kcode ( 'UTF8' ) do
str = 'The Narrator'
topic = Topic . create ( :author_name = > str )
assert_equal str , topic . author_name
2008-01-04 03:19:20 +00:00
2008-09-21 15:30:16 +00:00
assert_kind_of ActiveSupport :: Multibyte . proxy_class , str . mb_chars
topic = Topic . find_by_author_name ( str . mb_chars )
2008-01-04 03:19:20 +00:00
2008-09-21 15:30:16 +00:00
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
2008-01-04 03:19:20 +00:00
end
2006-11-20 12:02:04 +00:00
end
2008-01-04 03:19:20 +00:00
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
2007-09-30 07:09:44 +00:00
assert_equal 50 , accounts ( :signals37 ) . credit_limit
accounts ( :signals37 ) . increment! :credit_limit
2008-01-18 07:30:42 +00:00
assert_equal 51 , accounts ( :signals37 , :reload ) . credit_limit
2007-09-30 07:09:44 +00:00
accounts ( :signals37 ) . increment ( :credit_limit ) . increment! ( :credit_limit )
assert_equal 53 , accounts ( :signals37 , :reload ) . credit_limit
2005-01-06 02:31:35 +00:00
end
2008-01-18 07:30:42 +00:00
2005-01-06 02:31:35 +00:00
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
2008-01-18 07:30:42 +00:00
2008-01-03 00:30:22 +00:00
def test_increment_attribute_by
assert_equal 50 , accounts ( :signals37 ) . credit_limit
accounts ( :signals37 ) . increment! :credit_limit , 5
2008-01-18 07:30:42 +00:00
assert_equal 55 , accounts ( :signals37 , :reload ) . credit_limit
2008-01-03 00:30:22 +00:00
accounts ( :signals37 ) . increment ( :credit_limit , 1 ) . increment! ( :credit_limit , 3 )
assert_equal 59 , accounts ( :signals37 , :reload ) . credit_limit
end
2008-01-18 07:30:42 +00:00
2005-01-06 02:31:35 +00:00
def test_decrement_attribute
2007-09-30 07:09:44 +00:00
assert_equal 50 , accounts ( :signals37 ) . credit_limit
2005-01-06 02:31:35 +00:00
2007-09-30 07:09:44 +00:00
accounts ( :signals37 ) . decrement! ( :credit_limit )
assert_equal 49 , accounts ( :signals37 , :reload ) . credit_limit
2008-01-18 07:30:42 +00:00
2007-09-30 07:09:44 +00:00
accounts ( :signals37 ) . decrement ( :credit_limit ) . decrement! ( :credit_limit )
assert_equal 47 , accounts ( :signals37 , :reload ) . credit_limit
2005-01-06 02:31:35 +00:00
end
2008-01-18 07:30:42 +00:00
2008-01-03 00:30:22 +00:00
def test_decrement_attribute_by
assert_equal 50 , accounts ( :signals37 ) . credit_limit
accounts ( :signals37 ) . decrement! :credit_limit , 5
2008-01-18 07:30:42 +00:00
assert_equal 45 , accounts ( :signals37 , :reload ) . credit_limit
2008-01-03 00:30:22 +00:00
accounts ( :signals37 ) . decrement ( :credit_limit , 1 ) . decrement! ( :credit_limit , 3 )
assert_equal 41 , accounts ( :signals37 , :reload ) . credit_limit
end
2008-01-18 07:30:42 +00:00
2005-01-06 02:31:35 +00:00
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
2005-11-16 08:16:54 +00:00
res = Post . count_by_sql " SELECT COUNT(*) FROM posts LEFT JOIN comments ON posts.id=comments.post_id WHERE posts. #{ QUOTED_TYPE } = 'Post' "
2008-01-18 07:30:42 +00:00
2007-07-16 20:26:10 +00:00
res2 = Post . count ( :conditions = > " posts. #{ QUOTED_TYPE } = 'Post' " , :joins = > " LEFT JOIN comments ON posts.id=comments.post_id " )
2005-04-30 15:45:15 +00:00
assert_equal res , res2
2008-01-18 07:30:42 +00:00
2006-02-20 03:15:22 +00:00
res3 = nil
2006-02-09 09:17:40 +00:00
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
2008-01-18 07:30:42 +00:00
2006-04-27 22:39:45 +00:00
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 "
2006-02-20 03:15:22 +00:00
res5 = nil
assert_nothing_raised do
2006-04-27 22:39:45 +00:00
res5 = Post . count ( :conditions = > " p. #{ QUOTED_TYPE } = 'Post' AND p.id=co.post_id " ,
:joins = > " p, comments co " ,
2006-02-20 03:15:22 +00:00
:select = > " p.id " )
end
2008-01-18 07:30:42 +00:00
assert_equal res4 , res5
2006-03-19 17:19:17 +00:00
2006-11-05 02:01:31 +00:00
unless current_adapter? ( :SQLite2Adapter , :DeprecatedSQLiteAdapter )
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
2006-02-20 03:15:22 +00:00
end
2005-04-30 15:45:15 +00:00
end
2008-01-18 07:30:42 +00:00
def test_clear_association_cache_stored
2005-06-25 10:56:20 +00:00
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
2008-01-18 07:30:42 +00:00
2005-06-25 10:56:20 +00:00
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 ]
2006-12-21 23:28:12 +00:00
2005-06-25 10:56:20 +00:00
firm . clients << clients
2006-12-21 23:28:12 +00:00
assert_equal clients . map ( & :name ) . to_set , firm . clients . map ( & :name ) . to_set
2005-06-25 10:56:20 +00:00
firm . clear_association_cache
2006-12-21 23:28:12 +00:00
assert_equal clients . map ( & :name ) . to_set , firm . clients . map ( & :name ) . to_set
2005-06-25 10:56:20 +00:00
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-11-06 10:18:51 +00:00
def test_scoped_find_conditions
2006-01-04 03:43:28 +00:00
scoped_developers = Developer . with_scope ( :find = > { :conditions = > 'salary > 90000' } ) do
2005-10-26 13:20:02 +00:00
Developer . find ( :all , :conditions = > 'id < 5' )
end
2006-01-04 03:43:28 +00:00
assert ! scoped_developers . include? ( developers ( :david ) ) # David's salary is less than 90,000
assert_equal 3 , scoped_developers . size
2005-10-26 13:20:02 +00:00
end
2008-01-18 07:30:42 +00:00
2005-11-06 10:18:51 +00:00
def test_scoped_find_limit_offset
2006-01-04 03:43:28 +00:00
scoped_developers = Developer . with_scope ( :find = > { :limit = > 3 , :offset = > 2 } ) do
2005-10-26 13:20:02 +00:00
Developer . find ( :all , :order = > 'id' )
2008-01-18 07:30:42 +00:00
end
2006-01-04 03:43:28 +00:00
assert ! scoped_developers . include? ( developers ( :david ) )
assert ! scoped_developers . include? ( developers ( :jamis ) )
assert_equal 3 , scoped_developers . size
2008-01-18 07:30:42 +00:00
2005-11-06 10:18:51 +00:00
# Test without scoped find conditions to ensure we get the whole thing
2005-10-26 13:20:02 +00:00
developers = Developer . find ( :all , :order = > 'id' )
2006-01-04 03:43:28 +00:00
assert_equal Developer . count , developers . size
2005-10-26 13:20:02 +00:00
end
2006-01-20 20:43:40 +00:00
2008-01-18 07:30:42 +00:00
def test_scoped_find_order
# Test order in scope
2006-04-26 06:37:04 +00:00
scoped_developers = Developer . with_scope ( :find = > { :limit = > 1 , :order = > 'salary DESC' } ) do
Developer . find ( :all )
2008-01-18 07:30:42 +00:00
end
2006-04-26 06:37:04 +00:00
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' )
2008-01-18 07:30:42 +00:00
end
2006-04-26 06:37:04 +00:00
# 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
2006-06-03 21:51:57 +00:00
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
2006-11-07 00:18:07 +00:00
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
2008-03-12 21:26:02 +00:00
def test_find_last
last = Developer . find :last
assert_equal last , Developer . find ( :first , :order = > 'id desc' )
end
2008-06-03 18:32:53 +00:00
2008-03-24 19:59:22 +00:00
def test_last
assert_equal Developer . find ( :first , :order = > 'id desc' ) , Developer . last
end
2008-04-28 18:27:52 +00:00
def test_all_with_conditions
assert_equal Developer . find ( :all , :order = > 'id desc' ) , Developer . all ( :order = > 'id desc' )
end
2008-06-03 18:32:53 +00:00
2008-03-12 21:26:02 +00:00
def test_find_ordered_last
last = Developer . find :last , :order = > 'developers.salary ASC'
assert_equal last , Developer . find ( :all , :order = > 'developers.salary ASC' ) . last
end
def test_find_reverse_ordered_last
last = Developer . find :last , :order = > 'developers.salary DESC'
assert_equal last , Developer . find ( :all , :order = > 'developers.salary DESC' ) . last
end
def test_find_multiple_ordered_last
last = Developer . find :last , :order = > 'developers.name, developers.salary DESC'
assert_equal last , Developer . find ( :all , :order = > 'developers.name, developers.salary DESC' ) . last
end
2008-06-03 18:32:53 +00:00
2008-03-12 21:26:02 +00:00
def test_find_scoped_ordered_last
last_developer = Developer . with_scope ( :find = > { :order = > 'developers.salary ASC' } ) do
Developer . find ( :last )
end
assert_equal last_developer , Developer . find ( :all , :order = > 'developers.salary ASC' ) . last
end
2008-06-03 18:32:53 +00:00
2006-12-01 21:24:47 +00:00
def test_abstract_class
2006-12-19 19:47:21 +00:00
assert ! ActiveRecord :: Base . abstract_class?
2006-03-16 02:46:01 +00:00
assert LoosePerson . abstract_class?
assert ! LooseDescendant . abstract_class?
2006-12-01 21:24:47 +00:00
end
def test_base_class
2006-03-16 02:46:01 +00:00
assert_equal LoosePerson , LoosePerson . base_class
assert_equal LooseDescendant , LooseDescendant . base_class
assert_equal TightPerson , TightPerson . base_class
assert_equal TightPerson , TightDescendant . base_class
2006-12-01 21:24:47 +00:00
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
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.
2006-12-19 19:47:21 +00:00
assert ! SubStiPost . descends_from_active_record?
2006-12-01 21:24:47 +00:00
end
def test_find_on_abstract_base_class_doesnt_use_type_condition
old_class = LooseDescendant
Object . send :remove_const , :LooseDescendant
2008-04-21 10:00:01 +00:00
descendant = old_class . create! :first_name = > 'bob'
2006-12-01 21:24:47 +00:00
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
2006-01-20 20:43:40 +00:00
end
2006-03-05 23:41:17 +00:00
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
2006-03-09 21:12:28 +00:00
def test_to_xml
2007-11-14 18:59:05 +00:00
xml = REXML :: Document . new ( topics ( :first ) . to_xml ( :indent = > 0 ) )
2006-03-15 02:17:31 +00:00
bonus_time_in_current_timezone = topics ( :first ) . bonus_time . xmlschema
written_on_in_current_timezone = topics ( :first ) . written_on . xmlschema
2006-03-18 21:27:40 +00:00
last_read_in_current_timezone = topics ( :first ) . last_read . xmlschema
2007-11-14 18:59:05 +00:00
assert_equal " topic " , xml . root . name
assert_equal " The First Topic " , xml . elements [ " //title " ] . text
assert_equal " David " , xml . elements [ " //author-name " ] . text
assert_equal " 1 " , xml . elements [ " //id " ] . text
assert_equal " integer " , xml . elements [ " //id " ] . attributes [ 'type' ]
assert_equal " 1 " , xml . elements [ " //replies-count " ] . text
assert_equal " integer " , xml . elements [ " //replies-count " ] . attributes [ 'type' ]
assert_equal written_on_in_current_timezone , xml . elements [ " //written-on " ] . text
assert_equal " datetime " , xml . elements [ " //written-on " ] . attributes [ 'type' ]
assert_equal " --- Have a nice day \n " , xml . elements [ " //content " ] . text
assert_equal " yaml " , xml . elements [ " //content " ] . attributes [ 'type' ]
assert_equal " david@loudthinking.com " , xml . elements [ " //author-email-address " ] . text
assert_equal nil , xml . elements [ " //parent-id " ] . text
assert_equal " integer " , xml . elements [ " //parent-id " ] . attributes [ 'type' ]
assert_equal " true " , xml . elements [ " //parent-id " ] . attributes [ 'nil' ]
2008-11-19 16:09:44 +00:00
if current_adapter? ( :SybaseAdapter , :OracleAdapter )
2007-11-14 18:59:05 +00:00
assert_equal last_read_in_current_timezone , xml . elements [ " //last-read " ] . text
assert_equal " datetime " , xml . elements [ " //last-read " ] . attributes [ 'type' ]
2006-03-18 03:02:32 +00:00
else
2007-11-14 18:59:05 +00:00
assert_equal " 2004-04-15 " , xml . elements [ " //last-read " ] . text
assert_equal " date " , xml . elements [ " //last-read " ] . attributes [ 'type' ]
2006-03-18 03:02:32 +00:00
end
2007-11-14 18:59:05 +00:00
2006-03-22 19:30:02 +00:00
# Oracle and DB2 don't have true boolean or time-only fields
2006-07-07 10:48:43 +00:00
unless current_adapter? ( :OracleAdapter , :DB2Adapter )
2007-11-14 18:59:05 +00:00
assert_equal " false " , xml . elements [ " //approved " ] . text
assert_equal " boolean " , xml . elements [ " //approved " ] . attributes [ 'type' ]
assert_equal bonus_time_in_current_timezone , xml . elements [ " //bonus-time " ] . text
assert_equal " datetime " , xml . elements [ " //bonus-time " ] . attributes [ 'type' ]
2006-03-16 03:20:46 +00:00
end
2006-03-09 21:12:28 +00:00
end
2006-06-03 00:01:08 +00:00
2006-03-09 21:12:28 +00:00
def test_to_xml_skipping_attributes
2006-09-15 07:02:05 +00:00
xml = topics ( :first ) . to_xml ( :indent = > 0 , :skip_instruct = > true , :except = > [ :title , :replies_count ] )
2006-03-09 21:12:28 +00:00
assert_equal " <topic> " , xml . first ( 7 )
2006-06-04 00:33:52 +00:00
assert ! xml . include? ( %( <title>The First Topic</title> ) )
2008-01-18 07:30:42 +00:00
assert xml . include? ( %( <author-name>David</author-name> ) )
2006-03-09 21:12:28 +00:00
2006-09-15 07:02:05 +00:00
xml = topics ( :first ) . to_xml ( :indent = > 0 , :skip_instruct = > true , :except = > [ :title , :author_name , :replies_count ] )
2006-06-04 00:33:52 +00:00
assert ! xml . include? ( %( <title>The First Topic</title> ) )
2008-01-18 07:30:42 +00:00
assert ! xml . include? ( %( <author-name>David</author-name> ) )
2006-03-09 21:12:28 +00:00
end
2006-09-15 07:02:05 +00:00
2006-03-10 00:25:29 +00:00
def test_to_xml_including_has_many_association
2006-09-15 07:02:05 +00:00
xml = topics ( :first ) . to_xml ( :indent = > 0 , :skip_instruct = > true , :include = > :replies , :except = > :replies_count )
2006-03-10 00:25:29 +00:00
assert_equal " <topic> " , xml . first ( 7 )
2007-06-27 16:37:40 +00:00
assert xml . include? ( %( <replies type="array"><reply> ) )
2008-03-24 02:50:02 +00:00
assert xml . include? ( %( <title>The Second Topic of the day</title> ) )
2006-03-10 00:25:29 +00:00
end
2006-08-20 14:38:58 +00:00
def test_array_to_xml_including_has_many_association
xml = [ topics ( :first ) , topics ( :second ) ] . to_xml ( :indent = > 0 , :skip_instruct = > true , :include = > :replies )
2007-06-27 16:37:40 +00:00
assert xml . include? ( %( <replies type="array"><reply> ) )
2006-08-20 14:38:58 +00:00
end
2006-09-05 18:54:24 +00:00
def test_array_to_xml_including_methods
xml = [ topics ( :first ) , topics ( :second ) ] . to_xml ( :indent = > 0 , :skip_instruct = > true , :methods = > [ :topic_id ] )
2006-09-29 22:45:58 +00:00
assert xml . include? ( %( <topic-id type="integer"> #{ topics ( :first ) . topic_id } </topic-id> ) ) , xml
assert xml . include? ( %( <topic-id type="integer"> #{ topics ( :second ) . topic_id } </topic-id> ) ) , xml
2006-09-05 18:54:24 +00:00
end
2008-01-18 07:30:42 +00:00
2006-08-03 17:19:45 +00:00
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
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
2006-03-10 00:25:29 +00:00
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
2008-01-18 07:30:42 +00:00
2006-03-10 00:25:29 +00:00
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? ( %( <account> ) )
2007-06-27 16:37:40 +00:00
assert xml . include? ( %( <clients type="array"><client> ) )
2006-03-10 00:25:29 +00:00
end
2006-03-10 03:50:00 +00:00
def test_to_xml_including_multiple_associations_with_options
xml = companies ( :first_firm ) . to_xml (
2008-01-18 07:30:42 +00:00
:indent = > 0 , :skip_instruct = > true ,
2006-03-10 03:50:00 +00:00
:include = > { :clients = > { :only = > :name } }
)
2008-01-18 07:30:42 +00:00
2006-03-10 03:50:00 +00:00
assert_equal " <firm> " , xml . first ( 6 )
2006-06-04 00:33:52 +00:00
assert xml . include? ( %( <client><name>Summit</name></client> ) )
2007-06-27 16:37:40 +00:00
assert xml . include? ( %( <clients type="array"><client> ) )
2006-03-10 03:50:00 +00:00
end
2008-01-18 07:30:42 +00:00
2006-04-29 23:00:47 +00:00
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? ( %( <arbitrary-method>I am Jack's profound disappointment</arbitrary-method> ) )
end
2008-01-18 07:30:42 +00:00
2007-05-25 23:04:45 +00:00
def test_to_xml_with_block
value = " Rockin' the block "
xml = Company . new . to_xml ( :skip_instruct = > true ) do | xml |
xml . tag! " arbitrary-element " , value
end
assert_equal " <company> " , xml . first ( 9 )
assert xml . include? ( %( <arbitrary-element> #{ value } </arbitrary-element> ) )
end
2008-01-18 07:30:42 +00:00
2006-04-06 16:06:38 +00:00
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
2008-01-18 07:30:42 +00:00
2006-06-19 16:45:34 +00:00
def test_to_param_should_return_string
assert_kind_of String , Client . find ( :first ) . to_param
end
2008-01-18 07:30:42 +00:00
2007-05-31 17:31:33 +00:00
def test_inspect_class
assert_equal 'ActiveRecord::Base' , ActiveRecord :: Base . inspect
assert_equal 'LoosePerson(abstract)' , LoosePerson . inspect
assert_match ( / ^Topic \ (id: integer, title: string / , Topic . inspect )
end
def test_inspect_instance
2007-05-18 02:11:43 +00:00
topic = topics ( :first )
2007-05-31 17:31:33 +00:00
assert_equal %( # <Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: " #{ topic . written_on . to_s ( :db ) } ", bonus_time: " #{ topic . bonus_time . to_s ( :db ) } ", last_read: " #{ topic . last_read . to_s ( :db ) } ", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, type: nil> ) , topic . inspect
2007-05-18 02:11:43 +00:00
end
2007-05-18 07:24:03 +00:00
2007-05-31 17:31:33 +00:00
def test_inspect_new_instance
2007-05-19 01:35:19 +00:00
assert_match / Topic id: nil / , Topic . new . inspect
end
2007-06-11 05:02:42 +00:00
def test_inspect_limited_select_instance
assert_equal %( # <Topic id: 1> ) , Topic . find ( :first , :select = > 'id' , :conditions = > 'id = 1' ) . inspect
assert_equal %( # <Topic id: 1, title: "The First Topic"> ) , Topic . find ( :first , :select = > 'id, title' , :conditions = > 'id = 1' ) . inspect
end
2008-01-18 07:30:42 +00:00
2007-08-06 00:18:50 +00:00
def test_inspect_class_without_table
assert_equal " NonExistentTable(Table doesn't exist) " , NonExistentTable . inspect
end
2007-06-11 05:02:42 +00:00
2007-05-18 02:11:43 +00:00
def test_attribute_for_inspect
t = topics ( :first )
2007-12-06 00:01:11 +00:00
t . title = " The First Topic Now Has A Title With \n Newlines And More Than 50 Characters "
2007-05-18 07:24:03 +00:00
assert_equal %( " #{ t . written_on . to_s ( :db ) } " ) , t . attribute_for_inspect ( :written_on )
2007-12-06 00:01:11 +00:00
assert_equal '"The First Topic Now Has A Title With\nNewlines And M..."' , t . attribute_for_inspect ( :title )
2007-05-18 02:11:43 +00:00
end
2008-01-18 07:30:42 +00:00
2007-12-03 02:01:21 +00:00
def test_becomes
assert_kind_of Reply , topics ( :first ) . becomes ( Reply )
assert_equal " The First Topic " , topics ( :first ) . becomes ( Reply ) . title
end
2007-12-21 01:48:20 +00:00
def test_silence_sets_log_level_to_error_in_block
original_logger = ActiveRecord :: Base . logger
log = StringIO . new
ActiveRecord :: Base . logger = Logger . new ( log )
ActiveRecord :: Base . logger . level = Logger :: DEBUG
ActiveRecord :: Base . silence do
ActiveRecord :: Base . logger . warn " warn "
ActiveRecord :: Base . logger . error " error "
end
assert_equal " error \n " , log . string
ensure
ActiveRecord :: Base . logger = original_logger
end
def test_silence_sets_log_level_back_to_level_before_yield
original_logger = ActiveRecord :: Base . logger
log = StringIO . new
ActiveRecord :: Base . logger = Logger . new ( log )
ActiveRecord :: Base . logger . level = Logger :: WARN
ActiveRecord :: Base . silence do
end
assert_equal Logger :: WARN , ActiveRecord :: Base . logger . level
ensure
ActiveRecord :: Base . logger = original_logger
end
def test_benchmark_with_log_level
original_logger = ActiveRecord :: Base . logger
log = StringIO . new
ActiveRecord :: Base . logger = Logger . new ( log )
ActiveRecord :: Base . logger . level = Logger :: WARN
ActiveRecord :: Base . benchmark ( " Debug Topic Count " , Logger :: DEBUG ) { Topic . count }
ActiveRecord :: Base . benchmark ( " Warn Topic Count " , Logger :: WARN ) { Topic . count }
ActiveRecord :: Base . benchmark ( " Error Topic Count " , Logger :: ERROR ) { Topic . count }
assert_no_match / Debug Topic Count / , log . string
assert_match / Warn Topic Count / , log . string
assert_match / Error Topic Count / , log . string
ensure
ActiveRecord :: Base . logger = original_logger
end
def test_benchmark_with_use_silence
original_logger = ActiveRecord :: Base . logger
log = StringIO . new
ActiveRecord :: Base . logger = Logger . new ( log )
ActiveRecord :: Base . benchmark ( " Logging " , Logger :: DEBUG , true ) { ActiveRecord :: Base . logger . debug " Loud " }
ActiveRecord :: Base . benchmark ( " Logging " , Logger :: DEBUG , false ) { ActiveRecord :: Base . logger . debug " Quiet " }
assert_no_match / Loud / , log . string
assert_match / Quiet / , log . string
ensure
ActiveRecord :: Base . logger = original_logger
end
2008-09-21 15:30:16 +00:00
2008-12-22 15:18:43 +00:00
def test_create_with_custom_timestamps
custom_datetime = 1 . hour . ago . beginning_of_day
%w( created_at created_on updated_at updated_on ) . each do | attribute |
parrot = LiveParrot . create ( :name = > " colombian " , attribute = > custom_datetime )
assert_equal custom_datetime , parrot [ attribute ]
end
end
2008-09-21 15:30:16 +00:00
private
def with_kcode ( kcode )
if RUBY_VERSION < '1.9'
orig_kcode , $KCODE = $KCODE , kcode
begin
yield
ensure
$KCODE = orig_kcode
end
else
yield
end
end
2007-12-06 00:01:11 +00:00
end