|
5 | 5 | require 'models/subscriber' |
6 | 6 |
|
7 | 7 | class AdapterTestSqlserver < ActiveRecord::TestCase |
| 8 | + |
| 9 | + fixtures :tasks |
8 | 10 |
|
9 | 11 | def setup |
10 | 12 | @connection = ActiveRecord::Base.connection |
@@ -377,65 +379,75 @@ def setup |
377 | 379 | context 'For DatabaseStatements' do |
378 | 380 |
|
379 | 381 | context "finding out what user_options are available" do |
| 382 | + |
380 | 383 | should "run the database consistency checker useroptions command" do |
381 | 384 | @connection.expects(:select_rows).with(regexp_matches(/^dbcc\s+useroptions$/i)).returns [] |
382 | 385 | @connection.user_options |
383 | 386 | end |
384 | 387 |
|
385 | | - should "return a symbolized hash of the results" do |
386 | | - @connection.expects(:select_rows).with(regexp_matches(/^dbcc\s+useroptions$/i)).returns [['some', 'thing'], ['an', 'other thing']] |
387 | | - res = @connection.user_options |
388 | | - assert_equal 'thing', res[:some] |
389 | | - assert_equal 'other thing', res[:an] |
390 | | - assert_equal 2, res.keys.size |
| 388 | + should "return a underscored key hash with indifferent access of the results" do |
| 389 | + @connection.expects(:select_rows).with(regexp_matches(/^dbcc\s+useroptions$/i)).returns [['some', 'thing'], ['isolation level', 'read uncommitted']] |
| 390 | + uo = @connection.user_options |
| 391 | + assert_equal 2, uo.keys.size |
| 392 | + assert_equal 'thing', uo['some'] |
| 393 | + assert_equal 'thing', uo[:some] |
| 394 | + assert_equal 'read uncommitted', uo['isolation_level'] |
| 395 | + assert_equal 'read uncommitted', uo[:isolation_level] |
391 | 396 | end |
| 397 | + |
392 | 398 | end |
393 | 399 |
|
394 | 400 | context "altering isolation levels" do |
| 401 | + |
395 | 402 | should "barf if the requested isolation level is not valid" do |
396 | | - @connection.class::VALID_ISOLATION_LEVELS.expects(:include?).returns false |
397 | 403 | assert_raise(ArgumentError) do |
398 | | - @connection.run_with_isolation_level 'something' do; end |
| 404 | + @connection.run_with_isolation_level 'INVALID ISOLATION LEVEL' do; end |
399 | 405 | end |
400 | 406 | end |
401 | 407 |
|
402 | 408 | context "with a valid isolation level" do |
| 409 | + |
403 | 410 | setup do |
404 | | - @connection.class::VALID_ISOLATION_LEVELS.expects(:include?).returns true |
405 | | - @connection.stubs(:user_options).returns({:"isolation level" => "something"}) |
406 | | - @yieldy = states('yield').starts_as(:not_yielded) |
| 411 | + @t1 = tasks(:first_task) |
| 412 | + @t2 = tasks(:another_task) |
| 413 | + assert @t1, 'Tasks :first_task should be in AR fixtures' |
| 414 | + assert @t2, 'Tasks :another_task should be in AR fixtures' |
| 415 | + good_isolation_level = @connection.user_options[:isolation_level].blank? || @connection.user_options[:isolation_level] =~ /read committed/i |
| 416 | + assert good_isolation_level, "User isolation level is not at a happy starting place: #{@connection.user_options[:isolation_level].inspect}" |
407 | 417 | end |
408 | 418 |
|
409 | | - should "set the isolation level to that supplied before calling the supplied block" do |
410 | | - @connection.expects(:execute).with(regexp_matches(/set transaction isolation level new_isolation_level/i)).when(@yieldy.is(:not_yielded)) |
411 | | - @connection.stubs(:execute).when(@yieldy.is(:yielded)) |
412 | | - |
413 | | - @connection.run_with_isolation_level 'new_isolation_level' do |
414 | | - @yieldy.become(:yielded) |
| 419 | + should 'allow #run_with_isolation_level to not take a block to set it' do |
| 420 | + begin |
| 421 | + @connection.run_with_isolation_level 'READ UNCOMMITTED' |
| 422 | + assert_match %r|read uncommitted|i, @connection.user_options[:isolation_level] |
| 423 | + ensure |
| 424 | + @connection.run_with_isolation_level 'READ COMMITTED' |
415 | 425 | end |
416 | 426 | end |
417 | | - |
418 | | - should "set the isolation level back to the original after calling the supplied block" do |
419 | | - @connection.expects(:execute).with(regexp_matches(/set transaction isolation level something/i)).when(@yieldy.is(:yielded)) |
420 | | - @connection.stubs(:execute).when(@yieldy.is(:not_yielded)) |
421 | 427 |
|
422 | | - @connection.run_with_isolation_level 'new_isolation_level' do |
423 | | - @yieldy.become(:yielded) |
424 | | - end |
| 428 | + should 'return block value using #run_with_isolation_level' do |
| 429 | + assert_same_elements Task.find(:all), @connection.run_with_isolation_level('READ UNCOMMITTED') { Task.find(:all) } |
425 | 430 | end |
426 | | - |
427 | | - should "set the isolation level back to the original after calling the supplied block even when the block raises an exception" do |
428 | | - @connection.expects(:execute).with(regexp_matches(/set transaction isolation level something/i)).when(@yieldy.is(:yielded)) |
429 | | - @connection.stubs(:execute).when(@yieldy.is(:not_yielded)) |
430 | 431 |
|
431 | | - assert_raise(RuntimeError) do |
432 | | - @connection.run_with_isolation_level 'new_isolation_level' do |
433 | | - @yieldy.become(:yielded) |
434 | | - raise "a problem" |
| 432 | + should 'pass a read uncommitted isolation level test' do |
| 433 | + assert_nil @t2.starting, 'Fixture should have this empty.' |
| 434 | + begin |
| 435 | + Task.transaction do |
| 436 | + @t2.starting = Time.now |
| 437 | + @t2.save |
| 438 | + @dirty_t2 = @connection.run_with_isolation_level('READ UNCOMMITTED') { Task.find(@t2.id) } |
| 439 | + raise ActiveRecord::ActiveRecordError |
435 | 440 | end |
| 441 | + rescue |
| 442 | + 'Do Nothing' |
436 | 443 | end |
| 444 | + assert @dirty_t2, 'Should have a Task record from within block above.' |
| 445 | + assert @dirty_t2.starting, 'Should have a dirty date.' |
| 446 | + assert_nil Task.find(@t2.id).starting, 'Should be nil again from botched transaction above.' |
437 | 447 | end |
| 448 | + |
438 | 449 | end |
| 450 | + |
439 | 451 | end |
440 | 452 |
|
441 | 453 | end |
|
0 commit comments