| 1 | | require 'abstract_unit' |
|---|
| 2 | | require 'fixtures/default' |
|---|
| 3 | | require 'fixtures/post' |
|---|
| 4 | | require 'fixtures/task' |
|---|
| 5 | | |
|---|
| 6 | | class SqlServerAdapterTest < Test::Unit::TestCase |
|---|
| 7 | | fixtures :posts, :tasks |
|---|
| 8 | | |
|---|
| 9 | | def setup |
|---|
| 10 | | @connection = ActiveRecord::Base.connection |
|---|
| 11 | | end |
|---|
| 12 | | |
|---|
| 13 | | def teardown |
|---|
| 14 | | @connection.execute("SET LANGUAGE us_english") |
|---|
| 15 | | end |
|---|
| 16 | | |
|---|
| 17 | | # SQL Server 2000 has a bug where some unambiguous date formats are not |
|---|
| 18 | | # correctly identified if the session language is set to german |
|---|
| 19 | | def test_date_insertion_when_language_is_german |
|---|
| 20 | | @connection.execute("SET LANGUAGE deutsch") |
|---|
| 21 | | |
|---|
| 22 | | assert_nothing_raised do |
|---|
| 23 | | Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) |
|---|
| 24 | | end |
|---|
| 25 | | end |
|---|
| 26 | | |
|---|
| 27 | | def test_execute_without_block_closes_statement |
|---|
| 28 | | assert_all_statements_used_are_closed do |
|---|
| 29 | | @connection.execute("SELECT 1") |
|---|
| 30 | | end |
|---|
| 31 | | end |
|---|
| 32 | | |
|---|
| 33 | | def test_execute_with_block_closes_statement |
|---|
| 34 | | assert_all_statements_used_are_closed do |
|---|
| 35 | | @connection.execute("SELECT 1") do |sth| |
|---|
| 36 | | assert !sth.finished?, "Statement should still be alive within block" |
|---|
| 37 | | end |
|---|
| 38 | | end |
|---|
| 39 | | end |
|---|
| 40 | | |
|---|
| 41 | | def test_insert_with_identity_closes_statement |
|---|
| 42 | | assert_all_statements_used_are_closed do |
|---|
| 43 | | @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") |
|---|
| 44 | | end |
|---|
| 45 | | end |
|---|
| 46 | | |
|---|
| 47 | | def test_insert_without_identity_closes_statement |
|---|
| 48 | | assert_all_statements_used_are_closed do |
|---|
| 49 | | @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") |
|---|
| 50 | | end |
|---|
| 51 | | end |
|---|
| 52 | | |
|---|
| 53 | | def test_active_closes_statement |
|---|
| 54 | | assert_all_statements_used_are_closed do |
|---|
| 55 | | @connection.active? |
|---|
| 56 | | end |
|---|
| 57 | | end |
|---|
| 58 | | |
|---|
| 59 | | def assert_all_statements_used_are_closed(&block) |
|---|
| 60 | | existing_handles = [] |
|---|
| 61 | | ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} |
|---|
| 62 | | GC.disable |
|---|
| 63 | | |
|---|
| 64 | | yield |
|---|
| 65 | | |
|---|
| 66 | | used_handles = [] |
|---|
| 67 | | ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} |
|---|
| 68 | | |
|---|
| 69 | | assert_block "No statements were used within given block" do |
|---|
| 70 | | used_handles.size > 0 |
|---|
| 71 | | end |
|---|
| 72 | | |
|---|
| 73 | | ObjectSpace.each_object(DBI::StatementHandle) do |handle| |
|---|
| 74 | | assert_block "Statement should have been closed within given block" do |
|---|
| 75 | | handle.finished? |
|---|
| 76 | | end |
|---|
| 77 | | end |
|---|
| 78 | | ensure |
|---|
| 79 | | GC.enable |
|---|
| 80 | | end |
|---|
| 81 | | end |
|---|
| | 1 | require 'abstract_unit' |
|---|
| | 2 | require 'fixtures/default' |
|---|
| | 3 | require 'fixtures/post' |
|---|
| | 4 | require 'fixtures/task' |
|---|
| | 5 | |
|---|
| | 6 | class SqlServerAdapterTest < Test::Unit::TestCase |
|---|
| | 7 | fixtures :posts, :tasks |
|---|
| | 8 | |
|---|
| | 9 | def setup |
|---|
| | 10 | @connection = ActiveRecord::Base.connection |
|---|
| | 11 | end |
|---|
| | 12 | |
|---|
| | 13 | def teardown |
|---|
| | 14 | @connection.execute("SET LANGUAGE us_english") |
|---|
| | 15 | end |
|---|
| | 16 | |
|---|
| | 17 | # SQL Server 2000 has a bug where some unambiguous date formats are not |
|---|
| | 18 | # correctly identified if the session language is set to german |
|---|
| | 19 | def test_date_insertion_when_language_is_german |
|---|
| | 20 | @connection.execute("SET LANGUAGE deutsch") |
|---|
| | 21 | |
|---|
| | 22 | assert_nothing_raised do |
|---|
| | 23 | Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) |
|---|
| | 24 | end |
|---|
| | 25 | end |
|---|
| | 26 | |
|---|
| | 27 | def test_execute_without_block_closes_statement |
|---|
| | 28 | assert_all_statements_used_are_closed do |
|---|
| | 29 | @connection.execute("SELECT 1") |
|---|
| | 30 | end |
|---|
| | 31 | end |
|---|
| | 32 | |
|---|
| | 33 | def test_execute_with_block_closes_statement |
|---|
| | 34 | assert_all_statements_used_are_closed do |
|---|
| | 35 | @connection.execute("SELECT 1") do |sth| |
|---|
| | 36 | assert !sth.finished?, "Statement should still be alive within block" |
|---|
| | 37 | end |
|---|
| | 38 | end |
|---|
| | 39 | end |
|---|
| | 40 | |
|---|
| | 41 | def test_insert_with_identity_closes_statement |
|---|
| | 42 | assert_all_statements_used_are_closed do |
|---|
| | 43 | @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") |
|---|
| | 44 | end |
|---|
| | 45 | end |
|---|
| | 46 | |
|---|
| | 47 | def test_insert_without_identity_closes_statement |
|---|
| | 48 | assert_all_statements_used_are_closed do |
|---|
| | 49 | @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") |
|---|
| | 50 | end |
|---|
| | 51 | end |
|---|
| | 52 | |
|---|
| | 53 | def test_active_closes_statement |
|---|
| | 54 | assert_all_statements_used_are_closed do |
|---|
| | 55 | @connection.active? |
|---|
| | 56 | end |
|---|
| | 57 | end |
|---|
| | 58 | |
|---|
| | 59 | def assert_all_statements_used_are_closed(&block) |
|---|
| | 60 | existing_handles = [] |
|---|
| | 61 | ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} |
|---|
| | 62 | GC.disable |
|---|
| | 63 | |
|---|
| | 64 | yield |
|---|
| | 65 | |
|---|
| | 66 | used_handles = [] |
|---|
| | 67 | ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} |
|---|
| | 68 | |
|---|
| | 69 | assert_block "No statements were used within given block" do |
|---|
| | 70 | used_handles.size > 0 |
|---|
| | 71 | end |
|---|
| | 72 | |
|---|
| | 73 | ObjectSpace.each_object(DBI::StatementHandle) do |handle| |
|---|
| | 74 | assert_block "Statement should have been closed within given block" do |
|---|
| | 75 | handle.finished? |
|---|
| | 76 | end |
|---|
| | 77 | end |
|---|
| | 78 | ensure |
|---|
| | 79 | GC.enable |
|---|
| | 80 | end |
|---|
| | 81 | end |