class ActiveRecord::OrTest

Public Instance Methods

test_or_identity() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 18
def test_or_identity
  expected = Post.where("id = 1").to_a
  assert_equal expected, Post.where("id = 1").or(Post.where("id = 1")).to_a
end
test_or_inside_named_scope() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 103
def test_or_inside_named_scope
  expected = Post.where("body LIKE '\%a\%' OR title LIKE ?", "%'%").order("id DESC").to_a
  assert_equal expected, Post.order(id: :desc).typographically_interesting
end
test_or_on_loaded_relation() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 108
def test_or_on_loaded_relation
  expected = Post.where("id = 1 or id = 2").to_a
  p = Post.where("id = 1")
  p.load
  assert_equal true, p.loaded?
  assert_equal expected, p.or(Post.where("id = 2")).to_a
end
test_or_preserves_other_querying_methods() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 52
def test_or_preserves_other_querying_methods
  expected = Post.where("id = 1 or id = 2 or id = 3").order("body asc").to_a
  partial = Post.order("body asc")
  assert_equal expected, partial.where("id = 1").or(partial.where(id: [2, 3])).to_a
  assert_equal expected, Post.order("body asc").where("id = 1").or(Post.order("body asc").where(id: [2, 3])).to_a
end
test_or_when_grouping() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 92
def test_or_when_grouping
  groups = Post.where("id < 10").group("body").select("body, COUNT(*) AS c")
  expected = groups.having("COUNT(*) > 1 OR body like 'Such%'").to_a.map { |o| [o.body, o.c] }
  assert_equal expected, groups.having("COUNT(*) > 1").or(groups.having("body like 'Such%'")).to_a.map { |o| [o.body, o.c] }
end
test_or_with_bind_params() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 33
def test_or_with_bind_params
  assert_equal Post.find([1, 2]), Post.where(id: 1).or(Post.where(id: 2)).to_a
end
test_or_with_incompatible_relations() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 59
def test_or_with_incompatible_relations
  error = assert_raises ArgumentError do
    Post.order("body asc").where("id = 1").or(Post.order("id desc").where(id: [2, 3])).to_a
  end

  assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message
end
test_or_with_incompatible_unscope() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 84
def test_or_with_incompatible_unscope
  error = assert_raises ArgumentError do
    Post.order("body asc").where("id = 1").or(Post.order("body asc").where("id = 2").unscope(:order)).to_a
  end

  assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message
end
test_or_with_named_scope() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 98
def test_or_with_named_scope
  expected = Post.where("id = 1 or body LIKE '\%a\%'").to_a
  assert_equal expected, Post.where("id = 1").or(Post.containing_the_letter_a)
end
test_or_with_non_relation_object_raises_error() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 116
def test_or_with_non_relation_object_raises_error
  assert_raises ArgumentError do
    Post.where(id: [1, 2, 3]).or(title: "Quails")
  end
end
test_or_with_null_both() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 37
def test_or_with_null_both
  expected = Post.none.to_a
  assert_equal expected, Post.none.or(Post.none).to_a
end
test_or_with_null_left() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 23
def test_or_with_null_left
  expected = Post.where("id = 1").to_a
  assert_equal expected, Post.none.or(Post.where("id = 1")).to_a
end
test_or_with_null_right() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 28
def test_or_with_null_right
  expected = Post.where("id = 1").to_a
  assert_equal expected, Post.where("id = 1").or(Post.none).to_a
end
test_or_with_references_inequality() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 122
def test_or_with_references_inequality
  joined = Post.includes(:author)
  actual = joined.where(authors: { id: 1 })
    .or(joined.where(title: "I don't have any comments"))
  expected = Author.find(1).posts + Post.where(title: "I don't have any comments")
  assert_equal expected.sort_by(&:id), actual.sort_by(&:id)
end
test_or_with_relation() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 13
def test_or_with_relation
  expected = Post.where("id = 1 or id = 2").to_a
  assert_equal expected, Post.where("id = 1").or(Post.where("id = 2")).to_a
end
test_or_with_unscope_order() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 79
def test_or_with_unscope_order
  expected = Post.where("id = 1 or id = 2")
  assert_equal expected, Post.order("body asc").where("id = 1").unscope(:order).or(Post.where("id = 2")).to_a
end
test_or_with_unscope_where() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 67
def test_or_with_unscope_where
  expected = Post.where("id = 1 or id = 2")
  partial = Post.where("id = 1 and id != 2")
  assert_equal expected, partial.or(partial.unscope(:where).where("id = 2")).to_a
end
test_or_with_unscope_where_column() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 73
def test_or_with_unscope_where_column
  expected = Post.where("id = 1 or id = 2")
  partial = Post.where(id: 1).where.not(id: 2)
  assert_equal expected, partial.or(partial.unscope(where: :id).where("id = 2")).to_a
end
test_or_without_left_where() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 42
def test_or_without_left_where
  expected = Post.all
  assert_equal expected, Post.or(Post.where("id = 1")).to_a
end
test_or_without_right_where() click to toggle source
# File activerecord/test/cases/relation/or_test.rb, line 47
def test_or_without_right_where
  expected = Post.all
  assert_equal expected, Post.where("id = 1").or(Post.all).to_a
end