aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-28 14:04:26 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-28 14:35:03 -0700
commitff45b9e9f7c4ff0fb4fdab8beb539913b876d63b (patch)
tree1513baae2abecb8e0ce14fe21ae8563c876d25ab /activerecord/test
parentb0b37942d729b6bdcd2e3178eda7fa1de203b3d0 (diff)
downloadrails-ff45b9e9f7c4ff0fb4fdab8beb539913b876d63b.tar.gz
rails-ff45b9e9f7c4ff0fb4fdab8beb539913b876d63b.tar.bz2
rails-ff45b9e9f7c4ff0fb4fdab8beb539913b876d63b.zip
Bring the implementation of Relation#or up to speed
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/relation/or_test.rb9
-rw-r--r--activerecord/test/cases/relation/where_clause_test.rb20
2 files changed, 26 insertions, 3 deletions
diff --git a/activerecord/test/cases/relation/or_test.rb b/activerecord/test/cases/relation/or_test.rb
index f2115d8aa6..1515b9c454 100644
--- a/activerecord/test/cases/relation/or_test.rb
+++ b/activerecord/test/cases/relation/or_test.rb
@@ -25,18 +25,22 @@ module ActiveRecord
assert_equal expected, Post.where('id = 1').or(Post.none).to_a
end
+ def test_or_with_bind_params
+ assert_equal Post.find([1, 2]), Post.where(id: 1).or(Post.where(id: 2)).to_a
+ end
+
def test_or_with_null_both
expected = Post.none.to_a
assert_equal expected, Post.none.or(Post.none).to_a
end
def test_or_without_left_where
- expected = Post.all.to_a
+ expected = Post.where('id = 1')
assert_equal expected, Post.or(Post.where('id = 1')).to_a
end
def test_or_without_right_where
- expected = Post.all.to_a
+ expected = Post.where('id = 1')
assert_equal expected, Post.where('id = 1').or(Post.all).to_a
end
@@ -76,6 +80,5 @@ module ActiveRecord
assert_equal p.loaded?, true
assert_equal expected, p.or(Post.where('id = 2')).to_a
end
-
end
end
diff --git a/activerecord/test/cases/relation/where_clause_test.rb b/activerecord/test/cases/relation/where_clause_test.rb
index db18980e0b..7325aec0a9 100644
--- a/activerecord/test/cases/relation/where_clause_test.rb
+++ b/activerecord/test/cases/relation/where_clause_test.rb
@@ -145,6 +145,26 @@ class ActiveRecord::Relation
assert_equal where_clause.ast, where_clause_with_empty.ast
end
+ test "or joins the two clauses using OR" do
+ where_clause = WhereClause.new([table["id"].eq(bind_param)], [attribute("id", 1)])
+ other_clause = WhereClause.new([table["name"].eq(bind_param)], [attribute("name", "Sean")])
+ expected_ast =
+ Arel::Nodes::Grouping.new(
+ Arel::Nodes::Or.new(table["id"].eq(bind_param), table["name"].eq(bind_param))
+ )
+ expected_binds = where_clause.binds + other_clause.binds
+
+ assert_equal expected_ast.to_sql, where_clause.or(other_clause).ast.to_sql
+ assert_equal expected_binds, where_clause.or(other_clause).binds
+ end
+
+ test "or does nothing with an empty where clause" do
+ where_clause = WhereClause.new([table["id"].eq(bind_param)], [attribute("id", 1)])
+
+ assert_equal where_clause, where_clause.or(WhereClause.empty)
+ assert_equal where_clause, WhereClause.empty.or(where_clause)
+ end
+
private
def table