Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added assert_date_from_db to do cleaner tests for SQL Server (closes #…

…3557) [Tom Ward]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3452 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6a2104d792cd55269e787c6746d552c53db9adeb 1 parent 3c8dbcb
@dhh dhh authored
View
11 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)
View
26 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
View
10 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
Please sign in to comment.
Something went wrong with that request. Please try again.