diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-01 17:20:04 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-01 17:20:04 +0000 |
commit | f2a29ca43cb0de38a25bf7f68bca5c11871692ce (patch) | |
tree | 49fafc3247a77dc1b8797e4425b28923af6a51d0 /activerecord/test | |
parent | 64612db7cf85aee8434e7b9b4fd8d6d0249c60e4 (diff) | |
download | rails-f2a29ca43cb0de38a25bf7f68bca5c11871692ce.tar.gz rails-f2a29ca43cb0de38a25bf7f68bca5c11871692ce.tar.bz2 rails-f2a29ca43cb0de38a25bf7f68bca5c11871692ce.zip |
Added support for ODBC connections to MS SQL Server so you can connect from a non-Windows machine #1569 [Mark Imbriaco/DeLynn Berry] Added support for limit/offset with the MS SQL Server driver so that pagination will now work #1569 [DeLynn Berry]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1583 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/associations_test.rb | 28 | ||||
-rwxr-xr-x | activerecord/test/base_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/binary_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/connections/native_sqlserver_odbc/connection.rb | 26 | ||||
-rwxr-xr-x | activerecord/test/deprecated_finder_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlserver.drop.sql | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlserver.sql | 6 | ||||
-rwxr-xr-x | activerecord/test/inheritance_test.rb | 8 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 4 |
9 files changed, 83 insertions, 13 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 0218f7e6c7..e7160c9365 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -902,6 +902,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase 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") @@ -916,7 +917,13 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase assert_equal 2, ken.projects(true).size kenReloaded = Developer.find_by_name 'Ken' - kenReloaded.projects.each { |prj| assert_equal(now.to_s, prj.joined_on.to_s) } + # SQL Server doesn't have a separate column type just for dates, + # so the time is in the string and incorrectly formatted + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::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 end def test_build @@ -1004,7 +1011,13 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase end def test_additional_columns_from_join_table - assert_equal Date.new(2004, 10, 10).to_s, Developer.find(1).projects.first.joined_on.to_s + # SQL Server doesn't have a separate column type just for dates, + # so the time is in the string and incorrectly formatted + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + assert_equal Time.mktime(2004, 10, 10).strftime("%Y/%m/%d 00:00:00"), Developer.find(1).projects.first.joined_on.to_s + else + assert_equal Date.new(2004, 10, 10).to_s, Developer.find(1).projects.first.joined_on.to_s + end end def test_destroy_all @@ -1019,8 +1032,15 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase def test_rich_association jamis = developers(:jamis) jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) - 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 + # SQL Server doesn't have a separate column type just for dates, + # so the time is in the string and incorrectly formatted + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::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 end def test_associations_with_conditions diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 1c5702741e..b6ae403ca0 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -319,6 +319,10 @@ class BasicsTest < Test::Unit::TestCase end def test_update_all + # The ADO library doesn't support the number of affected rows + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter + return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + end assert_equal 2, Topic.update_all("content = 'bulk updated!'") assert_equal "bulk updated!", Topic.find(1).content assert_equal "bulk updated!", Topic.find(2).content @@ -337,6 +341,10 @@ class BasicsTest < Test::Unit::TestCase end def test_delete_all + # The ADO library doesn't support the number of affected rows + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter + return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + end assert_equal 2, Topic.delete_all end diff --git a/activerecord/test/binary_test.rb b/activerecord/test/binary_test.rb index d635621cef..ba43524264 100644 --- a/activerecord/test/binary_test.rb +++ b/activerecord/test/binary_test.rb @@ -8,6 +8,13 @@ class BinaryTest < Test::Unit::TestCase def test_load_save # Without using prepared statements, it makes no sense to test + # BLOB data with SQL Server, because the length of a statement is + # limited to 8KB. + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter + return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + end + + # Without using prepared statements, it makes no sense to test # BLOB data with DB2, because the length of a statement is # limited to 32KB. if ActiveRecord::ConnectionAdapters.const_defined? :DB2Adapter diff --git a/activerecord/test/connections/native_sqlserver_odbc/connection.rb b/activerecord/test/connections/native_sqlserver_odbc/connection.rb new file mode 100644 index 0000000000..b156a07920 --- /dev/null +++ b/activerecord/test/connections/native_sqlserver_odbc/connection.rb @@ -0,0 +1,26 @@ +print "Using native SQLServer via ODBC\n" +require 'fixtures/course' +require 'logger' + +ActiveRecord::Base.logger = Logger.new("debug.log") + +dsn1 = 'activerecord_unittest' +dsn2 = 'activerecord_unittest2' + +ActiveRecord::Base.establish_connection( + :adapter => "sqlserver", + :mode => "ODBC", + :host => "localhost", + :username => "sa", + :password => "", + :dsn => dsn1 +) + +Course.establish_connection( + :adapter => "sqlserver", + :mode => "ODBC", + :host => "localhost", + :username => "sa", + :password => "", + :dsn => dsn2 +) diff --git a/activerecord/test/deprecated_finder_test.rb b/activerecord/test/deprecated_finder_test.rb index 27a0a66253..0111cff3ac 100755 --- a/activerecord/test/deprecated_finder_test.rb +++ b/activerecord/test/deprecated_finder_test.rb @@ -112,7 +112,7 @@ class FinderTest < Test::Unit::TestCase assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5]) assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0]) end - + def test_find_all_with_limit_and_offset first_three_developers = Developer.find_all nil, 'id ASC', [3, 0] second_three_developers = Developer.find_all nil, 'id ASC', [3, 3] @@ -128,14 +128,13 @@ class FinderTest < Test::Unit::TestCase end def test_find_all_by_one_attribute_with_options - topics = Topic.find_all_by_content("Have a nice day", nil, "id DESC") + topics = Topic.find_all_by_content("Have a nice day", "id DESC") assert topics(:first), topics.last - topics = Topic.find_all_by_content("Have a nice day", nil, "id DESC") + topics = Topic.find_all_by_content("Have a nice day", "id DESC") assert topics(:first), topics.first end - protected def bind(statement, *vars) if vars.first.is_a?(Hash) diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql index efa98fe500..226544392d 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql @@ -21,3 +21,5 @@ DROP TABLE authors; DROP TABLE tasks; DROP TABLE categories; DROP TABLE categories_posts; +DROP TABLE fk_test_has_pd; +DROP TABLE fk_test_has_fk; diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 9c5658e387..1177b37035 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -2,7 +2,7 @@ CREATE TABLE accounts ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, firm_id int default NULL, credit_limit int default NULL -) +); CREATE TABLE companies ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, @@ -12,7 +12,7 @@ CREATE TABLE companies ( name varchar(50) default NULL, client_of int default NULL, rating int default 1 -) +); CREATE TABLE topics ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, @@ -27,7 +27,7 @@ CREATE TABLE topics ( replies_count int default 0, parent_id int default NULL, type varchar(50) default NULL -) +); CREATE TABLE developers ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb index 3ac29b73c8..dbbbb6eee7 100755 --- a/activerecord/test/inheritance_test.rb +++ b/activerecord/test/inheritance_test.rb @@ -6,7 +6,15 @@ class InheritanceTest < Test::Unit::TestCase fixtures :companies, :projects def test_a_bad_type_column + #SQLServer need to turn Identity Insert On before manually inserting into the Identity column + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + Company.connection.execute "SET IDENTITY_INSERT companies ON" + end Company.connection.insert "INSERT INTO companies (id, type, name) VALUES(100, 'bad_class!', 'Not happening')" + #We then need to turn it back Off before continuing. + if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + Company.connection.execute "SET IDENTITY_INSERT companies OFF" + end assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) } end diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 2ff618b0e6..576d1f6c3d 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -707,7 +707,7 @@ class ValidationsTest < Test::Unit::TestCase def test_validates_numericality_of Topic.validates_numericality_of( :approved, :allow_nil => true ) ["10", "10.0", "10.5", "-10.5", "-0.0001","0090","-090","-090.1",nil,""].each do |v| - t = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => v) + t = Topic.new("title" => "numeric test", "content" => "whatever", "approved" => v) assert t.valid?, "#{v} not recognized as a number" # we cannot check this as approved is actually an integer field #assert_in_delta v.to_f, t.approved, 0.0000001 @@ -726,7 +726,7 @@ class ValidationsTest < Test::Unit::TestCase def test_validates_numericality_of_int Topic.validates_numericality_of( :approved, :only_integer => true, :allow_nil => true ) ["42", "+42", "-42", "042", "0042", "-042", 42, nil,""].each do |v| - t = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => v) + t = Topic.new("title" => "numeric test", "content" => "whatever", "approved" => v) assert t.valid?, "#{v} not recognized as integer" assert_equal((v.nil? or v == "")? nil : v.to_i, t.approved) end |