Sha256: 8da12d9148c3f915efb2353bf9ba0b7fa92551e0109ecc055d41f0d34209a68f

Contents?: true

Size: 1.76 KB

Versions: 44

Compression:

Stored size: 1.76 KB

Contents

require File.expand_path('../abstract_unit', __FILE__)

class TestPredicates < ActiveSupport::TestCase
  fixtures :departments

  include CompositePrimaryKeys::Predicates

  def test_or
    dep = Department.arel_table

    predicates = Array.new

    3.times do |i|
      predicates << dep[:id].eq(i)
    end

    connection = ActiveRecord::Base.connection
    quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
    expected = "(#{quoted} = 0 OR #{quoted} = 1 OR #{quoted} = 2)"

    pred = cpk_or_predicate(predicates)
    assert_equal(with_quoted_identifiers(expected), pred.to_sql)
  end

  def test_or_with_many_values
    dep = Arel::Table.new(:departments)

    predicates = Array.new

    number_of_predicates = 3000 # This should really be big
    number_of_predicates.times do |i|
      predicates << dep[:id].eq(i)
    end

    connection = ActiveRecord::Base.connection
    quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
    expected_ungrouped = ((0...number_of_predicates).map { |i| "#{quoted} = #{i}" }).join(' OR ')
    expected = "(#{expected_ungrouped})"

    pred = cpk_or_predicate(predicates)
    assert_equal(with_quoted_identifiers(expected), pred.to_sql)
  end

  def test_and
    dep = Department.arel_table

    predicates = Array.new

    3.times do |i|
      predicates << dep[:id].eq(i)
    end

    connection = ActiveRecord::Base.connection
    quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
    expected = "#{quoted} = 0 AND #{quoted} = 1 AND #{quoted} = 2"

    pred = cpk_and_predicate(predicates)
    assert_equal(with_quoted_identifiers(expected), pred.to_sql)
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
composite_primary_keys-13.0.1 test/test_predicates.rb
composite_primary_keys-13.0.0 test/test_predicates.rb
composite_primary_keys-12.0.10 test/test_predicates.rb
composite_primary_keys-12.0.9 test/test_predicates.rb
composite_primary_keys-12.0.8 test/test_predicates.rb
composite_primary_keys-12.0.6 test/test_predicates.rb
composite_primary_keys-12.0.5 test/test_predicates.rb
composite_primary_keys-12.0.4 test/test_predicates.rb
composite_primary_keys-12.0.3 test/test_predicates.rb
composite_primary_keys-12.0.2 test/test_predicates.rb
composite_primary_keys-11.3.1 test/test_predicates.rb
composite_primary_keys-12.0.1 test/test_predicates.rb
composite_primary_keys-12.0.0 test/test_predicates.rb
composite_primary_keys-12.0.0.rc5 test/test_predicates.rb
composite_primary_keys-12.0.0.rc4 test/test_predicates.rb
composite_primary_keys-12.0.0.rc3 test/test_predicates.rb
composite_primary_keys-12.0.0.rc2 test/test_predicates.rb
composite_primary_keys-12.0.0.rc1 test/test_predicates.rb
composite_primary_keys-11.2.0 test/test_predicates.rb
composite_primary_keys-11.1.0 test/test_predicates.rb