spec/finder_spec.rb in redistat-0.1.1 vs spec/finder_spec.rb in redistat-0.2.0
- old
+ new
@@ -7,46 +7,48 @@
db.flushdb
@scope = "PageViews"
@label = "about_us"
@date = Time.now
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
- @stats = {"views" => 3, "visitors" => 2}
+ @stats = {"views" => 3, "visitors" => 2}
+ @two_hours_ago = 2.hours.ago
+ @one_hour_ago = 1.hour.ago
end
it "should initialize properly" do
- two_hours_ago = 2.hours.ago
- one_hour_ago = 1.hour.ago
- options = {:scope => "PageViews", :label => "Label", :from => two_hours_ago, :till => one_hour_ago, :depth => :hour, :interval => :hour}
+ options = {:scope => "PageViews", :label => "Label", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
- finder = Redistat::Finder.new(options)
- finder.options.should == options
-
finder = Redistat::Finder.new
finder.send(:set_options, options)
- finder.options.should == options
-
- finder = Redistat::Finder.dates(two_hours_ago, one_hour_ago).scope("PageViews").label("Label").depth(:hour).interval(:hour)
- finder.options.should == options
-
- finder = Redistat::Finder.scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour)
- finder.options.should == options
+ finder.options[:scope].should be_a(Redistat::Scope)
+ finder.options[:scope].to_s.should == options[:scope]
+ finder.options[:label].should be_a(Redistat::Label)
+ finder.options[:label].to_s.should == options[:label]
+ finder.options.should == options.merge(:scope => finder.options[:scope], :label => finder.options[:label])
- finder = Redistat::Finder.label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews")
- finder.options.should == options
+ finder = Redistat::Finder.dates(@two_hours_ago, @one_hour_ago)
+ finder.options[:from].should == @two_hours_ago
+ finder.options[:till].should == @one_hour_ago
- finder = Redistat::Finder.from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews").label("Label")
- finder.options.should == options
+ finder = Redistat::Finder.scope("hello")
+ finder.options[:scope].to_s.should == "hello"
- finder = Redistat::Finder.till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews").label("Label").from(two_hours_ago)
- finder.options.should == options
+ finder = Redistat::Finder.label("hello")
+ finder.options[:label].to_s.should == "hello"
- finder = Redistat::Finder.depth(:hour).interval(:hour).scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago)
- finder.options.should == options
+ finder = Redistat::Finder.from(@two_hours_ago)
+ finder.options[:from].should == @two_hours_ago
- finder = Redistat::Finder.interval(:hour).scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour)
- finder.options.should == options
+ finder = Redistat::Finder.till(@one_hour_ago)
+ finder.options[:till].should == @one_hour_ago
+ finder = Redistat::Finder.depth(:hour)
+ finder.options[:depth].should == :hour
+
+ finder = Redistat::Finder.interval(:hour)
+ finder.options[:interval].should == :hour
+
end
it "should fetch stats properly" do
first_stat, last_stat = create_example_stats
@@ -87,10 +89,25 @@
it "should throw error on invalid options" do
lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions)
end
+ it "should find children" do
+ Redistat::Key.new("PageViews", "message/public/die").update_index
+ Redistat::Key.new("PageViews", "message/public/live").update_index
+ Redistat::Key.new("PageViews", "message/public/fester").update_index
+ members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}message/public") # checking 'message/public'
+ options = {:scope => "PageViews", :label => "message/public", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
+ finder = Redistat::Finder.new(options)
+ finder.children.first.should be_a(Redistat::Finder)
+ subs = finder.children.map { |f| f.options[:label].me }
+ subs.should have(3).items
+ subs.should include('die')
+ subs.should include('live')
+ subs.should include('fester')
+ end
+
describe "Lazy-Loading" do
before(:each) do
@first_stat, @last_stat = create_example_stats
@@ -101,11 +118,10 @@
{"visitors"=>"2", "views"=>"3"},
{"visitors"=>"2", "views"=>"3"}, {}]
end
it "should lazy-load" do
-
@finder.instance_variable_get("@result").should be_nil
stats = @finder.all
@finder.instance_variable_get("@result").should_not be_nil
stats.should == @finder.find # find method directly fetches results
@@ -144,10 +160,10 @@
@finder.each_with_index { |r, i| res[i] = r }
@match.each_with_index { |r, i| match[i] = r }
res.should == match
end
- end
+ end # "Lazy-Loading"
# helper methods
def create_example_stats