spec/monocle_spec.rb in monocle-0.2.2 vs spec/monocle_spec.rb in monocle-0.2.3

- old
+ new

@@ -19,19 +19,23 @@ attr_accessor :updated_at attr_accessor :overall_views, :yearly_views, :monthly_views attr_accessor :weekly_views, :daily_views, :hourly_views attr_accessor :quarterly_views + attr_accessor :overall_clicks, :yearly_clicks, :monthly_clicks + attr_accessor :weekly_clicks, :daily_clicks, :hourly_clicks + attr_accessor :quarterly_clicks + def self.find(id) o = new o.id = id.to_i o end def initialize @id = '12345' - @overall_views = 0 + @overall_views, @overall_clicks = 0 @updated_at = Time.now - 1.hour end def update_column(field, count) self.send("#{field}=", count) @@ -41,10 +45,11 @@ def make_viewed_objects(number_of_objects_to_make) number_of_objects_to_make.times do |i| o = TestObject.new o.id = i o.view! + o.click! end end describe Monocle do @@ -79,16 +84,18 @@ describe '#destroy_views' do it 'deletes views for object' do object.view! object.destroy_views REDIS.hget('monocle:test_object:12345', 'overall_views').should == nil + REDIS.hget('monocle:test_object:12345', 'overall_clicks').should == nil end end describe '#cache_field_for_view' do it 'returns the cache field for the given view type' do object.cache_field_for_view('overall').should == :overall_views + object.cache_field_for_click('overall').should == :overall_clicks end end describe '#should_cache_view_count?' do context 'the class has cache_view_counts set to true' do @@ -115,11 +122,11 @@ end context 'when cache time is over threshold' do describe '#view!' do before { object.stub(:updated_at).and_return(Time.now - 1.hour) } - before { 50.times { object.view! }} + before { 50.times { object.view! } } after { object.destroy_views } %w(overall yearly monthly weekly daily hourly quarterly).each do |view_type| it "sets #{view_type} views count" do object.send("#{view_type}_views_count").should == 50 @@ -128,10 +135,26 @@ it "updates cached #{view_type} views count" do object.send("#{view_type}_views").should == 50 end end end + + describe '#click!' do + before { object.stub(:updated_at).and_return(Time.now - 1.hour) } + before { 50.times { object.click! } } + after { object.destroy_views } + + %w(overall yearly monthly weekly daily hourly quarterly).each do |view_type| + it "sets #{view_type} views count" do + object.send("#{view_type}_clicks_count").should == 50 + end + + it "updates cached #{view_type} views count" do + object.send("#{view_type}_clicks").should == 50 + end + end + end end context 'when cache time is under threshold' do describe '#view!' do before { object.stub(:updated_at).and_return(Time.now) } @@ -143,9 +166,25 @@ object.send("#{view_type}_views_count").should == 50 end it "updates cached #{view_type} views count" do object.send("#{view_type}_views").to_i.should == 0 + end + end + end + + describe '#click!' do + before { object.stub(:updated_at).and_return(Time.now) } + before { 50.times { object.click! }} + after { object.destroy_views } + + %w(overall yearly monthly weekly daily hourly quarterly).each do |view_type| + it "sets #{view_type} views count" do + object.send("#{view_type}_clicks_count").should == 50 + end + + it "updates cached #{view_type} views count" do + object.send("#{view_type}_clicks").to_i.should == 0 end end end end end