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'
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
assert ! Topic . find ( :all ) . empty?
2008-03-30 02:17:37 -04:00
2008-04-30 01:25:52 -04:00
assert_equal Topic . find ( :all ) , Topic . base
assert_equal Topic . find ( :all ) , Topic . base . to_a
assert_equal Topic . find ( :first ) , Topic . base . first
2009-03-11 10:26:26 -04:00
assert_equal Topic . find ( :all ) , 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
Topic . columns
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
2010-01-17 17:54:24 -05:00
all_posts . all
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
assert ! Topic . find ( :all ) . empty?
2008-03-30 02:17:37 -04:00
2008-04-30 01:25:52 -04:00
assert_equal Topic . find ( :all ) , Topic . base . find ( :all )
assert_equal Topic . find ( :first ) , Topic . base . find ( :first )
assert_equal Topic . count , Topic . base . count
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
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
assert ! Topic . find ( :all , :conditions = > { :approved = > true } ) . empty?
2008-03-30 02:17:37 -04:00
2008-03-23 22:50:02 -04:00
assert_equal Topic . find ( :all , :conditions = > { :approved = > true } ) , Topic . approved
assert_equal Topic . count ( :conditions = > { :approved = > true } ) , Topic . approved . count
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-06-27 20:57:32 -04:00
def test_scopes_can_be_specified_with_deep_hash_conditions
assert_equal Topic . replied . approved , Topic . replied . approved_as_hash_condition
end
2008-03-23 22:50:02 -04:00
def test_scopes_are_composable
2010-03-15 21:09:33 -04:00
assert_equal ( ( approved = Topic . find ( :all , :conditions = > { :approved = > true } ) ) , Topic . approved )
assert_equal ( ( replied = Topic . find ( :all , :conditions = > 'replies_count > 0' ) ) , 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
topics_written_before_the_third = Topic . find ( :all , :conditions = > [ 'written_on < ?' , topics ( :third ) . written_on ] )
topics_written_before_the_second = Topic . find ( :all , :conditions = > [ 'written_on < ?' , topics ( :second ) . written_on ] )
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
all_topics = Topic . find ( :all )
assert_equal all_topics , Topic . written_before ( nil )
end
2008-06-21 07:41:30 -04:00
def test_scopes_with_joins
address = author_addresses ( :david_address )
posts_with_authors_at_address = Post . find (
:all , :joins = > 'JOIN authors ON authors.id = posts.author_id' ,
:conditions = > [ 'authors.author_address_id = ?' , address . id ]
)
assert_equal posts_with_authors_at_address , Post . with_authors_at_address ( address )
end
def test_scopes_with_joins_respects_custom_select
address = author_addresses ( :david_address )
posts_with_authors_at_address_titles = Post . find ( :all ,
:select = > 'title' ,
:joins = > 'JOIN authors ON authors.id = posts.author_id' ,
:conditions = > [ 'authors.author_address_id = ?' , address . id ]
)
2010-11-16 09:36:47 -05:00
assert_equal posts_with_authors_at_address_titles . map ( & :title ) , Post . with_authors_at_address ( address ) . find ( :all , :select = > 'title' ) . map ( & :title )
2008-06-21 07:41:30 -04:00
end
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
2008-03-23 22:50:02 -04:00
def test_extensions
assert_equal 1 , Topic . anonymous_extension . one
assert_equal 2 , Topic . named_extension . two
end
2008-03-30 02:17:37 -04:00
2008-03-23 22:50:02 -04:00
def test_multiple_extensions
assert_equal 2 , Topic . multiple_extensions . extension_two
2008-03-30 02:17:37 -04:00
assert_equal 1 , Topic . multiple_extensions . extension_one
2008-03-23 22:50:02 -04:00
end
2008-03-30 02:17:37 -04:00
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__
assert ! Topic . find ( :all ) . empty?
2008-03-30 02:17:37 -04:00
2008-04-30 01:25:52 -04:00
assert_equal Topic . find ( :all ) , 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__
assert ! Topic . find ( :all , scope = { :conditions = > " content LIKE '%Have%' " } ) . empty?
2008-03-30 02:17:37 -04:00
2008-03-23 22:50:02 -04:00
assert_equal Topic . find ( :all , scope ) , Topic . scoped ( scope )
end
2008-05-11 15:33:25 -04:00
2008-05-20 07:11:25 -04:00
def test_first_and_last_should_support_find_options
assert_equal Topic . base . first ( :order = > 'title' ) , Topic . base . find ( :first , :order = > 'title' )
assert_equal Topic . base . last ( :order = > 'title' ) , Topic . base . find ( :last , :order = > 'title' )
end
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
def test_first_and_last_find_options_should_use_query_when_results_are_loaded
topics = Topic . base
topics . reload # force load
assert_queries ( 2 ) do
topics . first ( :order = > 'title' )
topics . last ( :order = > 'title' )
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
topics = Topic . base . scoped ( :conditions = > { :id = > 0 } )
assert ! topics . many?
topics = Topic . base . scoped ( :conditions = > { :id = > 1 } )
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
2008-12-29 20:14:30 -05:00
assert_equal Developer . find_all_by_name ( 'Jamis' ) . to_set , Developer . find_all_by_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 )
assert_equal post . comments . size , Post . scoped ( :joins = > join ) . scoped ( :joins = > join , :conditions = > " posts.id = #{ post . id } " ) . size
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
2010-01-17 13:33:18 -05:00
assert_equal Topic . where ( :approved = > true ) . to_a , Topic . rejected . approved . all
assert_equal Topic . where ( :approved = > false ) . to_a , Topic . approved . rejected . all
2009-01-24 12:54:10 -05:00
# Nested hash conditions with same keys
2010-01-17 13:33:18 -05:00
assert_equal [ posts ( :sti_comments ) ] , Post . with_special_comments . with_very_special_comments . all
2009-01-24 12:54:10 -05:00
# Nested hash conditions with different keys
2010-01-17 13:33:18 -05:00
assert_equal [ posts ( :sti_comments ) ] , Post . with_special_comments . with_post ( 4 ) . all . 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
Comment . for_first_post . for_first_author . all
end
end
2010-01-17 11:04:26 -05:00
2010-11-24 03:17:49 -05:00
def test_scopes_with_reserved_names
2010-03-01 22:24:35 -05:00
class << Topic
def public_method ; end
public :public_method
2010-03-30 09:41:56 -04:00
2010-03-01 22:24:35 -05:00
def protected_method ; end
protected :protected_method
2010-03-30 09:41:56 -04:00
2010-03-01 22:24:35 -05:00
def private_method ; end
private :private_method
end
2010-03-30 09:41:56 -04:00
2010-03-01 22:24:35 -05:00
[ :public_method , :protected_method , :private_method ] . each do | reserved_method |
assert Topic . respond_to? ( reserved_method , true )
ActiveRecord :: Base . logger . expects ( :warn )
Topic . scope ( reserved_method )
2010-01-17 11:04:26 -05:00
end
end
2010-01-17 18:08:19 -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
approved_topics = Topic . scoped . approved . order ( 'id DESC' )
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
Topic . approved . by_lifo . replied . written_before ( Time . now ) . all
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
assert_queries ( 1 ) { post . comments . containing_the_letter_e . all }
assert_no_queries { post . comments . containing_the_letter_e . all }
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
one = assert_queries ( 1 ) { post . comments . limit_by ( 1 ) . all }
assert_equal 1 , one . size
2010-06-03 19:53:45 -04:00
2011-03-28 17:36:29 -04:00
two = assert_queries ( 1 ) { post . comments . limit_by ( 2 ) . all }
assert_equal 2 , two . size
2010-06-03 19:53:45 -04:00
2011-03-28 17:36:29 -04:00
assert_no_queries { post . comments . limit_by ( 1 ) . all }
assert_no_queries { post . comments . limit_by ( 2 ) . all }
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
2008-03-23 22:50:02 -04:00
end
2008-12-28 14:25:55 -05:00
2009-05-18 19:17:35 -04:00
class DynamicScopeMatchTest < ActiveRecord :: TestCase
2008-12-28 14:25:55 -05:00
def test_scoped_by_no_match
assert_nil ActiveRecord :: DynamicScopeMatch . match ( " not_scoped_at_all " )
end
def test_scoped_by
match = ActiveRecord :: DynamicScopeMatch . match ( " scoped_by_age_and_sex_and_location " )
assert_not_nil match
assert match . scope?
assert_equal %w( age sex location ) , match . attribute_names
end
end
class DynamicScopeTest < ActiveRecord :: TestCase
2009-05-18 19:17:35 -04:00
fixtures :posts
2011-07-17 18:41:02 -04:00
def setup
2011-07-17 22:49:04 -04:00
@test_klass = Class . new ( Post ) do
def self . name ; " Post " ; end
2011-07-17 18:41:02 -04:00
end
end
2008-12-28 14:25:55 -05:00
def test_dynamic_scope
2011-07-17 22:49:04 -04:00
assert_equal @test_klass . scoped_by_author_id ( 1 ) . find ( 1 ) , @test_klass . find ( 1 )
assert_equal @test_klass . scoped_by_author_id_and_title ( 1 , " Welcome to the weblog " ) . first , @test_klass . find ( :first , :conditions = > { :author_id = > 1 , :title = > " Welcome to the weblog " } )
2008-12-28 14:25:55 -05:00
end
2010-08-02 21:22:02 -04:00
def test_dynamic_scope_should_create_methods_after_hitting_method_missing
2011-07-17 22:49:04 -04:00
assert_blank @test_klass . methods . grep ( / scoped_by_type / )
@test_klass . scoped_by_type ( nil )
assert_present @test_klass . methods . grep ( / scoped_by_type / )
2010-08-02 21:22:02 -04:00
end
2011-07-17 18:41:02 -04:00
def test_dynamic_scope_with_less_number_of_arguments
2011-07-17 22:49:04 -04:00
assert_raise ( ArgumentError ) { @test_klass . scoped_by_author_id_and_title ( 1 ) }
2011-07-17 18:41:02 -04:00
end
2008-12-28 14:25:55 -05:00
end