Make Model.expire act like the other options, and add tests for it

This commit is contained in:
2012-04-18 15:08:25 +01:00
parent fccf0db68a
commit e15f637603
4 changed files with 24 additions and 2 deletions

View File

@@ -46,8 +46,12 @@ module Redistat
alias :class_name :scope
def expire(exp)
options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp)
def expire(exp = nil)
if !exp.nil?
options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp)
else
options[:expire]
end
end
def connect_to(opts = {})

View File

@@ -26,5 +26,6 @@ class ModelHelper4
include Redistat::Model
scope "FancyHelper"
expire :hour => 24*3600
end

View File

@@ -38,6 +38,7 @@ describe Redistat::Model do
ModelHelper2.store_event.should == true
ModelHelper2.hashed_label.should == true
ModelHelper2.scope.should be_nil
ModelHelper2.expire.should be_nil
ModelHelper1.depth.should == nil
ModelHelper1.store_event.should == nil
@@ -57,6 +58,7 @@ describe Redistat::Model do
ModelHelper4.scope.should == "FancyHelper"
ModelHelper4.send(:name).should == "FancyHelper"
ModelHelper4.expire.should == {:hour => 24*3600}
end
it "should store and fetch stats" do

View File

@@ -10,6 +10,7 @@ describe Redistat::Summary do
@date = Time.now
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
@stats = {"views" => 3, "visitors" => 2}
@expire = {:hour => 24*3600}
end
it "should update a single summary properly" do
@@ -32,6 +33,20 @@ describe Redistat::Summary do
summary["visitors"].should == "1"
end
it "should set key expiry properly" do
Redistat::Summary.update_all(@key, @stats, :hour,{:expire => @expire})
((24*3600)-1..(24*3600)+1).should include(db.ttl(@key.to_s(:hour)))
[:day, :month, :year].each do |depth|
db.ttl(@key.to_s(depth)).should == -1
end
db.flushdb
Redistat::Summary.update_all(@key, @stats, :hour, {:expire => {}})
[:hour, :day, :month, :year].each do |depth|
db.ttl(@key.to_s(depth)).should == -1
end
end
it "should update all summaries properly" do
Redistat::Summary.update_all(@key, @stats, :sec)
[:year, :month, :day, :hour, :min, :sec, :usec].each do |depth|