Skip to content

Commit

Permalink
Moving SQL Server tests from AR core to plugin sqlserver adapter. Clo…
Browse files Browse the repository at this point in the history
…ses #9884 [lawrence]
  • Loading branch information
lawrence committed Oct 16, 2007
1 parent 32fbea8 commit 72a3221
Show file tree
Hide file tree
Showing 9 changed files with 849 additions and 1 deletion.
44 changes: 44 additions & 0 deletions RUNNING_UNIT_TESTS
@@ -0,0 +1,44 @@
== Creating the test database

The default names for the test databases are "activerecord_unittest" and
"activerecord_unittest2". If you want to use another database name then be sure
to update the connection adapter setups you want to test with in
test/connections/<your database>/connection.rb.


== Requirements

The tests of this adapter depend on the existence of rails edge. All the tests
defined by rails edge are re-used. For this to work the following directory
structure is assumed to exist:

#{RAILS_ROOT}/vendor/plugins/adapters/sqlserver
#{RAILS_ROOT}/vendor/rails/activerecord/test

Define a user named 'rails' in SQL Server with all privileges granted. Use an empty
password for user 'rails', or alternatively use the OSQLPASSWORD environment variable
which allows you to set a default password for the current session.

Then run "rake create_databases".


== Running with Rake

The easiest way to run the unit tests is through Rake. Either run "rake test_sqlserver"
or "rake test_sqlserver_odbc". For more information, checkout the full array
of rake tasks with "rake -T"

Rake can be found at http://rake.rubyforge.org


== Running by hand

Unit tests are located in test directory. If you only want to run a single test suite,
you can do so with:

rake test_sqlserver TEST=base_test.rb

That'll run the base suite using the SQLServer-Ruby adapter.



42 changes: 41 additions & 1 deletion Rakefile
@@ -1,5 +1,6 @@
require 'rubygems' require 'rubygems'
require 'rake' require 'rake'
require 'rake/testtask'
require 'rake/packagetask' require 'rake/packagetask'
require 'rake/gempackagetask' require 'rake/gempackagetask'
require 'rake/contrib/rubyforgepublisher' require 'rake/contrib/rubyforgepublisher'
Expand Down Expand Up @@ -30,7 +31,6 @@ Rake::GemPackageTask.new(spec) do |p|
p.need_zip = true p.need_zip = true
end end



desc "Publish the beta gem" desc "Publish the beta gem"
task :pgem => :package do task :pgem => :package do
Rake::SshFilePublisher.new("davidhh@wrath.rubyonrails.org", "public_html/gems/gems", "pkg", "#{PKG_NAME}-#{PKG_VERSION}.gem").upload Rake::SshFilePublisher.new("davidhh@wrath.rubyonrails.org", "public_html/gems/gems", "pkg", "#{PKG_NAME}-#{PKG_VERSION}.gem").upload
Expand All @@ -47,3 +47,43 @@ task :release => :package do
rubyforge.login rubyforge.login
rubyforge.add_release(PKG_NAME, PKG_NAME, "REL #{PKG_VERSION}", *packages) rubyforge.add_release(PKG_NAME, PKG_NAME, "REL #{PKG_VERSION}", *packages)
end end


SCHEMA_PATH = File.join(File.dirname(__FILE__), *%w(test fixtures db_definitions))

desc 'Create the SQL Server test databases'
task :create_databases do
# Define a user named 'rails' in SQL Server with all privileges granted
# Use an empty password for user 'rails', or alternatively use the OSQLPASSWORD environment variable
# which allows you to set a default password for the current session.
%x( osql -S localhost -U rails -Q "create database activerecord_unittest" -P )
%x( osql -S localhost -U rails -Q "create database activerecord_unittest2" -P )
%x( osql -S localhost -U rails -d activerecord_unittest -Q "exec sp_grantdbaccess 'rails'" -P )
%x( osql -S localhost -U rails -d activerecord_unittest2 -Q "exec sp_grantdbaccess 'rails'" -P )
%x( osql -S localhost -U rails -d activerecord_unittest -Q "grant BACKUP DATABASE, BACKUP LOG, CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TABLE, CREATE VIEW to 'rails';" -P )
%x( osql -S localhost -U rails -d activerecord_unittest2 -Q "grant BACKUP DATABASE, BACKUP LOG, CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TABLE, CREATE VIEW to 'rails';" -P )
end

desc 'Drop the SQL Server test databases'
task :drop_databases do
%x( osql -S localhost -U rails -Q "drop database activerecord_unittest" -P )
%x( osql -S localhost -U rails -Q "drop database activerecord_unittest2" -P )
end

desc 'Recreate the SQL Server test databases'
task :recreate_databases => [:drop_databases, :create_databases]


for adapter in %w( sqlserver sqlserver_odbc )
Rake::TestTask.new("test_#{adapter}") { |t|
t.libs << "test"
t.libs << "test/connections/native_#{adapter}"
t.libs << "../../../rails/activerecord/test/"
t.pattern = ["test/**/*_test_sqlserver.rb", "../../../rails/activerecord/test/**/*_test.rb"]
t.verbose = true
}

namespace adapter do
task :test => "test_#{adapter}"
end
end
88 changes: 88 additions & 0 deletions test/aaaa_create_tables_test_sqlserver.rb
@@ -0,0 +1,88 @@
# The filename begins with "aaaa" to ensure this is the first test.
require 'abstract_unit'

class AAAACreateTablesTestSqlserver < Test::Unit::TestCase
self.use_transactional_fixtures = false

def setup
@ar_path = "../../../rails/activerecord/test/fixtures/db_definitions"
@base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
end

def test_sqlserver_load_test_schema
execute_sql_file("#{@base_path}/sqlserver.drop.sql", ActiveRecord::Base.connection)
execute_sql_file("#{@base_path}/sqlserver.sql", ActiveRecord::Base.connection)
execute_sql_file("#{@base_path}/sqlserver2.drop.sql", Course.connection)
execute_sql_file("#{@base_path}/sqlserver2.sql", Course.connection)
assert true
end

#FUTURE
def __test_activerecord_load_test_schema
#FUTURE: eval(File.read("#{@ar_path}/schema.rb"))
eval(File.read("#{@base_path}/schema.rb"))
connection = ActiveRecord::Base.connection
begin
ActiveRecord::Base.connection = Course.connection
#FUTURE: eval(File.read("#{@ar_path}/schema2.rb"))
eval(File.read("#{@base_path}/schema2.rb"))
ensure
ActiveRecord::Base.connection = connection
end
assert true
end

private

def execute_sql_file(path, connection)
File.read(path).split(';').each_with_index do |sql, i|
begin
connection.execute("\n\n-- statement ##{i}\n#{sql}\n") unless sql.blank?
rescue ActiveRecord::StatementInvalid
#$stderr.puts "warning: #{$!}"
end
end
# The filename begins with "aaaa" to ensure this is the first test.
require 'abstract_unit'

class AAAACreateTablesTestSqlserver < Test::Unit::TestCase
self.use_transactional_fixtures = false

def setup
@ar_path = "../../../rails/activerecord/test/fixtures/db_definitions"
@base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
end

def test_sqlserver_load_test_schema
execute_sql_file("#{@base_path}/sqlserver.drop.sql", ActiveRecord::Base.connection)
execute_sql_file("#{@base_path}/sqlserver.sql", ActiveRecord::Base.connection)
execute_sql_file("#{@base_path}/sqlserver2.drop.sql", Course.connection)
execute_sql_file("#{@base_path}/sqlserver2.sql", Course.connection)
assert true
end

#FUTURE
def __test_activerecord_load_test_schema
#FUTURE: eval(File.read("#{@ar_path}/schema.rb"))
eval(File.read("#{@base_path}/schema.rb"))
connection = ActiveRecord::Base.connection
begin
ActiveRecord::Base.connection = Course.connection
#FUTURE: eval(File.read("#{@ar_path}/schema2.rb"))
eval(File.read("#{@base_path}/schema2.rb"))
ensure
ActiveRecord::Base.connection = connection
end
assert true
end

private

def execute_sql_file(path, connection)
File.read(path).split(';').each_with_index do |sql, i|
begin
connection.execute("\n\n-- statement ##{i}\n#{sql}\n") unless sql.blank?
rescue ActiveRecord::StatementInvalid
#$stderr.puts "warning: #{$!}"
end
end
46 changes: 46 additions & 0 deletions test/connections/native_sqlserver/connection.rb
@@ -0,0 +1,46 @@
print "Using native SQLServer\n"
require_dependency 'fixtures/course'
require 'logger'

ActiveRecord::Base.logger = Logger.new("debug.log")

ActiveRecord::Base.configurations = {
'arunit' => {
:adapter => 'sqlserver',
:host => 'localhost',
:username => 'rails',
:database => 'activerecord_unittest'
},
'arunit2' => {
:adapter => 'sqlserver',
:host => 'localhost',
:username => 'rails',
:database => 'activerecord_unittest2'
}
}

ActiveRecord::Base.establish_connection 'arunit'
Course.establish_connection 'arunit2'
print "Using native SQLServer\n"
require_dependency 'fixtures/course'
require 'logger'

ActiveRecord::Base.logger = Logger.new("debug.log")

ActiveRecord::Base.configurations = {
'arunit' => {
:adapter => 'sqlserver',
:host => 'localhost',
:username => 'rails',
:database => 'activerecord_unittest'
},
'arunit2' => {
:adapter => 'sqlserver',
:host => 'localhost',
:username => 'rails',
:database => 'activerecord_unittest2'
}
}

ActiveRecord::Base.establish_connection 'arunit'
Course.establish_connection 'arunit2'
50 changes: 50 additions & 0 deletions test/connections/native_sqlserver_odbc/connection.rb
@@ -0,0 +1,50 @@
print "Using native SQLServer via ODBC\n"
require_dependency 'fixtures/course'
require 'logger'

ActiveRecord::Base.logger = Logger.new("debug.log")

ActiveRecord::Base.configurations = {
'arunit' => {
:adapter => 'sqlserver',
:mode => 'ODBC',
:host => 'localhost',
:username => 'rails',
:dsn => 'activerecord_unittest'
},
'arunit2' => {
:adapter => 'sqlserver',
:mode => 'ODBC',
:host => 'localhost',
:username => 'rails',
:dsn => 'activerecord_unittest2'
}
}

ActiveRecord::Base.establish_connection 'arunit'
Course.establish_connection 'arunit2'
print "Using native SQLServer via ODBC\n"
require_dependency 'fixtures/course'
require 'logger'

ActiveRecord::Base.logger = Logger.new("debug.log")

ActiveRecord::Base.configurations = {
'arunit' => {
:adapter => 'sqlserver',
:mode => 'ODBC',
:host => 'localhost',
:username => 'rails',
:dsn => 'activerecord_unittest'
},
'arunit2' => {
:adapter => 'sqlserver',
:mode => 'ODBC',
:host => 'localhost',
:username => 'rails',
:dsn => 'activerecord_unittest2'
}
}

ActiveRecord::Base.establish_connection 'arunit'
Course.establish_connection 'arunit2'
70 changes: 70 additions & 0 deletions test/fixtures/db_definitions/sqlserver.drop.sql
@@ -0,0 +1,70 @@
DROP TABLE accounts;
DROP TABLE funny_jokes;
DROP TABLE companies;
DROP TABLE topics;
DROP TABLE developers;
DROP TABLE projects;
DROP TABLE developers_projects;
DROP TABLE customers;
DROP TABLE orders;
DROP TABLE movies;
DROP TABLE subscribers;
DROP TABLE booleantests;
DROP TABLE defaults;
DROP TABLE auto_id_tests;
DROP TABLE entrants;
DROP TABLE colnametests;
DROP TABLE mixins;
DROP TABLE people;
DROP TABLE readers;
DROP TABLE binaries;
DROP TABLE computers;
DROP TABLE posts;
DROP TABLE comments;
DROP TABLE authors;
DROP TABLE tasks;
DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
DROP TABLE mixed_case_monkeys;
DROP TABLE minimalistics;
DROP TABLE accounts;
DROP TABLE funny_jokes;
DROP TABLE companies;
DROP TABLE topics;
DROP TABLE developers;
DROP TABLE projects;
DROP TABLE developers_projects;
DROP TABLE customers;
DROP TABLE orders;
DROP TABLE movies;
DROP TABLE subscribers;
DROP TABLE booleantests;
DROP TABLE defaults;
DROP TABLE auto_id_tests;
DROP TABLE entrants;
DROP TABLE colnametests;
DROP TABLE mixins;
DROP TABLE people;
DROP TABLE readers;
DROP TABLE binaries;
DROP TABLE computers;
DROP TABLE posts;
DROP TABLE comments;
DROP TABLE authors;
DROP TABLE tasks;
DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
DROP TABLE mixed_case_monkeys;
DROP TABLE minimalistics;

0 comments on commit 72a3221

Please sign in to comment.