1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
require_relative 'spec_helper'
describe "synchronize_sql extension" do
module Sync
private
def literal_string_append(sql, v)
db.synchronize{super}
end
end
before do
@db = Sequel.mock
@db.pool.extend(Module.new do
def assign_connection(*args)
r = super
@times_connection_acquired ||= 0
@times_connection_acquired += 1 if r
return r
end
def times_connection_acquired
v = @times_connection_acquired
@times_connection_acquired = 0
v || 0
end
end)
@db.extend_datasets(Sync)
@ds = @db[:tab1]
end
it 'does not checkout a connection if SQL is given as a string' do
@ds.extension(:synchronize_sql).with_sql('SELECT 1').sql
@db.pool.times_connection_acquired.must_equal 0
end
it 'checks out an extra connection on insert_sql if there are no strings' do
@ds.insert_sql(:numeric_foo => 8)
@db.pool.times_connection_acquired.must_equal 0
extds = @ds.extension(:synchronize_sql)
extds.insert_sql(:numeric_foo => 8)
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out just one connection on insert_sql if there are multiple strings' do
@ds.insert_sql(:string_foo1 => 'eight', :string_foo2 => 'nine', :string_foo3 => 'ten')
@db.pool.times_connection_acquired.must_equal 3
extds = @ds.extension(:synchronize_sql)
extds.insert_sql(:string_foo1 => 'eight', :string_foo2 => 'nine', :string_foo3 => 'ten')
@db.pool.times_connection_acquired.must_equal 1
end
it 'cheks out an extra connectrion on update_sql if there are no strings' do
@ds.where(:numeric_foo => [1, 2, 3, 4, 5]).update_sql(:numeric_foo => 99)
@db.pool.times_connection_acquired.must_equal 0
extds = @ds.extension(:synchronize_sql)
extds.where(:numeric_foo => [1, 2, 3, 4, 5]).update_sql(:numeric_foo => 99)
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out just one connection on update_sql if there are multiple strings' do
@ds.where(:numeric_foo => [1, 2, 3, 4, 5]).update_sql(:string_foo1 => 'eight', :string_foo2 => 'nine', :string_foo3 => 'ten')
@db.pool.times_connection_acquired.must_equal 3
extds = @ds.extension(:synchronize_sql)
extds.where(:numeric_foo => [1, 2, 3, 4, 5]).update_sql(:string_foo1 => 'eight', :string_foo2 => 'nine', :string_foo3 => 'ten')
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out an extra connection on delete_sql if there are no strings' do
@ds.where(:numeric_foo => [1, 2, 3]).delete_sql
@db.pool.times_connection_acquired.must_equal 0
extds = @ds.extension(:synchronize_sql)
extds.where(:numeric_foo => [1, 2, 3]).delete_sql
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out just one connection on delete_sql if there are multiple strings' do
@ds.where(:string_foo => ['one', 'two', 'three', 'four']).delete_sql
@db.pool.times_connection_acquired.must_equal 4
extds = @ds.extension(:synchronize_sql)
extds.where(:string_foo => ['one', 'two', 'three', 'four']).delete_sql
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out an extra connection on select_sql if there are no strings' do
@ds.where(:numeric_foo => [1, 2, 3]).select_sql
@db.pool.times_connection_acquired.must_equal 0
extds = @ds.extension(:synchronize_sql)
extds.where(:numeric_foo => [1, 2, 3]).select_sql
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out just one connection on select_sql if there are multiple strings' do
@ds.where(:string_foo => ['one', 'two', 'three', 'four']).select_sql
@db.pool.times_connection_acquired.must_equal 4
extds = @ds.extension(:synchronize_sql)
extds.where(:string_foo => ['one', 'two', 'three', 'four']).select_sql
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out an extra connection on fetch if there are no strings' do
@db.fetch('SELECT * FROM tab1 WHERE numeric_foo IN (?, ?, ?, ?)', 1, 2, 3, 4).select_sql
@db.pool.times_connection_acquired.must_equal 0
@db.extension(:synchronize_sql)
@db.fetch('SELECT * FROM tab1 WHERE numeric_foo IN (?, ?, ?, ?)', 1, 2, 3, 4).select_sql
@db.pool.times_connection_acquired.must_equal 1
end
it 'checks out just one connection on fetch if there are multiple strings' do
@db.fetch('SELECT * FROM tab1 WHERE string_foo IN (?, ?, ?, ?)', 'one', 'two', 'three', 'four').select_sql
@db.pool.times_connection_acquired.must_equal 4
@db.extension(:synchronize_sql)
@db.fetch('SELECT * FROM tab1 WHERE string_foo IN (?, ?, ?, ?)', 'one', 'two', 'three', 'four').select_sql
@db.pool.times_connection_acquired.must_equal 1
end
end
|