|
| 1 | +require "cases/helper" |
| 2 | +require 'models/post' |
| 3 | + |
| 4 | +module ActiveRecord |
| 5 | + class OrTest < ActiveRecord::TestCase |
| 6 | + fixtures :posts |
| 7 | + |
| 8 | + def test_or_with_relation |
| 9 | + expected = Post.where('id = 1 or id = 2').to_a |
| 10 | + assert_equal expected, Post.where('id = 1').or(Post.where('id = 2')).to_a |
| 11 | + end |
| 12 | + |
| 13 | + def test_or_identity |
| 14 | + expected = Post.where('id = 1').to_a |
| 15 | + assert_equal expected, Post.where('id = 1').or(Post.where('id = 1')).to_a |
| 16 | + end |
| 17 | + |
| 18 | + def test_or_with_null_left |
| 19 | + expected = Post.where('id = 1').to_a |
| 20 | + assert_equal expected, Post.none.or(Post.where('id = 1')).to_a |
| 21 | + end |
| 22 | + |
| 23 | + def test_or_with_null_right |
| 24 | + expected = Post.where('id = 1').to_a |
| 25 | + assert_equal expected, Post.where('id = 1').or(Post.none).to_a |
| 26 | + end |
| 27 | + |
| 28 | + def test_or_with_bind_params |
| 29 | + assert_equal Post.find([1, 2]), Post.where(id: 1).or(Post.where(id: 2)).to_a |
| 30 | + end |
| 31 | + |
| 32 | + def test_or_with_null_both |
| 33 | + expected = Post.none.to_a |
| 34 | + assert_equal expected, Post.none.or(Post.none).to_a |
| 35 | + end |
| 36 | + |
| 37 | + def test_or_without_left_where |
| 38 | + expected = Post.where('id = 1') |
| 39 | + assert_equal expected, Post.or(Post.where('id = 1')).to_a |
| 40 | + end |
| 41 | + |
| 42 | + def test_or_without_right_where |
| 43 | + expected = Post.where('id = 1') |
| 44 | + assert_equal expected, Post.where('id = 1').or(Post.all).to_a |
| 45 | + end |
| 46 | + |
| 47 | + def test_or_preserves_other_querying_methods |
| 48 | + expected = Post.where('id = 1 or id = 2 or id = 3').order('body asc').to_a |
| 49 | + partial = Post.order('body asc') |
| 50 | + assert_equal expected, partial.where('id = 1').or(partial.where(:id => [2, 3])).to_a |
| 51 | + assert_equal expected, Post.order('body asc').where('id = 1').or(Post.order('body asc').where(:id => [2, 3])).to_a |
| 52 | + end |
| 53 | + |
| 54 | + def test_or_with_incompatible_relations |
| 55 | + assert_raises ArgumentError do |
| 56 | + Post.order('body asc').where('id = 1').or(Post.order('id desc').where(:id => [2, 3])).to_a |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def test_or_when_grouping |
| 61 | + groups = Post.where('id < 10').group('body').select('body, COUNT(*) AS c') |
| 62 | + expected = groups.having("COUNT(*) > 1 OR body like 'Such%'").to_a.map {|o| [o.body, o.c] } |
| 63 | + assert_equal expected, groups.having('COUNT(*) > 1').or(groups.having("body like 'Such%'")).to_a.map {|o| [o.body, o.c] } |
| 64 | + end |
| 65 | + |
| 66 | + def test_or_with_named_scope |
| 67 | + expected = Post.where("id = 1 or body LIKE '\%a\%'").to_a |
| 68 | + assert_equal expected, Post.where('id = 1').or(Post.containing_the_letter_a) |
| 69 | + end |
| 70 | + |
| 71 | + def test_or_inside_named_scope |
| 72 | + expected = Post.where("body LIKE '\%a\%' OR title LIKE ?", "%'%").order('id DESC').to_a |
| 73 | + assert_equal expected, Post.order(id: :desc).typographically_interesting |
| 74 | + end |
| 75 | + |
| 76 | + def test_or_on_loaded_relation |
| 77 | + expected = Post.where('id = 1 or id = 2').to_a |
| 78 | + p = Post.where('id = 1') |
| 79 | + p.load |
| 80 | + assert_equal p.loaded?, true |
| 81 | + assert_equal expected, p.or(Post.where('id = 2')).to_a |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments