lots of changes and cleanup, among others created

a Redistat::Scope object
This commit is contained in:
2010-07-23 21:46:14 +03:00
parent 22515337f6
commit 9c9b784a8a
8 changed files with 106 additions and 41 deletions

View File

@@ -2,6 +2,10 @@ require "spec_helper"
describe Redistat do
before(:all) do
db.flushdb
end
it "should have a valid redis client instance" do
db.should_not be_nil
end

View File

@@ -2,6 +2,10 @@ require "spec_helper"
describe Redistat::Event do
before(:all) do
db.flushdb
end
before(:each) do
@scope = "PageViews"
@label = "about_us"
@@ -14,6 +18,7 @@ describe Redistat::Event do
end
it "should initialize properly" do
@event.id.should be_nil
@event.scope.should == @scope
@event.label.should == @label
@event.label_hash.should == @label_hash
@@ -40,9 +45,11 @@ describe Redistat::Event do
end
it "should increment next_id" do
Redistat::Event.next_id.should == 1
Redistat::Event.next_id.should == 2
Redistat::Event.next_id.should == 3
event = Redistat::Event.new("VisitorCount", @label, @date, @stats, @meta, @options)
@event.next_id.should == 1
event.next_id.should == 1
@event.next_id.should == 2
event.next_id.should == 2
end
it "should store event properly" do
@@ -51,13 +58,13 @@ describe Redistat::Event do
@event.save
@event.new?.should be_false
keys = db.keys "*"
keys.should include("#{Redistat::KEY_EVENT_PREFIX}#{@event.id}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT_IDS_SUFFIX}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT}#{@event.id}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT_IDS}")
end
it "should find event by id" do
@event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options.merge({:store_event => true})).save
fetched = Redistat::Event.find(@event.id)
fetched = Redistat::Event.find(@scope, @event.id)
@event.scope.should == fetched.scope
@event.label.should == fetched.label
@event.date.to_s.should == fetched.date.to_s

View File

@@ -2,24 +2,29 @@ require "spec_helper"
describe Redistat::Label do
it "should initialize and SHA1 hash the label name" do
name = "/about/us"
label = Redistat::Label.new(name)
label.name.should == name
label.hash.should == Digest::SHA1.hexdigest(name)
before(:all) do
db.flushdb
end
before(:each) do
@name = "about_us"
@label = Redistat::Label.new(@name)
end
it "should initialize properly and SHA1 hash the label name" do
@label.name.should == @name
@label.hash.should == Digest::SHA1.hexdigest(@name)
end
it "should store a label hash lookup key" do
name = "/about/us"
label = Redistat::Label.new(name)
label.save
label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS_PREFIX}#{label.hash}").should == name
@label.save
@label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
name = "/contact/us"
label = Redistat::Label.create(name)
label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS_PREFIX}#{label.hash}").should == name
@name = "contact_us"
@label = Redistat::Label.create(@name)
@label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
end
def db

30
spec/scope_spec.rb Normal file
View File

@@ -0,0 +1,30 @@
require "spec_helper"
describe Redistat::Scope do
before(:all) do
db.flushdb
end
before(:each) do
@name = "PageViews"
@scope = Redistat::Scope.new(@name)
end
it "should initialize properly" do
@scope.to_s.should == @name
end
it "should increment next_id" do
scope = Redistat::Scope.new("Visitors")
@scope.next_id.should == 1
scope.next_id.should == 1
@scope.next_id.should == 2
scope.next_id.should == 2
end
def db
Redistat.redis
end
end