where_test.rb 3.9 KB
Newer Older
1
require "cases/helper"
2 3 4
require 'models/author'
require 'models/price_estimate'
require 'models/treasure'
5
require 'models/post'
J
Jon Leighton 已提交
6
require 'models/comment'
D
Damien Mathieu 已提交
7
require 'models/edge'
8 9 10

module ActiveRecord
  class WhereTest < ActiveRecord::TestCase
11 12 13 14 15 16 17 18 19 20 21 22 23 24
    fixtures :posts, :edges, :authors

    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
25 26

    def test_belongs_to_shallow_where
J
Jon Leighton 已提交
27 28
      author = Author.new
      author.id = 1
29

J
Jon Leighton 已提交
30
      assert_equal Post.where(author_id: 1).to_sql, Post.where(author: author).to_sql
31 32 33
    end

    def test_belongs_to_nested_where
J
Jon Leighton 已提交
34 35
      parent = Comment.new
      parent.id = 1
36

J
Jon Leighton 已提交
37 38 39 40
      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
41 42 43
    end

    def test_polymorphic_shallow_where
J
Jon Leighton 已提交
44 45
      treasure = Treasure.new
      treasure.id = 1
46

J
Jon Leighton 已提交
47 48
      expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
      actual   = PriceEstimate.where(estimate_of: treasure)
49

J
Jon Leighton 已提交
50
      assert_equal expected.to_sql, actual.to_sql
51 52 53
    end

    def test_polymorphic_sti_shallow_where
J
Jon Leighton 已提交
54 55
      treasure = HiddenTreasure.new
      treasure.id = 1
56

J
Jon Leighton 已提交
57 58
      expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
      actual   = PriceEstimate.where(estimate_of: treasure)
59

J
Jon Leighton 已提交
60
      assert_equal expected.to_sql, actual.to_sql
61 62 63
    end

    def test_polymorphic_nested_where
J
Jon Leighton 已提交
64 65
      thing = Post.new
      thing.id = 1
66

J
Jon Leighton 已提交
67 68
      expected = Treasure.where(price_estimates: { thing_type: 'Post', thing_id: 1 }).joins(:price_estimates)
      actual   = Treasure.where(price_estimates: { thing: thing }).joins(:price_estimates)
69

J
Jon Leighton 已提交
70
      assert_equal expected.to_sql, actual.to_sql
71 72 73
    end

    def test_polymorphic_sti_nested_where
J
Jon Leighton 已提交
74 75
      treasure = HiddenTreasure.new
      treasure.id = 1
76

J
Jon Leighton 已提交
77 78
      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)
79

J
Jon Leighton 已提交
80
      assert_equal expected.to_sql, actual.to_sql
81
    end
82 83 84 85 86 87 88 89 90 91 92

    def test_where_error
      assert_raises(ActiveRecord::StatementInvalid) do
        Post.where(:id => { 'posts.author_id' => 10 }).first
      end
    end

    def test_where_with_table_name
      post = Post.first
      assert_equal post, Post.where(:posts => { 'id' => post.id }).first
    end
D
Damien Mathieu 已提交
93 94

    def test_where_with_table_name_and_empty_hash
95 96 97
      assert_raises(ArgumentError) do
        Post.where(:posts => {})
      end
D
Damien Mathieu 已提交
98 99
    end

100
    def test_where_with_table_name_and_empty_array
101 102 103
      assert_raises(ArgumentError) do
        Post.where(:id => [])
      end
104 105
    end

D
Damien Mathieu 已提交
106
    def test_where_with_empty_hash_and_no_foreign_key
107 108 109
      assert_raises(ArgumentError) do
        Edge.where(:sink => {}).count
      end
D
Damien Mathieu 已提交
110
    end
111 112 113 114 115 116

    def test_where_with_blank_conditions
      [[], {}, nil, ""].each do |blank|
        assert_equal 4, Edge.where(blank).order("sink_id").to_a.size
      end
    end
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    def test_where_with_integer_for_string_column
      count = Post.where(:title => 0).count
      assert_equal 0, count
    end

    def test_where_with_float_for_string_column
      count = Post.where(:title => 0.0).count
      assert_equal 0, count
    end

    def test_where_with_boolean_for_string_column
      count = Post.where(:title => false).count
      assert_equal 0, count
    end

    def test_where_with_decimal_for_string_column
      count = Post.where(:title => BigDecimal.new(0)).count
      assert_equal 0, count
    end

    def test_where_with_duration_for_string_column
      count = Post.where(:title => 0.seconds).count
      assert_equal 0, count
    end
142 143
  end
end