2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-03-23 22:50:02 -04:00
|
|
|
require 'models/post'
|
|
|
|
require 'models/topic'
|
|
|
|
require 'models/comment'
|
2012-02-12 05:33:08 -05:00
|
|
|
require 'models/reply'
|
2008-03-23 22:50:02 -04:00
|
|
|
require 'models/author'
|
2008-08-21 11:08:42 -04:00
|
|
|
require 'models/developer'
|
2008-03-23 22:50:02 -04:00
|
|
|
|
|
|
|
class NamedScopeTest < ActiveRecord::TestCase
|
2008-06-21 07:41:30 -04:00
|
|
|
fixtures :posts, :authors, :topics, :comments, :author_addresses
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_implements_enumerable
|
2012-08-03 06:51:52 -04:00
|
|
|
assert !Topic.all.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal Topic.all.to_a, Topic.base
|
|
|
|
assert_equal Topic.all.to_a, Topic.base.to_a
|
|
|
|
assert_equal Topic.first, Topic.base.first
|
|
|
|
assert_equal Topic.all.to_a, Topic.base.map { |i| i }
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_found_items_are_cached
|
2008-04-30 01:25:52 -04:00
|
|
|
all_posts = Topic.base
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_queries(1) do
|
|
|
|
all_posts.collect
|
|
|
|
all_posts.collect
|
|
|
|
end
|
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_reload_expires_cache_of_found_items
|
2008-04-30 01:25:52 -04:00
|
|
|
all_posts = Topic.base
|
2012-07-27 07:01:25 -04:00
|
|
|
all_posts.to_a
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
new_post = Topic.create!
|
|
|
|
assert !all_posts.include?(new_post)
|
|
|
|
assert all_posts.reload.include?(new_post)
|
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_delegates_finds_and_calculations_to_the_base_class
|
2012-08-03 06:51:52 -04:00
|
|
|
assert !Topic.all.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal Topic.all.to_a, Topic.base.to_a
|
|
|
|
assert_equal Topic.first, Topic.base.first
|
|
|
|
assert_equal Topic.count, Topic.base.count
|
2008-04-30 01:25:52 -04:00
|
|
|
assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count)
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-06-06 23:55:39 -04:00
|
|
|
def test_method_missing_priority_when_delegating
|
|
|
|
klazz = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "topics"
|
|
|
|
scope :since, Proc.new { where('written_on >= ?', Time.now - 1.day) }
|
|
|
|
scope :to, Proc.new { where('written_on <= ?', Time.now) }
|
|
|
|
end
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_equal klazz.to.since.to_a, klazz.since.to.to_a
|
2012-06-06 23:55:39 -04:00
|
|
|
end
|
|
|
|
|
2008-08-13 07:36:39 -04:00
|
|
|
def test_scope_should_respond_to_own_methods_and_methods_of_the_proxy
|
2010-01-17 17:54:24 -05:00
|
|
|
assert Topic.approved.respond_to?(:limit)
|
2008-08-13 07:36:39 -04:00
|
|
|
assert Topic.approved.respond_to?(:count)
|
|
|
|
assert Topic.approved.respond_to?(:length)
|
|
|
|
end
|
|
|
|
|
2008-08-21 11:38:27 -04:00
|
|
|
def test_respond_to_respects_include_private_parameter
|
2010-06-29 11:18:55 -04:00
|
|
|
assert !Topic.approved.respond_to?(:tables_in_string)
|
|
|
|
assert Topic.approved.respond_to?(:tables_in_string, true)
|
2008-08-21 11:38:27 -04:00
|
|
|
end
|
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified
|
2012-07-27 12:27:47 -04:00
|
|
|
assert !Topic.all.merge!(:where => {:approved => true}).to_a.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-07-27 12:27:47 -04:00
|
|
|
assert_equal Topic.all.merge!(:where => {:approved => true}).to_a, Topic.approved
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Topic.where(:approved => true).count, Topic.approved.count
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-06-13 16:39:10 -04:00
|
|
|
def test_scopes_with_string_name_can_be_composed
|
|
|
|
# NOTE that scopes defined with a string as a name worked on their own
|
|
|
|
# but when called on another scope the other scope was completely replaced
|
|
|
|
assert_equal Topic.replied.approved, Topic.replied.approved_as_string
|
|
|
|
end
|
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_scopes_are_composable
|
2012-07-27 12:27:47 -04:00
|
|
|
assert_equal((approved = Topic.all.merge!(:where => {:approved => true}).to_a), Topic.approved)
|
|
|
|
assert_equal((replied = Topic.all.merge!(:where => 'replies_count > 0').to_a), Topic.replied)
|
2008-03-30 02:17:37 -04:00
|
|
|
assert !(approved == replied)
|
2008-03-23 22:50:02 -04:00
|
|
|
assert !(approved & replied).empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_equal approved & replied, Topic.approved.replied
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_procedural_scopes
|
2012-04-27 06:42:22 -04:00
|
|
|
topics_written_before_the_third = Topic.where('written_on < ?', topics(:third).written_on)
|
|
|
|
topics_written_before_the_second = Topic.where('written_on < ?', topics(:second).written_on)
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_not_equal topics_written_before_the_second, topics_written_before_the_third
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_equal topics_written_before_the_third, Topic.written_before(topics(:third).written_on)
|
|
|
|
assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
|
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2009-03-12 11:06:19 -04:00
|
|
|
def test_procedural_scopes_returning_nil
|
2012-08-03 06:51:52 -04:00
|
|
|
all_topics = Topic.all
|
2009-03-12 11:06:19 -04:00
|
|
|
|
|
|
|
assert_equal all_topics, Topic.written_before(nil)
|
|
|
|
end
|
2008-06-21 07:41:30 -04:00
|
|
|
|
2010-10-19 20:27:50 -04:00
|
|
|
def test_scope_with_object
|
|
|
|
objects = Topic.with_object
|
|
|
|
assert_operator objects.length, :>, 0
|
|
|
|
assert objects.all?(&:approved?), 'all objects should be approved'
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_has_many_associations_have_access_to_scopes
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_not_equal Post.containing_the_letter_a, authors(:david).posts
|
|
|
|
assert !Post.containing_the_letter_a.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a
|
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scope_with_STI
|
2010-05-03 12:18:25 -04:00
|
|
|
assert_equal 3,Post.containing_the_letter_a.count
|
|
|
|
assert_equal 1,SpecialPost.containing_the_letter_a.count
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_has_many_through_associations_have_access_to_scopes
|
2008-05-13 21:08:31 -04:00
|
|
|
assert_not_equal Comment.containing_the_letter_e, authors(:david).comments
|
2008-03-23 22:50:02 -04:00
|
|
|
assert !Comment.containing_the_letter_e.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e
|
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_honor_current_scopes_from_when_defined
|
2010-01-17 11:04:26 -05:00
|
|
|
assert !Post.ranked_by_comments.limit_by(5).empty?
|
|
|
|
assert !authors(:david).posts.ranked_by_comments.limit_by(5).empty?
|
|
|
|
assert_not_equal Post.ranked_by_comments.limit_by(5), authors(:david).posts.ranked_by_comments.limit_by(5)
|
2009-02-13 01:07:39 -05:00
|
|
|
assert_not_equal Post.top(5), authors(:david).posts.top(5)
|
2009-04-12 13:11:40 -04:00
|
|
|
# Oracle sometimes sorts differently if WHERE condition is changed
|
2010-01-17 17:54:24 -05:00
|
|
|
assert_equal authors(:david).posts.ranked_by_comments.limit_by(5).to_a.sort_by(&:id), authors(:david).posts.top(5).to_a.sort_by(&:id)
|
2010-01-17 11:04:26 -05:00
|
|
|
assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5)
|
2009-02-13 01:07:39 -05:00
|
|
|
end
|
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_active_records_have_scope_named__all__
|
2012-08-03 06:51:52 -04:00
|
|
|
assert !Topic.all.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal Topic.all.to_a, Topic.base
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2008-03-23 22:50:02 -04:00
|
|
|
def test_active_records_have_scope_named__scoped__
|
2012-04-26 07:44:20 -04:00
|
|
|
scope = Topic.where("content LIKE '%Have%'")
|
|
|
|
assert !scope.empty?
|
2008-03-30 02:17:37 -04:00
|
|
|
|
2012-07-27 12:27:47 -04:00
|
|
|
assert_equal scope, Topic.all.merge!(where: "content LIKE '%Have%'")
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|
2008-05-11 15:33:25 -04:00
|
|
|
|
2008-05-20 07:11:25 -04:00
|
|
|
def test_first_and_last_should_allow_integers_for_limit
|
2011-09-06 03:36:04 -04:00
|
|
|
assert_equal Topic.base.first(2), Topic.base.to_a.first(2)
|
2011-09-05 13:37:53 -04:00
|
|
|
assert_equal Topic.base.last(2), Topic.base.order("id").to_a.last(2)
|
2008-05-20 07:11:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_first_and_last_should_not_use_query_when_results_are_loaded
|
|
|
|
topics = Topic.base
|
|
|
|
topics.reload # force load
|
|
|
|
assert_no_queries do
|
|
|
|
topics.first
|
|
|
|
topics.last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-27 10:31:53 -04:00
|
|
|
def test_empty_should_not_load_results
|
|
|
|
topics = Topic.base
|
|
|
|
assert_queries(2) do
|
|
|
|
topics.empty? # use count query
|
|
|
|
topics.collect # force load
|
|
|
|
topics.empty? # use loaded (no query)
|
|
|
|
end
|
|
|
|
end
|
2008-07-09 09:08:04 -04:00
|
|
|
|
2008-07-23 06:50:16 -04:00
|
|
|
def test_any_should_not_load_results
|
|
|
|
topics = Topic.base
|
2008-08-07 22:33:44 -04:00
|
|
|
assert_queries(2) do
|
|
|
|
topics.any? # use count query
|
|
|
|
topics.collect # force load
|
|
|
|
topics.any? # use loaded (no query)
|
2008-07-23 06:50:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_any_should_call_proxy_found_if_using_a_block
|
|
|
|
topics = Topic.base
|
|
|
|
assert_queries(1) do
|
|
|
|
topics.expects(:empty?).never
|
|
|
|
topics.any? { true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_any_should_not_fire_query_if_scope_loaded
|
2008-07-23 06:50:16 -04:00
|
|
|
topics = Topic.base
|
|
|
|
topics.collect # force load
|
|
|
|
assert_no_queries { assert topics.any? }
|
|
|
|
end
|
|
|
|
|
2011-03-29 10:08:40 -04:00
|
|
|
def test_model_class_should_respond_to_any
|
|
|
|
assert Topic.any?
|
|
|
|
Topic.delete_all
|
|
|
|
assert !Topic.any?
|
|
|
|
end
|
|
|
|
|
2009-03-16 08:56:32 -04:00
|
|
|
def test_many_should_not_load_results
|
|
|
|
topics = Topic.base
|
|
|
|
assert_queries(2) do
|
|
|
|
topics.many? # use count query
|
|
|
|
topics.collect # force load
|
|
|
|
topics.many? # use loaded (no query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_should_call_proxy_found_if_using_a_block
|
|
|
|
topics = Topic.base
|
|
|
|
assert_queries(1) do
|
|
|
|
topics.expects(:size).never
|
|
|
|
topics.many? { true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_many_should_not_fire_query_if_scope_loaded
|
2009-03-16 08:56:32 -04:00
|
|
|
topics = Topic.base
|
|
|
|
topics.collect # force load
|
|
|
|
assert_no_queries { assert topics.many? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_should_return_false_if_none_or_one
|
2012-04-26 07:44:20 -04:00
|
|
|
topics = Topic.base.where(:id => 0)
|
2009-03-16 08:56:32 -04:00
|
|
|
assert !topics.many?
|
2012-04-26 07:44:20 -04:00
|
|
|
topics = Topic.base.where(:id => 1)
|
2009-03-16 08:56:32 -04:00
|
|
|
assert !topics.many?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_should_return_true_if_more_than_one
|
|
|
|
assert Topic.base.many?
|
|
|
|
end
|
|
|
|
|
2011-03-29 10:08:40 -04:00
|
|
|
def test_model_class_should_respond_to_many
|
|
|
|
Topic.delete_all
|
|
|
|
assert !Topic.many?
|
|
|
|
Topic.create!
|
|
|
|
assert !Topic.many?
|
|
|
|
Topic.create!
|
|
|
|
assert Topic.many?
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_build_on_top_of_scope
|
2008-07-09 09:08:04 -04:00
|
|
|
topic = Topic.approved.build({})
|
|
|
|
assert topic.approved
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_build_new_on_top_of_scope
|
2008-07-09 09:08:04 -04:00
|
|
|
topic = Topic.approved.new
|
|
|
|
assert topic.approved
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_create_on_top_of_scope
|
2008-07-09 09:08:04 -04:00
|
|
|
topic = Topic.approved.create({})
|
|
|
|
assert topic.approved
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_create_with_bang_on_top_of_scope
|
2008-07-09 09:08:04 -04:00
|
|
|
topic = Topic.approved.create!({})
|
|
|
|
assert topic.approved
|
|
|
|
end
|
2009-03-10 07:49:58 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_build_on_top_of_chained_scopes
|
2008-07-09 09:08:04 -04:00
|
|
|
topic = Topic.approved.by_lifo.build({})
|
|
|
|
assert topic.approved
|
|
|
|
assert_equal 'lifo', topic.author_name
|
|
|
|
end
|
2008-08-21 07:37:19 -04:00
|
|
|
|
|
|
|
def test_find_all_should_behave_like_select
|
2010-01-17 17:54:24 -05:00
|
|
|
assert_equal Topic.base.to_a.select(&:approved), Topic.base.to_a.find_all(&:approved)
|
2008-08-21 07:37:19 -04:00
|
|
|
end
|
2008-08-21 11:08:42 -04:00
|
|
|
|
2008-08-25 06:37:34 -04:00
|
|
|
def test_rand_should_select_a_random_object_from_proxy
|
2010-06-04 19:13:37 -04:00
|
|
|
assert_kind_of Topic, Topic.approved.sample
|
2008-08-25 06:37:34 -04:00
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_should_use_where_in_query_for_scope
|
2012-05-18 09:46:54 -04:00
|
|
|
assert_equal Developer.where(name: 'Jamis').to_set, Developer.where(id: Developer.jamises).to_set
|
2008-08-21 11:08:42 -04:00
|
|
|
end
|
2008-08-29 10:11:25 -04:00
|
|
|
|
|
|
|
def test_size_should_use_count_when_results_are_not_loaded
|
|
|
|
topics = Topic.base
|
|
|
|
assert_queries(1) do
|
|
|
|
assert_sql(/COUNT/i) { topics.size }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_should_use_length_when_results_are_loaded
|
|
|
|
topics = Topic.base
|
|
|
|
topics.reload # force load
|
|
|
|
assert_no_queries do
|
|
|
|
topics.size # use loaded (no query)
|
|
|
|
end
|
|
|
|
end
|
2008-09-23 16:32:17 -04:00
|
|
|
|
2011-12-17 06:41:17 -05:00
|
|
|
def test_should_not_duplicates_where_values
|
|
|
|
where_values = Topic.where("1=1").scope_with_lambda.where_values
|
|
|
|
assert_equal ["1=1"], where_values
|
|
|
|
end
|
|
|
|
|
2008-09-23 16:32:17 -04:00
|
|
|
def test_chaining_with_duplicate_joins
|
|
|
|
join = "INNER JOIN comments ON comments.post_id = posts.id"
|
|
|
|
post = Post.find(1)
|
2012-04-26 07:44:20 -04:00
|
|
|
assert_equal post.comments.size, Post.joins(join).joins(join).where("posts.id = #{post.id}").size
|
2008-09-23 16:32:17 -04:00
|
|
|
end
|
2009-01-24 12:54:10 -05:00
|
|
|
|
2009-03-10 07:49:58 -04:00
|
|
|
def test_chaining_should_use_latest_conditions_when_creating
|
|
|
|
post = Topic.rejected.new
|
|
|
|
assert !post.approved?
|
|
|
|
|
|
|
|
post = Topic.rejected.approved.new
|
|
|
|
assert post.approved?
|
2009-01-24 12:54:10 -05:00
|
|
|
|
2009-03-10 07:49:58 -04:00
|
|
|
post = Topic.approved.rejected.new
|
|
|
|
assert !post.approved?
|
|
|
|
|
|
|
|
post = Topic.approved.rejected.approved.new
|
|
|
|
assert post.approved?
|
2009-01-24 12:54:10 -05:00
|
|
|
end
|
|
|
|
|
2009-03-10 07:49:58 -04:00
|
|
|
def test_chaining_should_use_latest_conditions_when_searching
|
2009-01-24 12:54:10 -05:00
|
|
|
# Normal hash conditions
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_equal Topic.where(:approved => true).to_a, Topic.rejected.approved.to_a
|
|
|
|
assert_equal Topic.where(:approved => false).to_a, Topic.approved.rejected.to_a
|
2009-01-24 12:54:10 -05:00
|
|
|
|
|
|
|
# Nested hash conditions with same keys
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.to_a
|
2009-01-24 12:54:10 -05:00
|
|
|
|
|
|
|
# Nested hash conditions with different keys
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).to_a.uniq
|
2009-01-24 12:54:10 -05:00
|
|
|
end
|
2009-03-10 07:49:58 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_batch_finders
|
2009-03-11 10:26:26 -04:00
|
|
|
assert_equal 3, Topic.approved.count
|
|
|
|
|
|
|
|
assert_queries(4) do
|
|
|
|
Topic.approved.find_each(:batch_size => 1) {|t| assert t.approved? }
|
|
|
|
end
|
|
|
|
|
2009-03-11 11:24:30 -04:00
|
|
|
assert_queries(2) do
|
2009-03-11 10:26:26 -04:00
|
|
|
Topic.approved.find_in_batches(:batch_size => 2) do |group|
|
|
|
|
group.each {|t| assert t.approved? }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-08-09 19:41:36 -04:00
|
|
|
|
|
|
|
def test_table_names_for_chaining_scopes_with_and_without_table_name_included
|
|
|
|
assert_nothing_raised do
|
2012-07-27 07:01:25 -04:00
|
|
|
Comment.for_first_post.for_first_author.to_a
|
2009-08-09 19:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
2010-01-17 11:04:26 -05:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_on_relations
|
2010-01-22 09:40:29 -05:00
|
|
|
# Topic.replied
|
2012-07-27 12:27:47 -04:00
|
|
|
approved_topics = Topic.all.approved.order('id DESC')
|
2010-01-22 09:40:29 -05:00
|
|
|
assert_equal topics(:fourth), approved_topics.first
|
|
|
|
|
|
|
|
replied_approved_topics = approved_topics.replied
|
|
|
|
assert_equal topics(:third), replied_approved_topics.first
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_index_on_scope
|
2010-01-19 12:22:08 -05:00
|
|
|
approved = Topic.approved.order('id ASC')
|
|
|
|
assert_equal topics(:second), approved[0]
|
|
|
|
assert approved.loaded?
|
|
|
|
end
|
2010-03-30 09:41:56 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_nested_scopes_queries_size
|
2010-03-30 09:41:56 -04:00
|
|
|
assert_queries(1) do
|
2012-07-27 07:01:25 -04:00
|
|
|
Topic.approved.by_lifo.replied.written_before(Time.now).to_a
|
2010-03-30 09:41:56 -04:00
|
|
|
end
|
|
|
|
end
|
2010-04-05 16:54:39 -04:00
|
|
|
|
2011-03-28 17:36:29 -04:00
|
|
|
# Note: these next two are kinda odd because they are essentially just testing that the
|
|
|
|
# query cache works as it should, but they are here for legacy reasons as they was previously
|
|
|
|
# a separate cache on association proxies, and these show that that is not necessary.
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_are_cached_on_associations
|
2010-04-05 16:54:39 -04:00
|
|
|
post = posts(:welcome)
|
|
|
|
|
2011-03-28 17:36:29 -04:00
|
|
|
Post.cache do
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_queries(1) { post.comments.containing_the_letter_e.to_a }
|
|
|
|
assert_no_queries { post.comments.containing_the_letter_e.to_a }
|
2011-03-28 17:36:29 -04:00
|
|
|
end
|
2010-04-05 16:54:39 -04:00
|
|
|
end
|
2010-04-06 20:16:45 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_with_arguments_are_cached_on_associations
|
2010-06-03 19:53:45 -04:00
|
|
|
post = posts(:welcome)
|
|
|
|
|
2011-03-28 17:36:29 -04:00
|
|
|
Post.cache do
|
2012-07-27 07:01:25 -04:00
|
|
|
one = assert_queries(1) { post.comments.limit_by(1).to_a }
|
2011-03-28 17:36:29 -04:00
|
|
|
assert_equal 1, one.size
|
2010-06-03 19:53:45 -04:00
|
|
|
|
2012-07-27 07:01:25 -04:00
|
|
|
two = assert_queries(1) { post.comments.limit_by(2).to_a }
|
2011-03-28 17:36:29 -04:00
|
|
|
assert_equal 2, two.size
|
2010-06-03 19:53:45 -04:00
|
|
|
|
2012-07-27 07:01:25 -04:00
|
|
|
assert_no_queries { post.comments.limit_by(1).to_a }
|
|
|
|
assert_no_queries { post.comments.limit_by(2).to_a }
|
2011-03-28 17:36:29 -04:00
|
|
|
end
|
2010-06-03 19:53:45 -04:00
|
|
|
end
|
|
|
|
|
2011-06-02 22:58:00 -04:00
|
|
|
def test_scopes_to_get_newest
|
|
|
|
post = posts(:welcome)
|
|
|
|
old_last_comment = post.comments.newest
|
|
|
|
new_comment = post.comments.create(:body => "My new comment")
|
|
|
|
assert_equal new_comment, post.comments.newest
|
|
|
|
assert_not_equal old_last_comment, post.comments.newest
|
|
|
|
end
|
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scopes_are_reset_on_association_reload
|
2010-04-06 20:16:45 -04:00
|
|
|
post = posts(:welcome)
|
|
|
|
|
|
|
|
[:destroy_all, :reset, :delete_all].each do |method|
|
|
|
|
before = post.comments.containing_the_letter_e
|
2011-02-17 18:55:05 -05:00
|
|
|
post.association(:comments).send(method)
|
2011-05-11 06:37:35 -04:00
|
|
|
assert before.object_id != post.comments.containing_the_letter_e.object_id, "CollectionAssociation##{method} should reset the named scopes cache"
|
2010-04-06 20:16:45 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-29 13:47:04 -04:00
|
|
|
|
2010-11-24 03:17:49 -05:00
|
|
|
def test_scoped_are_lazy_loaded_if_table_still_does_not_exist
|
2010-06-29 13:47:04 -04:00
|
|
|
assert_nothing_raised do
|
|
|
|
require "models/without_table"
|
|
|
|
end
|
|
|
|
end
|
2012-03-21 18:18:18 -04:00
|
|
|
|
|
|
|
def test_eager_scopes_are_deprecated
|
|
|
|
klass = Class.new(ActiveRecord::Base)
|
|
|
|
klass.table_name = 'posts'
|
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
klass.scope :welcome_2, klass.where(:id => posts(:welcome).id)
|
|
|
|
end
|
|
|
|
assert_equal [posts(:welcome).title], klass.welcome_2.map(&:title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eager_default_scope_relations_are_deprecated
|
|
|
|
klass = Class.new(ActiveRecord::Base)
|
|
|
|
klass.table_name = 'posts'
|
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
klass.send(:default_scope, klass.where(:id => posts(:welcome).id))
|
|
|
|
end
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal [posts(:welcome).title], klass.all.map(&:title)
|
2012-03-21 18:18:18 -04:00
|
|
|
end
|
2008-03-23 22:50:02 -04:00
|
|
|
end
|