Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / named_scope_test.rb
1 require "cases/helper"
2 require 'models/post'
3 require 'models/topic'
4 require 'models/comment'
5 require 'models/reply'
6 require 'models/author'
7 require 'models/developer'
8
9 class NamedScopeTest < ActiveRecord::TestCase
10 fixtures :posts, :authors, :topics, :comments, :author_addresses
11
12 def test_implements_enumerable
13 assert !Topic.find(:all).empty?
14
15 assert_equal Topic.find(:all), Topic.base
16 assert_equal Topic.find(:all), Topic.base.to_a
17 assert_equal Topic.find(:first), Topic.base.first
18 assert_equal Topic.find(:all), Topic.base.each { |i| i }
19 end
20
21 def test_found_items_are_cached
22 Topic.columns
23 all_posts = Topic.base
24
25 assert_queries(1) do
26 all_posts.collect
27 all_posts.collect
28 end
29 end
30
31 def test_reload_expires_cache_of_found_items
32 all_posts = Topic.base
33 all_posts.inspect
34
35 new_post = Topic.create!
36 assert !all_posts.include?(new_post)
37 assert all_posts.reload.include?(new_post)
38 end
39
40 def test_delegates_finds_and_calculations_to_the_base_class
41 assert !Topic.find(:all).empty?
42
43 assert_equal Topic.find(:all), Topic.base.find(:all)
44 assert_equal Topic.find(:first), Topic.base.find(:first)
45 assert_equal Topic.count, Topic.base.count
46 assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count)
47 end
48
49 def test_scope_should_respond_to_own_methods_and_methods_of_the_proxy
50 assert Topic.approved.respond_to?(:proxy_found)
51 assert Topic.approved.respond_to?(:count)
52 assert Topic.approved.respond_to?(:length)
53 end
54
55 def test_respond_to_respects_include_private_parameter
56 assert !Topic.approved.respond_to?(:load_found)
57 assert Topic.approved.respond_to?(:load_found, true)
58 end
59
60 def test_subclasses_inherit_scopes
61 assert Topic.scopes.include?(:base)
62
63 assert Reply.scopes.include?(:base)
64 assert_equal Reply.find(:all), Reply.base
65 end
66
67 def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified
68 assert !Topic.find(:all, :conditions => {:approved => true}).empty?
69
70 assert_equal Topic.find(:all, :conditions => {:approved => true}), Topic.approved
71 assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count
72 end
73
74 def test_scopes_with_string_name_can_be_composed
75 # NOTE that scopes defined with a string as a name worked on their own
76 # but when called on another scope the other scope was completely replaced
77 assert_equal Topic.replied.approved, Topic.replied.approved_as_string
78 end
79
80 def test_scopes_can_be_specified_with_deep_hash_conditions
81 assert_equal Topic.replied.approved, Topic.replied.approved_as_hash_condition
82 end
83
84 def test_scopes_are_composable
85 assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved
86 assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied
87 assert !(approved == replied)
88 assert !(approved & replied).empty?
89
90 assert_equal approved & replied, Topic.approved.replied
91 end
92
93 def test_procedural_scopes
94 topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on])
95 topics_written_before_the_second = Topic.find(:all, :conditions => ['written_on < ?', topics(:second).written_on])
96 assert_not_equal topics_written_before_the_second, topics_written_before_the_third
97
98 assert_equal topics_written_before_the_third, Topic.written_before(topics(:third).written_on)
99 assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
100 end
101
102 def test_scopes_with_joins
103 address = author_addresses(:david_address)
104 posts_with_authors_at_address = Post.find(
105 :all, :joins => 'JOIN authors ON authors.id = posts.author_id',
106 :conditions => [ 'authors.author_address_id = ?', address.id ]
107 )
108 assert_equal posts_with_authors_at_address, Post.with_authors_at_address(address)
109 end
110
111 def test_scopes_with_joins_respects_custom_select
112 address = author_addresses(:david_address)
113 posts_with_authors_at_address_titles = Post.find(:all,
114 :select => 'title',
115 :joins => 'JOIN authors ON authors.id = posts.author_id',
116 :conditions => [ 'authors.author_address_id = ?', address.id ]
117 )
118 assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select => 'title')
119 end
120
121 def test_extensions
122 assert_equal 1, Topic.anonymous_extension.one
123 assert_equal 2, Topic.named_extension.two
124 end
125
126 def test_multiple_extensions
127 assert_equal 2, Topic.multiple_extensions.extension_two
128 assert_equal 1, Topic.multiple_extensions.extension_one
129 end
130
131 def test_has_many_associations_have_access_to_named_scopes
132 assert_not_equal Post.containing_the_letter_a, authors(:david).posts
133 assert !Post.containing_the_letter_a.empty?
134
135 assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a
136 end
137
138 def test_has_many_through_associations_have_access_to_named_scopes
139 assert_not_equal Comment.containing_the_letter_e, authors(:david).comments
140 assert !Comment.containing_the_letter_e.empty?
141
142 assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e
143 end
144
145 def test_active_records_have_scope_named__all__
146 assert !Topic.find(:all).empty?
147
148 assert_equal Topic.find(:all), Topic.base
149 end
150
151 def test_active_records_have_scope_named__scoped__
152 assert !Topic.find(:all, scope = {:conditions => "content LIKE '%Have%'"}).empty?
153
154 assert_equal Topic.find(:all, scope), Topic.scoped(scope)
155 end
156
157 def test_proxy_options
158 expected_proxy_options = { :conditions => { :approved => true } }
159 assert_equal expected_proxy_options, Topic.approved.proxy_options
160 end
161
162 def test_first_and_last_should_support_find_options
163 assert_equal Topic.base.first(:order => 'title'), Topic.base.find(:first, :order => 'title')
164 assert_equal Topic.base.last(:order => 'title'), Topic.base.find(:last, :order => 'title')
165 end
166
167 def test_first_and_last_should_allow_integers_for_limit
168 assert_equal Topic.base.first(2), Topic.base.to_a.first(2)
169 assert_equal Topic.base.last(2), Topic.base.to_a.last(2)
170 end
171
172 def test_first_and_last_should_not_use_query_when_results_are_loaded
173 topics = Topic.base
174 topics.reload # force load
175 assert_no_queries do
176 topics.first
177 topics.last
178 end
179 end
180
181 def test_first_and_last_find_options_should_use_query_when_results_are_loaded
182 topics = Topic.base
183 topics.reload # force load
184 assert_queries(2) do
185 topics.first(:order => 'title')
186 topics.last(:order => 'title')
187 end
188 end
189
190 def test_empty_should_not_load_results
191 topics = Topic.base
192 assert_queries(2) do
193 topics.empty? # use count query
194 topics.collect # force load
195 topics.empty? # use loaded (no query)
196 end
197 end
198
199 def test_any_should_not_load_results
200 topics = Topic.base
201 assert_queries(2) do
202 topics.any? # use count query
203 topics.collect # force load
204 topics.any? # use loaded (no query)
205 end
206 end
207
208 def test_any_should_call_proxy_found_if_using_a_block
209 topics = Topic.base
210 assert_queries(1) do
211 topics.expects(:empty?).never
212 topics.any? { true }
213 end
214 end
215
216 def test_any_should_not_fire_query_if_named_scope_loaded
217 topics = Topic.base
218 topics.collect # force load
219 assert_no_queries { assert topics.any? }
220 end
221
222 def test_should_build_with_proxy_options
223 topic = Topic.approved.build({})
224 assert topic.approved
225 end
226
227 def test_should_build_new_with_proxy_options
228 topic = Topic.approved.new
229 assert topic.approved
230 end
231
232 def test_should_create_with_proxy_options
233 topic = Topic.approved.create({})
234 assert topic.approved
235 end
236
237 def test_should_create_with_bang_with_proxy_options
238 topic = Topic.approved.create!({})
239 assert topic.approved
240 end
241
242 def test_should_build_with_proxy_options_chained
243 topic = Topic.approved.by_lifo.build({})
244 assert topic.approved
245 assert_equal 'lifo', topic.author_name
246 end
247
248 def test_find_all_should_behave_like_select
249 assert_equal Topic.base.select(&:approved), Topic.base.find_all(&:approved)
250 end
251
252 def test_rand_should_select_a_random_object_from_proxy
253 assert Topic.approved.rand.is_a?(Topic)
254 end
255
256 def test_should_use_where_in_query_for_named_scope
257 assert_equal Developer.find_all_by_name('Jamis'), Developer.find_all_by_id(Developer.jamises)
258 end
259
260 def test_size_should_use_count_when_results_are_not_loaded
261 topics = Topic.base
262 assert_queries(1) do
263 assert_sql(/COUNT/i) { topics.size }
264 end
265 end
266
267 def test_size_should_use_length_when_results_are_loaded
268 topics = Topic.base
269 topics.reload # force load
270 assert_no_queries do
271 topics.size # use loaded (no query)
272 end
273 end
274
275 def test_chaining_with_duplicate_joins
276 join = "INNER JOIN comments ON comments.post_id = posts.id"
277 post = Post.find(1)
278 assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size
279 end
280 end