Sha256: b8e74499d7276eadd3bdd62ed5274c16a011b3e1259c8353535028d605d31ed0

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

require_relative '../../spec_helper'
require 'sql/maker/condition'

describe 'SQL::Maker::Condition' do
  let(:w1) do
    w1 = SQL::Maker::Condition.new()
    w1.add(:x => 1)
    w1.add(:y => 2)
  end
  let(:w2) { SQL::Maker::Condition.new() }
  let(:w3) { SQL::Maker::Condition.new() }

  it 'and_before' do
    w = (w1 & w2)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?))'
    expect(w.bind.join(', ')).to be == '1, 2'

    w.add(:z => 99)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?)) AND (z = ?)'
    expect(w.bind.join(', ')).to be == '1, 2, 99'
  end

  it 'and_after' do 
    w = (w2 & w1)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?))'
    expect(w.bind.join(', ')).to be == '1, 2'

    w.add(:z => 99)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?)) AND (z = ?)'
    expect(w.bind.join(', ')).to be == '1, 2, 99'
  end

  it 'or_before' do
    w = (w1 | w2)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?))'
    expect(w.bind.join(', ')).to be == '1, 2'

    w.add(:z => 99)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?)) AND (z = ?)'
    expect(w.bind.join(', ')).to be == '1, 2, 99'
  end

  it 'or_after' do
    w = (w2 | w1)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?))'
    expect(w.bind.join(', ')).to be == '1, 2'

    w.add(:z => 99)
    expect(w.as_sql).to be == '((x = ?) AND (y = ?)) AND (z = ?)'
    expect(w.bind.join(', ')).to be == '1, 2, 99'
  end

  it 'and_both' do
    w = (w2 & w3)
    expect(w.as_sql).to be == ''
    expect(w.bind.join(', ')).to be == ''

    w.add(:z => 99)
    expect(w.as_sql).to be == '(z = ?)'
    expect(w.bind.join(', ')).to be == '99'
  end

  it 'or_both' do
    w = (w2 | w3)
    expect(w.as_sql).to be == ''
    expect(w.bind.join(', ')).to be == ''

    w.add(:z => 99)
    expect(w.as_sql).to be == '(z = ?)'
    expect(w.bind.join(', ')).to be == '99'
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sql-maker-1.0.0 spec/maker/condition/compose_empty_spec.rb
sql-maker-0.0.5 spec/maker/condition/compose_empty_spec.rb
sql-maker-0.0.4 spec/maker/condition/compose_empty_spec.rb
sql-maker-0.0.3 spec/maker/condition/compose_empty_spec.rb
sql-maker-0.0.2 spec/maker/condition/compose_empty_spec.rb
sql-maker-0.0.1 spec/maker/condition/compose_empty_spec.rb