relation_test.rb 7.1 KB
Newer Older
1
require "cases/helper"
2 3
require 'models/post'
require 'models/comment'
4 5
require 'models/author'
require 'models/rating'
6 7 8

module ActiveRecord
  class RelationTest < ActiveRecord::TestCase
9
    fixtures :posts, :comments, :authors
10

11
    class FakeKlass < Struct.new(:table_name, :name)
12 13 14 15
      extend ActiveRecord::Delegation::DelegateCache

      inherited self

16 17 18
      def self.connection
        Post.connection
      end
A
Aaron Patterson 已提交
19 20
    end

21
    def test_construction
22
      relation = Relation.new FakeKlass, :b
23
      assert_equal FakeKlass, relation.klass
24 25 26 27
      assert_equal :b, relation.table
      assert !relation.loaded, 'relation is not loaded'
    end

28
    def test_responds_to_model_and_returns_klass
29 30
      relation = Relation.new FakeKlass, :b
      assert_equal FakeKlass, relation.model
31 32
    end

33
    def test_initialize_single_values
34
      relation = Relation.new FakeKlass, :b
35
      (Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |method|
36 37
        assert_nil relation.send("#{method}_value"), method.to_s
      end
38
      assert_equal({}, relation.create_with_value)
39 40 41
    end

    def test_multi_value_initialize
42
      relation = Relation.new FakeKlass, :b
43 44 45 46 47 48
      Relation::MULTI_VALUE_METHODS.each do |method|
        assert_equal [], relation.send("#{method}_values"), method.to_s
      end
    end

    def test_extensions
49
      relation = Relation.new FakeKlass, :b
50 51
      assert_equal [], relation.extensions
    end
A
Aaron Patterson 已提交
52

53
    def test_empty_where_values_hash
54
      relation = Relation.new FakeKlass, :b
A
Aaron Patterson 已提交
55 56
      assert_equal({}, relation.where_values_hash)

57
      relation.where! :hello
A
Aaron Patterson 已提交
58 59 60
      assert_equal({}, relation.where_values_hash)
    end

61 62
    def test_has_values
      relation = Relation.new Post, Post.arel_table
63
      relation.where! relation.table[:id].eq(10)
64 65 66 67 68
      assert_equal({:id => 10}, relation.where_values_hash)
    end

    def test_values_wrong_table
      relation = Relation.new Post, Post.arel_table
69
      relation.where! Comment.arel_table[:id].eq(10)
70 71 72
      assert_equal({}, relation.where_values_hash)
    end

73 74 75 76 77
    def test_tree_is_not_traversed
      relation = Relation.new Post, Post.arel_table
      left     = relation.table[:id].eq(10)
      right    = relation.table[:id].eq(10)
      combine  = left.and right
78
      relation.where! combine
79 80 81
      assert_equal({}, relation.where_values_hash)
    end

A
Aaron Patterson 已提交
82
    def test_table_name_delegates_to_klass
83 84
      relation = Relation.new FakeKlass.new('posts'), :b
      assert_equal 'posts', relation.table_name
A
Aaron Patterson 已提交
85 86 87
    end

    def test_scope_for_create
88
      relation = Relation.new FakeKlass, :b
A
Aaron Patterson 已提交
89 90
      assert_equal({}, relation.scope_for_create)
    end
91 92 93 94 95 96 97 98 99 100

    def test_create_with_value
      relation = Relation.new Post, Post.arel_table
      hash = { :hello => 'world' }
      relation.create_with_value = hash
      assert_equal hash, relation.scope_for_create
    end

    def test_create_with_value_with_wheres
      relation = Relation.new Post, Post.arel_table
101
      relation.where! relation.table[:id].eq(10)
102 103 104
      relation.create_with_value = {:hello => 'world'}
      assert_equal({:hello => 'world', :id => 10}, relation.scope_for_create)
    end
105 106 107 108 109 110

    # FIXME: is this really wanted or expected behavior?
    def test_scope_for_create_is_cached
      relation = Relation.new Post, Post.arel_table
      assert_equal({}, relation.scope_for_create)

111
      relation.where! relation.table[:id].eq(10)
112 113 114 115 116
      assert_equal({}, relation.scope_for_create)

      relation.create_with_value = {:hello => 'world'}
      assert_equal({}, relation.scope_for_create)
    end
A
Aaron Patterson 已提交
117

118 119 120 121 122 123
    def test_bad_constants_raise_errors
      assert_raises(NameError) do
        ActiveRecord::Relation::HelloWorld
      end
    end

A
Aaron Patterson 已提交
124
    def test_empty_eager_loading?
125
      relation = Relation.new FakeKlass, :b
A
Aaron Patterson 已提交
126 127 128 129
      assert !relation.eager_loading?
    end

    def test_eager_load_values
130
      relation = Relation.new FakeKlass, :b
131
      relation.eager_load! :b
A
Aaron Patterson 已提交
132 133
      assert relation.eager_loading?
    end
134 135

    def test_references_values
136
      relation = Relation.new FakeKlass, :b
137 138
      assert_equal [], relation.references_values
      relation = relation.references(:foo).references(:omg, :lol)
J
Jon Leighton 已提交
139
      assert_equal ['foo', 'omg', 'lol'], relation.references_values
140 141 142
    end

    def test_references_values_dont_duplicate
143
      relation = Relation.new FakeKlass, :b
144
      relation = relation.references(:foo).references(:foo)
J
Jon Leighton 已提交
145
      assert_equal ['foo'], relation.references_values
146
    end
147

148
    test 'merging a hash into a relation' do
149
      relation = Relation.new FakeKlass, :b
150
      relation = relation.merge where: :lol, readonly: true
151

152
      assert_equal [:lol], relation.where_values
153 154 155 156
      assert_equal true, relation.readonly_value
    end

    test 'merging an empty hash into a relation' do
157
      assert_equal [], Relation.new(FakeKlass, :b).merge({}).where_values
158
    end
J
Jon Leighton 已提交
159 160 161 162

    test 'merging a hash with unknown keys raises' do
      assert_raises(ArgumentError) { Relation::HashMerger.new(nil, omg: 'lol') }
    end
163 164

    test '#values returns a dup of the values' do
165
      relation = Relation.new(FakeKlass, :b).where! :foo
166 167 168 169 170 171 172
      values   = relation.values

      values[:where] = nil
      assert_not_nil relation.where_values
    end

    test 'relations can be created with a values hash' do
173
      relation = Relation.new(FakeKlass, :b, where: [:foo])
174
      assert_equal [:foo], relation.where_values
175 176 177
    end

    test 'merging a single where value' do
178
      relation = Relation.new(FakeKlass, :b)
179 180
      relation.merge!(where: :foo)
      assert_equal [:foo], relation.where_values
181
    end
182 183

    test 'merging a hash interpolates conditions' do
184 185 186 187 188 189
      klass = Class.new(FakeKlass) do
        def self.sanitize_sql(args)
          raise unless args == ['foo = ?', 'bar']
          'foo = bar'
        end
      end
190 191 192 193 194

      relation = Relation.new(klass, :b)
      relation.merge!(where: ['foo = ?', 'bar'])
      assert_equal ['foo = bar'], relation.where_values
    end
195

196
    def test_relation_merging_with_merged_joins_as_symbols
197 198
      special_comments_with_ratings = SpecialComment.joins(:ratings)
      posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)
199
      assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
200 201
    end

202
    def test_respond_to_for_non_selected_element
203 204 205
      post = Post.select(:title).first
      assert_equal false, post.respond_to?(:body), "post should not respond_to?(:body) since invoking it raises exception"

206
      silence_warnings { post = Post.select("'title' as post_title").first }
207
      assert_equal false, post.respond_to?(:title), "post should not respond_to?(:body) since invoking it raises exception"
208 209
    end

210 211 212 213 214 215 216
    def test_relation_merging_with_merged_joins_as_strings
      join_string = "LEFT OUTER JOIN #{Rating.quoted_table_name} ON #{SpecialComment.quoted_table_name}.id = #{Rating.quoted_table_name}.comment_id"
      special_comments_with_ratings = SpecialComment.joins join_string
      posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)
      assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
    end

217 218
  end
end