Module: ActiveRecord::Metal::Postgresql::PreparedQueries::Etest

Includes:
EtestBase
Defined in:
lib/active_record/metal/postgresql/prepared_queries.rb

Constant Summary

Constants included from EtestBase

EtestBase::SELF

Instance Method Summary collapse

Methods included from EtestBase

#count, load_expectation_assertions, #metal, #setup

Instance Method Details

#test_prepared_queries_housekeepingObject



60
61
62
63
64
65
66
67
# File 'lib/active_record/metal/postgresql/prepared_queries.rb', line 60

def test_prepared_queries_housekeeping
  # if we have two metal adapters working on the same connection,
  # one must not affect the prepared queries of the other.
  alloys = metal.ask "SELECT COUNT(*) FROM alloys WHERE id >= $1", 0
  other_metal = ActiveRecord::Metal.new
  expect! other_metal.ask("SELECT COUNT(*) FROM alloys WHERE id >= $1", 0) => alloys
  expect! metal.ask("SELECT COUNT(*) FROM alloys WHERE id >= $1", 0) => alloys
end

#test_prepared_query_fails_during_importObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_record/metal/postgresql/prepared_queries.rb', line 69

def test_prepared_query_fails_during_import
  metal.ask "DELETE FROM alloys"
  
  # If a prepared query fails, eg. during an import, the transaction
  # will fail and cancelled. This also means that there is no longer
  # a way to clean up the prepared query in the transaction, because
  # this fails with a "transaction failed already" error.
  query = metal.prepare "INSERT INTO alloys (id, num) VALUES($1, $1)"

  assert_raise(PG::Error) {  
    records = [ [1,1], [1,1] ]
    metal.import "alloys", records, :columns => [ "id", "num"]
  }

  # Note: after that point we can no longer do anything in this test.
  # This is because a test is wrapped in a transaction, and this
  # transaction is aborted.
  # expect! metal.count("alloys") => 0
end

#test_transaction_abortedObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/active_record/metal/postgresql/prepared_queries.rb', line 89

def test_transaction_aborted
  metal.ask "DELETE FROM alloys"
  metal.transaction do
    metal.ask "INSERT INTO alloys (id, num) VALUES($1, $1)", 1
    
    # "duplicate key value violates unique constraint"
    assert_raise(PG::Error) {  
      metal.ask "INSERT INTO alloys (id, num) VALUES($1, $1)", 1
    }

    # "current transaction is aborted"
    assert_raise(PG::Error) {  
      metal.ask "INSERT INTO alloys (id, num) VALUES($1, $1)", 2
    }
  end
end