class NamedScopingTest
Public Class Methods
pub()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 298 def pub; end
Protected Class Methods
pro()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 304 def pro; end
Private Class Methods
pri()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 301 def pri; end
Public Instance Methods
test_active_records_have_scope_named__all__()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 154 def test_active_records_have_scope_named__all__ assert !Topic.all.empty? assert_equal Topic.all.to_a, Topic.base end
test_active_records_have_scope_named__scoped__()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 160 def test_active_records_have_scope_named__scoped__ scope = Topic.where("content LIKE '%Have%'") assert !scope.empty? assert_equal scope, Topic.all.merge!(where: "content LIKE '%Have%'") end
test_any_should_call_proxy_found_if_using_a_block()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 199 def test_any_should_call_proxy_found_if_using_a_block topics = Topic.base assert_queries(1) do assert_not_called(topics, :empty?) do topics.any? { true } end end end
test_any_should_not_fire_query_if_scope_loaded()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 208 def test_any_should_not_fire_query_if_scope_loaded topics = Topic.base topics.load # force load assert_no_queries { assert topics.any? } end
test_any_should_not_load_results()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 190 def test_any_should_not_load_results topics = Topic.base assert_queries(2) do topics.any? # use count query topics.load # force load topics.any? # use loaded (no query) end end
test_calling_merge_at_first_in_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 51 def test_calling_merge_at_first_in_scope Topic.class_eval do scope :calling_merge_at_first_in_scope, Proc.new { merge(Topic.replied) } end assert_equal Topic.calling_merge_at_first_in_scope.to_a, Topic.replied.to_a end
test_chaining_applies_last_conditions_when_creating()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 408 def test_chaining_applies_last_conditions_when_creating post = Topic.rejected.new assert !post.approved? post = Topic.rejected.approved.new assert post.approved? post = Topic.approved.rejected.new assert !post.approved? post = Topic.approved.rejected.approved.new assert post.approved? end
test_chaining_combines_conditions_when_searching()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 422 def test_chaining_combines_conditions_when_searching # Normal hash conditions assert_equal Topic.where(approved: false).where(approved: true).to_a, Topic.rejected.approved.to_a assert_equal Topic.where(approved: true).where(approved: false).to_a, Topic.approved.rejected.to_a # Nested hash conditions with same keys assert_equal [], Post.with_special_comments.with_very_special_comments.to_a # Nested hash conditions with different keys assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).to_a.uniq end
test_chaining_with_duplicate_joins()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 402 def test_chaining_with_duplicate_joins join = "INNER JOIN comments ON comments.post_id = posts.id" post = Post.find(1) assert_equal post.comments.size, Post.joins(join).joins(join).where("posts.id = #{post.id}").size end
test_delegates_finds_and_calculations_to_the_base_class()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 42 def test_delegates_finds_and_calculations_to_the_base_class assert !Topic.all.empty? assert_equal Topic.all.to_a, Topic.base.to_a assert_equal Topic.first, Topic.base.first assert_equal Topic.count, Topic.base.count assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count) end
test_eager_default_scope_relations_are_remove()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 545 def test_eager_default_scope_relations_are_remove klass = Class.new(ActiveRecord::Base) klass.table_name = "posts" assert_raises(ArgumentError) do klass.send(:default_scope, klass.where(id: posts(:welcome).id)) end end
test_empty_should_not_load_results()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 181 def test_empty_should_not_load_results topics = Topic.base assert_queries(2) do topics.empty? # use count query topics.load # force load topics.empty? # use loaded (no query) end end
test_find_all_should_behave_like_select()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 370 def test_find_all_should_behave_like_select assert_equal Topic.base.to_a.select(&:approved), Topic.base.to_a.find_all(&:approved) end
test_first_and_last_should_allow_integers_for_limit()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 167 def test_first_and_last_should_allow_integers_for_limit assert_equal Topic.base.first(2), Topic.base.order("id").to_a.first(2) assert_equal Topic.base.last(2), Topic.base.order("id").to_a.last(2) end
test_first_and_last_should_not_use_query_when_results_are_loaded()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 172 def test_first_and_last_should_not_use_query_when_results_are_loaded topics = Topic.base topics.load # force load assert_no_queries do topics.first topics.last end end
test_found_items_are_cached()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 24 def test_found_items_are_cached all_posts = Topic.base assert_queries(1) do all_posts.collect { true } all_posts.collect { true } end end
test_has_many_associations_have_access_to_scopes()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 116 def test_has_many_associations_have_access_to_scopes assert_not_equal Post.containing_the_letter_a, authors(:david).posts assert !Post.containing_the_letter_a.empty? expected = authors(:david).posts & Post.containing_the_letter_a assert_equal expected.sort_by(&:id), authors(:david).posts.containing_the_letter_a.sort_by(&:id) end
test_has_many_through_associations_have_access_to_scopes()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 129 def test_has_many_through_associations_have_access_to_scopes assert_not_equal Comment.containing_the_letter_e, authors(:david).comments assert !Comment.containing_the_letter_e.empty? expected = authors(:david).comments & Comment.containing_the_letter_e assert_equal expected.sort_by(&:id), authors(:david).comments.containing_the_letter_e.sort_by(&:id) end
test_implements_enumerable()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 15 def test_implements_enumerable assert !Topic.all.empty? 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 } end
test_index_on_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 482 def test_index_on_scope approved = Topic.approved.order("id ASC") assert_equal topics(:second), approved[0] assert approved.loaded? end
test_many_should_call_proxy_found_if_using_a_block()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 229 def test_many_should_call_proxy_found_if_using_a_block topics = Topic.base assert_queries(1) do assert_not_called(topics, :size) do topics.many? { true } end end end
test_many_should_not_fire_query_if_scope_loaded()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 238 def test_many_should_not_fire_query_if_scope_loaded topics = Topic.base topics.load # force load assert_no_queries { assert topics.many? } end
test_many_should_not_load_results()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 220 def test_many_should_not_load_results topics = Topic.base assert_queries(2) do topics.many? # use count query topics.load # force load topics.many? # use loaded (no query) end end
test_many_should_return_false_if_none_or_one()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 244 def test_many_should_return_false_if_none_or_one topics = Topic.base.where(id: 0) assert !topics.many? topics = Topic.base.where(id: 1) assert !topics.many? end
test_many_should_return_true_if_more_than_one()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 251 def test_many_should_return_true_if_more_than_one assert Topic.base.many? end
test_method_missing_priority_when_delegating()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 58 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 assert_equal klazz.to.since.to_a, klazz.since.to.to_a end
test_model_class_should_respond_to_any()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 214 def test_model_class_should_respond_to_any assert Topic.any? Topic.delete_all assert !Topic.any? end
test_model_class_should_respond_to_extending()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 558 def test_model_class_should_respond_to_extending assert_raises OopsError do Comment.unscoped.oops_comments.destroy_all end end
test_model_class_should_respond_to_many()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 255 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
test_model_class_should_respond_to_none()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 564 def test_model_class_should_respond_to_none assert !Topic.none? Topic.delete_all assert Topic.none? end
test_model_class_should_respond_to_one()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 570 def test_model_class_should_respond_to_one assert !Topic.one? Topic.delete_all assert !Topic.one? Topic.create! assert Topic.one? end
test_nested_scopes_queries_size()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 488 def test_nested_scopes_queries_size assert_queries(1) do Topic.approved.by_lifo.replied.written_before(Time.now).to_a end end
test_procedural_scopes()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 95 def test_procedural_scopes 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) assert_not_equal topics_written_before_the_second, topics_written_before_the_third 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
test_procedural_scopes_returning_nil()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 104 def test_procedural_scopes_returning_nil all_topics = Topic.all assert_equal all_topics, Topic.written_before(nil) end
test_rand_should_select_a_random_object_from_proxy()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 374 def test_rand_should_select_a_random_object_from_proxy assert_kind_of Topic, Topic.approved.sample end
test_reload_expires_cache_of_found_items()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 33 def test_reload_expires_cache_of_found_items all_posts = Topic.base all_posts.to_a new_post = Topic.create! assert_not_includes all_posts, new_post assert_includes all_posts.reload, new_post end
test_reserved_scope_names()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 290 def test_reserved_scope_names klass = Class.new(ActiveRecord::Base) do self.table_name = "topics" scope :approved, -> { where(approved: true) } class << self public def pub; end private def pri; end protected def pro; end end end subklass = Class.new(klass) conflicts = [ :create, # public class method on AR::Base :relation, # private class method on AR::Base :new, # redefined class method on AR::Base :all, # a default scope :public, # some important methods on Module and Class :protected, :private, :name, :parent, :superclass ] non_conflicts = [ :find_by_title, # dynamic finder method :approved, # existing scope :pub, # existing public class method :pri, # existing private class method :pro, # existing protected class method :open, # a ::Kernel method ] conflicts.each do |name| e = assert_raises(ArgumentError, "scope `#{name}` should not be allowed") do klass.class_eval { scope name, -> { where(approved: true) } } end assert_match(/You tried to define a scope named \"#{name}\" on the model/, e.message) e = assert_raises(ArgumentError, "scope `#{name}` should not be allowed") do subklass.class_eval { scope name, -> { where(approved: true) } } end assert_match(/You tried to define a scope named \"#{name}\" on the model/, e.message) end non_conflicts.each do |name| assert_nothing_raised do silence_warnings do klass.class_eval { scope name, -> { where(approved: true) } } end end assert_nothing_raised do subklass.class_eval { scope name, -> { where(approved: true) } } end end end
test_scope_should_respond_to_own_methods_and_methods_of_the_proxy()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 67 def test_scope_should_respond_to_own_methods_and_methods_of_the_proxy assert Topic.approved.respond_to?(:limit) assert Topic.approved.respond_to?(:count) assert Topic.approved.respond_to?(:length) end
test_scope_with_STI()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 124 def test_scope_with_STI assert_equal 3, Post.containing_the_letter_a.count assert_equal 1, SpecialPost.containing_the_letter_a.count end
test_scope_with_object()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 110 def test_scope_with_object objects = Topic.with_object assert_operator objects.length, :>, 0 assert objects.all?(&:approved?), "all objects should be approved" end
test_scoped_are_lazy_loaded_if_table_still_does_not_exist()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 539 def test_scoped_are_lazy_loaded_if_table_still_does_not_exist assert_nothing_raised do require "models/without_table" end end
test_scopes_are_cached_on_associations()
click to toggle source
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.
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 497 def test_scopes_are_cached_on_associations post = posts(:welcome) Post.cache do assert_queries(1) { post.comments.containing_the_letter_e.to_a } assert_no_queries { post.comments.containing_the_letter_e.to_a } end end
test_scopes_are_composable()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 86 def test_scopes_are_composable 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) assert !(approved == replied) assert !(approved & replied).empty? assert_equal approved & replied, Topic.approved.replied end
test_scopes_are_reset_on_association_reload()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 529 def test_scopes_are_reset_on_association_reload post = posts(:welcome) [:destroy_all, :reset, :delete_all].each do |method| before = post.comments.containing_the_letter_e post.association(:comments).send(method) assert before.object_id != post.comments.containing_the_letter_e.object_id, "CollectionAssociation##{method} should reset the named scopes cache" end end
test_scopes_batch_finders()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 434 def test_scopes_batch_finders assert_equal 4, Topic.approved.count assert_queries(5) do Topic.approved.find_each(batch_size: 1) { |t| assert t.approved? } end assert_queries(3) do Topic.approved.find_in_batches(batch_size: 2) do |group| group.each { |t| assert t.approved? } end end end
test_scopes_body_is_a_callable()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 147 def test_scopes_body_is_a_callable e = assert_raises ArgumentError do Class.new(Post).class_eval { scope :containing_the_letter_z, where("body LIKE '%z%'") } end assert_equal "The scope body needs to be callable.", e.message end
test_scopes_honor_current_scopes_from_when_defined()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 137 def test_scopes_honor_current_scopes_from_when_defined 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) assert_not_equal Post.top(5), authors(:david).posts.top(5) # Oracle sometimes sorts differently if WHERE condition is changed 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) assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5) end
test_scopes_on_relations()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 473 def test_scopes_on_relations # Topic.replied approved_topics = Topic.all.approved.order("id DESC") assert_equal topics(:fifth), approved_topics.first replied_approved_topics = approved_topics.replied assert_equal topics(:third), replied_approved_topics.first end
test_scopes_to_get_newest()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 521 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
test_scopes_with_arguments_are_cached_on_associations()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 506 def test_scopes_with_arguments_are_cached_on_associations post = posts(:welcome) Post.cache do one = assert_queries(1) { post.comments.limit_by(1).to_a } assert_equal 1, one.size two = assert_queries(1) { post.comments.limit_by(2).to_a } assert_equal 2, two.size assert_no_queries { post.comments.limit_by(1).to_a } assert_no_queries { post.comments.limit_by(2).to_a } end end
test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 73 def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified assert !Topic.all.merge!(where: { approved: true }).to_a.empty? assert_equal Topic.all.merge!(where: { approved: true }).to_a, Topic.approved assert_equal Topic.where(approved: true).count, Topic.approved.count end
test_scopes_with_reserved_names()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 454 def test_scopes_with_reserved_names class << Topic def public_method; end public :public_method def protected_method; end protected :protected_method def private_method; end private :private_method end [:public_method, :protected_method, :private_method].each do |reserved_method| assert Topic.respond_to?(reserved_method, true) ActiveRecord::Base.logger.expects(:warn) silence_warnings { Topic.scope(reserved_method, -> {}) } end end
test_scopes_with_string_name_can_be_composed()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 80 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
test_should_build_new_on_top_of_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 269 def test_should_build_new_on_top_of_scope topic = Topic.approved.new assert topic.approved end
test_should_build_on_top_of_chained_scopes()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 284 def test_should_build_on_top_of_chained_scopes topic = Topic.approved.by_lifo.build({}) assert topic.approved assert_equal "lifo", topic.author_name end
test_should_build_on_top_of_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 264 def test_should_build_on_top_of_scope topic = Topic.approved.build({}) assert topic.approved end
test_should_create_on_top_of_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 274 def test_should_create_on_top_of_scope topic = Topic.approved.create({}) assert topic.approved end
test_should_create_with_bang_on_top_of_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 279 def test_should_create_with_bang_on_top_of_scope topic = Topic.approved.create!({}) assert topic.approved end
test_should_not_duplicates_where_values()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 397 def test_should_not_duplicates_where_values relation = Topic.where("1=1") assert_equal relation.where_clause, relation.scope_with_lambda.where_clause end
test_should_use_where_in_query_for_scope()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 378 def test_should_use_where_in_query_for_scope assert_equal Developer.where(name: "Jamis").to_set, Developer.where(id: Developer.jamises).to_set end
test_size_should_use_count_when_results_are_not_loaded()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 382 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
test_size_should_use_length_when_results_are_loaded()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 389 def test_size_should_use_length_when_results_are_loaded topics = Topic.base topics.load # force load assert_no_queries do topics.size # use loaded (no query) end end
test_spaces_in_scope_names()
click to toggle source
Method
delegation for scope names which look like /Aw*?z/ has been done by evaluating a string with a plain def statement. For scope names which contain spaces this approach doesn't work.
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 360 def test_spaces_in_scope_names klass = Class.new(ActiveRecord::Base) do self.table_name = "topics" scope :"title containing space", -> { where("title LIKE '% %'") } scope :approved, -> { where(approved: true) } end assert_equal klass.send(:"title containing space"), klass.where("title LIKE '% %'") assert_equal klass.approved.send(:"title containing space"), klass.approved.where("title LIKE '% %'") end
test_subclass_merges_scopes_properly()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 554 def test_subclass_merges_scopes_properly assert_equal 1, SpecialComment.where(body: "go crazy").created.count end
test_table_names_for_chaining_scopes_with_and_without_table_name_included()
click to toggle source
# File activerecord/test/cases/scoping/named_scoping_test.rb, line 448 def test_table_names_for_chaining_scopes_with_and_without_table_name_included assert_nothing_raised do Comment.for_first_post.for_first_author.to_a end end