diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb index d9c60ab6bd128117c8610b8b4185fc3bc8c1aa38..6348394685e8c27d7759a717531575f78d16c0a4 100755 --- a/activerecord/test/abstract_unit.rb +++ b/activerecord/test/abstract_unit.rb @@ -18,6 +18,17 @@ class Test::Unit::TestCase #:nodoc: def create_fixtures(*table_names, &block) Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, &block) end + + def assert_date_from_db(expected, actual, message = nil) + # SQL Server doesn't have a separate column type just for dates, + # so the time is in the string and incorrectly formatted + + if current_adapter?(:SQLServerAdapter) + assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00") + else + assert_equal expected.to_s, actual.to_s, message + end + end end def current_adapter?(type) diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index b329968f228428fddf6b42df403442c1c31428aa..79c7cc8120cf1656b85d8b2b1e63439ebafe46fc 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1170,7 +1170,6 @@ def test_habtm_adding_before_save_with_join_attributes no_of_devels = Developer.count no_of_projects = Project.count now = Date.today - sqlnow = Time.now.strftime("%Y/%m/%d 00:00:00") ken = Developer.new("name" => "Ken") ken.projects.push_with_attributes( Project.find(1), :joined_on => now ) p = Project.new("name" => "Foomatic") @@ -1185,13 +1184,7 @@ def test_habtm_adding_before_save_with_join_attributes assert_equal 2, ken.projects(true).size kenReloaded = Developer.find_by_name 'Ken' - # SQL Server doesn't have a separate column type just for dates, - # so the time is in the string and incorrectly formatted - if current_adapter?(:SQLServerAdapter) - kenReloaded.projects.each { |prj| assert_equal(sqlnow, prj.joined_on.to_s) } - else - kenReloaded.projects.each { |prj| assert_equal(now.to_s, prj.joined_on.to_s) } - end + kenReloaded.projects.each {|prj| assert_date_from_db(now, prj.joined_on)} end def test_habtm_saving_multiple_relationships @@ -1298,10 +1291,7 @@ def test_removing_associations_on_destroy end def test_additional_columns_from_join_table - # SQL Server doesn't have a separate column type just for dates, - # so the time is in the string and incorrectly formatted - expected = (current_adapter?(:SQLServerAdapter) ? Time.mktime(2004, 10, 10).strftime("%Y/%m/%d 00:00:00") : Date.new(2004, 10, 10).to_s) - assert_equal expected, Developer.find(1).projects.first.joined_on.to_s + assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on end def test_destroy_all @@ -1316,15 +1306,9 @@ def test_destroy_all def test_rich_association jamis = developers(:jamis) jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) - # SQL Server doesn't have a separate column type just for dates, - # so the time is in the string and incorrectly formatted - if current_adapter?(:SQLServerAdapter) - assert_equal Time.now.strftime("%Y/%m/%d 00:00:00"), jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on.to_s - assert_equal Time.now.strftime("%Y/%m/%d 00:00:00"), developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on.to_s - else - assert_equal Date.today.to_s, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on.to_s - assert_equal Date.today.to_s, developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on.to_s - end + + assert_date_from_db Date.today, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on + assert_date_from_db Date.today, developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on end def test_associations_with_conditions diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 42fefa90016bca994876a354063672e6d5653d57..d2f347e0cb8e7b08e1a42998951985d5664b4ca1 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -649,27 +649,21 @@ def test_mass_assignment_protection_inheritance end def test_multiparameter_attributes_on_date - # SQL Server doesn't have a separate column type just for dates, so all are returned as time - return true if current_adapter?(:SQLServerAdapter) - attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" } topic = Topic.find(1) topic.attributes = attributes # note that extra #to_date call allows test to pass for Oracle, which # treats dates/times the same - assert_equal Date.new(2004, 6, 24).to_s, topic.last_read.to_date.to_s + assert_date_from_db Date.new(2004, 6, 24), topic.last_read.to_date end def test_multiparameter_attributes_on_date_with_empty_date - # SQL Server doesn't have a separate column type just for dates, so all are returned as time - return true if current_adapter?(:SQLServerAdapter) - attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" } topic = Topic.find(1) topic.attributes = attributes # note that extra #to_date call allows test to pass for Oracle, which # treats dates/times the same - assert_equal Date.new(2004, 6, 1).to_s, topic.last_read.to_date.to_s + assert_date_from_db Date.new(2004, 6, 1), topic.last_read.to_date end def test_multiparameter_attributes_on_date_with_all_empty