require "#{File.dirname(__FILE__)}/../init" require "#{File.dirname(__FILE__)}/abstract_unit" require "#{File.dirname(__FILE__)}/test_models" class ActsAsHotRecordTest < Test::Unit::TestCase DATE_NOW = Date.today def teardown HotModel.delete_all HotModelModified.delete_all end #test for HotModel Class #set the hotness_level of our object with the default hotness values def test_set_hotness_default_values hot_model = create_hotmodel #set the hotness level of our object with the default values hot_model.set_hotness assert_equal 1, hot_model.hotness_level , "hotness_level must be the default min_level" assert_equal DATE_NOW + 1, hot_model.hotness_expired_at , "hotness_expired_at must be default to + 1.day of Today" assert_equal 1, HotModel.hot_records.size assert_equal hot_model.id, HotModel.hot_records.first.id end #test for HotModelModified Class #set the hotness_level of our object with the default hotness values def test_set_hotness_default_values hot_model = create_hot_model_modified #set the hotness level of our object with the default values hot_model.set_hotness assert_equal 100, hot_model.hotness_level , "hotness_level must be the default min_level" assert_equal DATE_NOW + 1, hot_model.hotness_expired_at , "hotness_expired_at must be default to + 1.day of Today" assert_equal 1, HotModelModified.hot_records.size assert_equal hot_model.id, HotModelModified.hot_records.first.id end #test for HotModel Class #set the hotness_level of our object with our given values def test_set_hotness_given_values hot_model = create_hotmodel expiry_date = DATE_NOW + 3 hot_model.set_hotness(:level=> 2, :expiry_date=> expiry_date ) assert_equal 2, hot_model.hotness_level , "hotness_level must be eqaul to the given option" assert_equal expiry_date, hot_model.hotness_expired_at, "hotness_expired_at must be equal to the expiry_date option" end #test for HotModelModified Class #set the hotness_level of our object with our given values def test_set_hotness_given_values_modified hot_model = create_hot_model_modified expiry_date = DATE_NOW + 3 hot_model.set_hotness(:level=> 105, :expiry_date=> expiry_date ) assert_equal 105, hot_model.hotness_level , "hotness_level must be eqaul to the given option" assert_equal expiry_date, hot_model.hotness_expired_at, "hotness_expired_at must be equal to the expiry_date option" #create another hot model with the max_level hot_model_2 = HotModelModified.new(:name => 'test') hot_model_2.save expiry_date = DATE_NOW + 3 assert_raise(June::Acts::HotRecord::HotnessMaxDuplicateError) {hot_model_2.set_hotness(:level=> 105, :expiry_date=> expiry_date )} hot_records = HotModelModified.hot_records(:level=> 105 ) assert_equal 1, hot_records.size , "there is only one hot_record with hotness_level 105" end #set the hot record to unhot def test_un_hot! hot_model = create_hotmodel hot_model.set_hotness assert_equal 1, hot_model.hotness_level , "hotness_level must be the default min_level" #we now un_hot our hot record hot_model.un_hot! assert_equal nil, hot_model.hotness_level , "hotness_level must be nil" end def test_hot_records 20.times do |i| hot_model = HotModel.new(:name => "Record #{i}") hot_model.set_hotness end # create a new record with hotness_level set to 2 hot_model = HotModel.new(:name => "Record 21") hot_model.set_hotness(:level=> 2) # gets all hot records for the class hot_records = HotModel.hot_records assert_equal 10, hot_records.size , "Default limit for hot_records is 10" # gets all hot records with the given options hot_records = HotModel.hot_records(:level=> 1 , :limit=> 5) assert_equal 5, hot_records.size , "limit must be equal to the given options" # gets all hot records with the given options hot_records = HotModel.hot_records(:level=> 2 , :limit=> 5) assert_equal 1, hot_records.size , "there is only one hot_record with hotness_level 2" end # test support for migration def test_remove_add_table_columns # our AR class HotModel has already hot columns so we will try to remove it # this method is mostly intended to be used for migration HotModel.remove_hot_columns columns = HotModel.column_names # assert if the columns are now removed assert !columns.include?('hotness_level') , "hotness_level is now remove from the table of our AR class" # now we will add our hot columns to our AR class HotModel.add_hot_columns columns = HotModel.column_names # assert if the columns are now added assert columns.include?('hotness_level') , "hotness_level is now added to the table of our AR class" end private def create_hotmodel hot_model = HotModel.new(:name => 'test') hot_model.save assert_equal 1, HotModel.find(:all).size, "records size must be equal to one" assert_equal nil, hot_model.hotness_level, "defaul hotness_level must be null" assert_equal nil, hot_model.hotness_expired_at, "defaul hotness_expiration date must be null" return hot_model end def create_hot_model_modified hot_model = HotModelModified.new(:name => 'test') hot_model.save assert_equal 1, HotModelModified.find(:all).size, "records size must be equal to one" assert_equal nil, hot_model.hotness_level, "defaul hotness_level must be null" assert_equal nil, hot_model.hotness_expired_at, "defaul hotness_expiration date must be null" return hot_model end end