where_test.rb 2.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
D
Damien Mathieu 已提交
11
    fixtures :posts, :edges
12 13

    def test_belongs_to_shallow_where
J
Jon Leighton 已提交
14 15
      author = Author.new
      author.id = 1
16

J
Jon Leighton 已提交
17
      assert_equal Post.where(author_id: 1).to_sql, Post.where(author: author).to_sql
18 19 20
    end

    def test_belongs_to_nested_where
J
Jon Leighton 已提交
21 22
      parent = Comment.new
      parent.id = 1
23

J
Jon Leighton 已提交
24 25 26 27
      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
28 29 30
    end

    def test_polymorphic_shallow_where
J
Jon Leighton 已提交
31 32
      treasure = Treasure.new
      treasure.id = 1
33

J
Jon Leighton 已提交
34 35
      expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
      actual   = PriceEstimate.where(estimate_of: treasure)
36

J
Jon Leighton 已提交
37
      assert_equal expected.to_sql, actual.to_sql
38 39 40
    end

    def test_polymorphic_sti_shallow_where
J
Jon Leighton 已提交
41 42
      treasure = HiddenTreasure.new
      treasure.id = 1
43

J
Jon Leighton 已提交
44 45
      expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
      actual   = PriceEstimate.where(estimate_of: treasure)
46

J
Jon Leighton 已提交
47
      assert_equal expected.to_sql, actual.to_sql
48 49 50
    end

    def test_polymorphic_nested_where
J
Jon Leighton 已提交
51 52
      thing = Post.new
      thing.id = 1
53

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

J
Jon Leighton 已提交
57
      assert_equal expected.to_sql, actual.to_sql
58 59 60
    end

    def test_polymorphic_sti_nested_where
J
Jon Leighton 已提交
61 62
      treasure = HiddenTreasure.new
      treasure.id = 1
63

J
Jon Leighton 已提交
64 65
      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)
66

J
Jon Leighton 已提交
67
      assert_equal expected.to_sql, actual.to_sql
68
    end
69 70 71 72 73 74 75 76 77 78 79

    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 已提交
80 81 82 83 84 85 86 87

    def test_where_with_table_name_and_empty_hash
      assert_equal 0, Post.where(:posts => {}).count
    end

    def test_where_with_empty_hash_and_no_foreign_key
      assert_equal 0, Edge.where(:sink => {}).count
    end
88 89 90 91 92

    def test_where_with_blank_conditions
      [[], {}, nil, ""].each do |blank|
        assert_equal 4, Edge.where(blank).order("sink_id").to_a.size
      end
93 94 95 96 97 98
    def test_where_with_table_name_and_empty_array
      assert_equal 0, Post.where(:id => []).count
    end

    def test_where_with_empty_hash_and_no_foreign_key
      assert_equal 0, Edge.where(:sink => {}).count
99
    end
100 101
  end
end