class ActiveRecord::WhereTest

Public Class Methods

method_missing(method, *args, &block) click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 193
def self.method_missing(method, *args, &block)
  Treasure.send(method, *args, &block)
end
new(parameters) click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 341
def initialize(parameters)
  @parameters = parameters
  @permitted = false
end

Public Instance Methods

is_a?(klass) click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 197
def is_a?(klass)
  model.is_a?(klass)
end
method_missing(method, *args, &block) click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 201
def method_missing(method, *args, &block)
  model.send(method, *args, &block)
end
permit!() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 350
def permit!
  @permitted = true
  self
end
test_aliased_attribute() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 216
def test_aliased_attribute
  expected = Topic.where(heading: "The First Topic")
  actual   = Topic.where(title: "The First Topic")

  assert_equal expected.to_sql, actual.to_sql
end
test_belongs_to_array_value_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 72
def test_belongs_to_array_value_where
  assert_equal Post.where(author_id: [1, 2]).to_sql, Post.where(author: [1, 2]).to_sql
end
test_belongs_to_nested_relation_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 76
def test_belongs_to_nested_relation_where
  expected = Post.where(author_id: Author.where(id: [1, 2])).to_sql
  actual   = Post.where(author:    Author.where(id: [1, 2])).to_sql

  assert_equal expected, actual
end
test_belongs_to_nested_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 83
def test_belongs_to_nested_where
  parent = Comment.new
  parent.id = 1

  expected = Post.where(comments: { parent_id: 1 }).joins(:comments)
  actual   = Post.where(comments: { parent: parent }).joins(:comments)

  assert_equal expected.to_sql, actual.to_sql
end
test_belongs_to_nested_where_with_relation() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 93
def test_belongs_to_nested_where_with_relation
  author = authors(:david)

  expected = Author.where(id: author).joins(:posts)
  actual   = Author.where(posts: { author_id: Author.where(id: author.id) }).joins(:posts)

  assert_equal expected.to_a, actual.to_a
end
test_belongs_to_nil_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 68
def test_belongs_to_nil_where
  assert_equal Post.where(author_id: nil).to_sql, Post.where(author: nil).to_sql
end
test_belongs_to_shallow_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 61
def test_belongs_to_shallow_where
  author = Author.new
  author.id = 1

  assert_equal Post.where(author_id: 1).to_sql, Post.where(author: author).to_sql
end
test_decorated_polymorphic_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 191
def test_decorated_polymorphic_where
  treasure_decorator = Struct.new(:model) do
    def self.method_missing(method, *args, &block)
      Treasure.send(method, *args, &block)
    end

    def is_a?(klass)
      model.is_a?(klass)
    end

    def method_missing(method, *args, &block)
      model.send(method, *args, &block)
    end
  end

  treasure = Treasure.new
  treasure.id = 1
  decorated_treasure = treasure_decorator.new(treasure)

  expected = PriceEstimate.where(estimate_of_type: "Treasure", estimate_of_id: 1)
  actual   = PriceEstimate.where(estimate_of: decorated_treasure)

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_array_where_multiple_types() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 143
def test_polymorphic_array_where_multiple_types
  treasure_1 = treasures(:diamond)
  treasure_2 = treasures(:sapphire)
  car = cars(:honda)

  expected = [price_estimates(:diamond), price_estimates(:sapphire_1), price_estimates(:sapphire_2), price_estimates(:honda)].sort
  actual = PriceEstimate.where(estimate_of: [treasure_1, treasure_2, car]).to_a.sort

  assert_equal expected, actual
end
test_polymorphic_nested_array_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 121
def test_polymorphic_nested_array_where
  treasure = Treasure.new
  treasure.id = 1
  hidden = HiddenTreasure.new
  hidden.id = 2

  expected = PriceEstimate.where(estimate_of_type: "Treasure", estimate_of_id: [treasure, hidden])
  actual   = PriceEstimate.where(estimate_of: [treasure, hidden])

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_nested_array_where_not() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 133
def test_polymorphic_nested_array_where_not
  treasure = treasures(:diamond)
  car = cars(:honda)

  expected = [price_estimates(:sapphire_1), price_estimates(:sapphire_2)]
  actual   = PriceEstimate.where.not(estimate_of: [treasure, car])

  assert_equal expected.sort_by(&:id), actual.sort_by(&:id)
end
test_polymorphic_nested_relation_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 154
def test_polymorphic_nested_relation_where
  expected = PriceEstimate.where(estimate_of_type: "Treasure", estimate_of_id: Treasure.where(id: [1, 2]))
  actual   = PriceEstimate.where(estimate_of: Treasure.where(id: [1, 2]))

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_nested_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 171
def test_polymorphic_nested_where
  thing = Post.new
  thing.id = 1

  expected = Treasure.where(price_estimates: { thing_type: "Post", thing_id: 1 }).joins(:price_estimates)
  actual   = Treasure.where(price_estimates: { thing: thing }).joins(:price_estimates)

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_shallow_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 102
def test_polymorphic_shallow_where
  treasure = Treasure.new
  treasure.id = 1

  expected = PriceEstimate.where(estimate_of_type: "Treasure", estimate_of_id: 1)
  actual   = PriceEstimate.where(estimate_of: treasure)

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_shallow_where_not() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 112
def test_polymorphic_shallow_where_not
  treasure = treasures(:sapphire)

  expected = [price_estimates(:diamond), price_estimates(:honda)]
  actual   = PriceEstimate.where.not(estimate_of: treasure)

  assert_equal expected.sort_by(&:id), actual.sort_by(&:id)
end
test_polymorphic_sti_nested_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 181
def test_polymorphic_sti_nested_where
  treasure = HiddenTreasure.new
  treasure.id = 1

  expected = Treasure.where(price_estimates: { estimate_of_type: "Treasure", estimate_of_id: 1 }).joins(:price_estimates)
  actual   = Treasure.where(price_estimates: { estimate_of: treasure }).joins(:price_estimates)

  assert_equal expected.to_sql, actual.to_sql
end
test_polymorphic_sti_shallow_where() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 161
def test_polymorphic_sti_shallow_where
  treasure = HiddenTreasure.new
  treasure.id = 1

  expected = PriceEstimate.where(estimate_of_type: "Treasure", estimate_of_id: 1)
  actual   = PriceEstimate.where(estimate_of: treasure)

  assert_equal expected.to_sql, actual.to_sql
end
test_rewhere_on_root() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 57
def test_rewhere_on_root
  assert_equal posts(:welcome), Post.rewhere(title: "Welcome to the weblog").first
end
test_where_copies_arel_bind_params() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 43
def test_where_copies_arel_bind_params
  chef = Chef.create!
  CakeDesigner.create!(chef: chef)

  cake_designers = CakeDesigner.joins(:chef).where(chefs: { id: chef.id })
  chefs = Chef.where(employable: cake_designers)

  assert_equal [chef], chefs.to_a
end
test_where_copies_bind_params() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 22
def test_where_copies_bind_params
  author = authors(:david)
  posts  = author.posts.where("posts.id != 1")
  joined = Post.where(id: posts)

  assert_operator joined.length, :>, 0

  joined.each { |post|
    assert_equal author, post.author
    assert_not_equal 1, post.id
  }
end
test_where_copies_bind_params_in_the_right_order() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 35
def test_where_copies_bind_params_in_the_right_order
  author = authors(:david)
  posts = author.posts.where.not(id: 1)
  joined = Post.where(id: posts, title: posts.first.title)

  assert_equal joined, [posts.first]
end
test_where_error() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 223
def test_where_error
  assert_nothing_raised do
    Post.where(id: { "posts.author_id" => 10 }).first
  end
end
test_where_on_association_with_custom_primary_key() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 282
def test_where_on_association_with_custom_primary_key
  author = authors(:david)
  essay = Essay.where(writer: author).first

  assert_equal essays(:david_modest_proposal), essay
end
test_where_on_association_with_custom_primary_key_with_array_of_base() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 304
def test_where_on_association_with_custom_primary_key_with_array_of_base
  author = authors(:david)
  essay = Essay.where(writer: [author]).first

  assert_equal essays(:david_modest_proposal), essay
end
test_where_on_association_with_custom_primary_key_with_array_of_ids() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 311
def test_where_on_association_with_custom_primary_key_with_array_of_ids
  essay = Essay.where(writer: ["David"]).first

  assert_equal essays(:david_modest_proposal), essay
end
test_where_on_association_with_custom_primary_key_with_relation() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 289
def test_where_on_association_with_custom_primary_key_with_relation
  author = authors(:david)
  essay = Essay.where(writer: Author.where(id: author.id)).first

  assert_equal essays(:david_modest_proposal), essay
end
test_where_on_association_with_relation_performs_subselect_not_two_queries() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 296
def test_where_on_association_with_relation_performs_subselect_not_two_queries
  author = authors(:david)

  assert_queries(1) do
    Essay.where(writer: Author.where(id: author.id)).to_a
  end
end
test_where_on_association_with_select_relation() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 331
def test_where_on_association_with_select_relation
  essay = Essay.where(author: Author.where(name: "David").select(:name)).take
  assert_equal essays(:david_modest_proposal), essay
end
test_where_with_blank_conditions() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 246
def test_where_with_blank_conditions
  [[], {}, nil, ""].each do |blank|
    assert_equal 4, Edge.where(blank).order("sink_id").to_a.size
  end
end
test_where_with_boolean_for_string_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 262
def test_where_with_boolean_for_string_column
  count = Post.where(title: false).count
  assert_equal 0, count
end
test_where_with_casted_value_is_nil() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 53
def test_where_with_casted_value_is_nil
  assert_equal 4, Topic.where(last_read: "").count
end
test_where_with_decimal_for_string_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 267
def test_where_with_decimal_for_string_column
  count = Post.where(title: BigDecimal.new(0)).count
  assert_equal 0, count
end
test_where_with_duration_for_string_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 272
def test_where_with_duration_for_string_column
  count = Post.where(title: 0.seconds).count
  assert_equal 0, count
end
test_where_with_empty_hash_and_no_foreign_key() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 242
def test_where_with_empty_hash_and_no_foreign_key
  assert_equal 0, Edge.where(sink: {}).count
end
test_where_with_float_for_string_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 257
def test_where_with_float_for_string_column
  count = Post.where(title: 0.0).count
  assert_equal 0, count
end
test_where_with_integer_for_binary_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 277
def test_where_with_integer_for_binary_column
  count = Binary.where(data: 0).count
  assert_equal 0, count
end
test_where_with_integer_for_string_column() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 252
def test_where_with_integer_for_string_column
  count = Post.where(title: 0).count
  assert_equal 0, count
end
test_where_with_relation_on_has_many_association() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 317
def test_where_with_relation_on_has_many_association
  essay = essays(:david_modest_proposal)
  author = Author.where(essays: Essay.where(id: essay.id)).first

  assert_equal authors(:david), author
end
test_where_with_relation_on_has_one_association() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 324
def test_where_with_relation_on_has_one_association
  author = authors(:david)
  author_address = AuthorAddress.where(author: Author.where(id: author.id)).first
  assert_equal author_addresses(:david_address), author_address
end
test_where_with_strong_parameters() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 336
def test_where_with_strong_parameters
  protected_params = Class.new do
    attr_reader :permitted
    alias :permitted? :permitted

    def initialize(parameters)
      @parameters = parameters
      @permitted = false
    end

    def to_h
      @parameters
    end

    def permit!
      @permitted = true
      self
    end
  end

  author = authors(:david)
  params = protected_params.new(name: author.name)
  assert_raises(ActiveModel::ForbiddenAttributesError) { Author.where(params) }
  assert_equal author, Author.where(params.permit!).first
end
test_where_with_table_name() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 229
def test_where_with_table_name
  post = Post.first
  assert_equal post, Post.where(posts: { "id" => post.id }).first
end
test_where_with_table_name_and_empty_array() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 238
def test_where_with_table_name_and_empty_array
  assert_equal 0, Post.where(id: []).count
end
test_where_with_table_name_and_empty_hash() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 234
def test_where_with_table_name_and_empty_hash
  assert_equal 0, Post.where(posts: {}).count
end
test_where_with_unsupported_arguments() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 362
def test_where_with_unsupported_arguments
  assert_raises(ArgumentError) { Author.where(42) }
end
to_h() click to toggle source
# File activerecord/test/cases/relation/where_test.rb, line 346
def to_h
  @parameters
end