aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relation/where_test.rb
blob: 76339fe41b64b59d6809f5f1d87cc8bc0bc67210 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require "cases/helper"
require 'models/author'
require 'models/price_estimate'
require 'models/treasure'
require 'models/post'

module ActiveRecord
  class WhereTest < ActiveRecord::TestCase
    fixtures :posts, :authors

    def test_belongs_to_shallow_where
      author = Post.first.author
      query_with_id = Post.where(:author_id => author)
      query_with_assoc = Post.where(:author => author)

      assert_equal query_with_id.to_sql, query_with_assoc.to_sql
    end

    def test_belongs_to_nested_where
      author = Post.first.author
      query_with_id = Author.where(:posts => {:author_id => author}).joins(:posts)
      query_with_assoc = Author.where(:posts => {:author => author}).joins(:posts)

      assert_equal query_with_id.to_sql, query_with_assoc.to_sql
    end

    def test_polymorphic_shallow_where
      treasure = Treasure.create(:name => 'gold coins')
      treasure.price_estimates << PriceEstimate.create(:price => 125)

      query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
      query_by_model = PriceEstimate.where(:estimate_of => treasure)

      assert_equal query_by_column.to_sql, query_by_model.to_sql
    end

    def test_polymorphic_sti_shallow_where
      treasure = HiddenTreasure.create!(:name => 'gold coins')
      treasure.price_estimates << PriceEstimate.create!(:price => 125)

      query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
      query_by_model = PriceEstimate.where(:estimate_of => treasure)

      assert_equal query_by_column.to_sql, query_by_model.to_sql
    end

    def test_polymorphic_nested_where
      estimate = PriceEstimate.create :price => 125
      treasure = Treasure.create :name => 'Booty'

      treasure.price_estimates << estimate

      query_by_column = Treasure.where(:price_estimates => {:estimate_of_type => 'Treasure', :estimate_of_id => treasure}).joins(:price_estimates)
      query_by_model = Treasure.where(:price_estimates => {:estimate_of => treasure}).joins(:price_estimates)

      assert_equal treasure, query_by_column.first
      assert_equal treasure, query_by_model.first
      assert_equal query_by_column.to_a, query_by_model.to_a
    end

    def test_polymorphic_sti_nested_where
      estimate = PriceEstimate.create :price => 125
      treasure = HiddenTreasure.create!(:name => 'gold coins')
      treasure.price_estimates << PriceEstimate.create!(:price => 125)

      treasure.price_estimates << estimate

      query_by_column = Treasure.where(:price_estimates => {:estimate_of_type => 'Treasure', :estimate_of_id => treasure}).joins(:price_estimates)
      query_by_model = Treasure.where(:price_estimates => {:estimate_of => treasure}).joins(:price_estimates)

      assert_equal treasure, query_by_column.first
      assert_equal treasure, query_by_model.first
      assert_equal query_by_column.to_a, query_by_model.to_a
    end

    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
  end
end