2013-02-27 22:39:42 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Report do
|
|
|
|
|
|
|
|
|
|
|
|
describe 'visits report' do
|
|
|
|
|
2013-03-07 11:07:59 -05:00
|
|
|
let(:report) { Report.find('visits', cache: false) }
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
|
|
context "no visits" do
|
|
|
|
it "returns an empty report" do
|
|
|
|
report.data.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with visits" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.user_visits.create(visited_at: 1.day.ago)
|
|
|
|
user.user_visits.create(visited_at: 2.days.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a report with data" do
|
|
|
|
report.data.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-03-07 11:07:59 -05:00
|
|
|
[:signup, :topic, :post].each do |arg|
|
|
|
|
describe "#{arg} report" do
|
|
|
|
pluralized = arg.to_s.pluralize
|
|
|
|
|
|
|
|
let(:report) { Report.find(pluralized, cache: false) }
|
|
|
|
|
|
|
|
context "no #{pluralized}" do
|
|
|
|
it 'returns an empty report' do
|
|
|
|
report.data.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with #{pluralized}" do
|
|
|
|
before do
|
|
|
|
fabricator = (arg == :signup ? :user : arg)
|
2013-03-12 11:57:33 -04:00
|
|
|
Fabricate(fabricator, created_at: 25.hours.ago)
|
|
|
|
Fabricate(fabricator, created_at: 1.hours.ago)
|
|
|
|
Fabricate(fabricator, created_at: 1.hours.ago)
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct data' do
|
|
|
|
report.data[0][:y].should == 1
|
|
|
|
report.data[1][:y].should == 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-12 11:57:33 -04:00
|
|
|
describe "total_users report" do
|
|
|
|
let(:report) { Report.find("total_users", cache: false) }
|
|
|
|
|
|
|
|
context "no total_users" do
|
|
|
|
it 'returns an empty report' do
|
|
|
|
report.data.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with users" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user, created_at: 25.hours.ago)
|
|
|
|
Fabricate(:user, created_at: 1.hours.ago)
|
|
|
|
Fabricate(:user, created_at: 1.hours.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct data' do
|
|
|
|
report.data[0][:y].should == 3
|
|
|
|
report.data[1][:y].should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-07 11:07:59 -05:00
|
|
|
describe '#fetch' do
|
|
|
|
context 'signups' do
|
|
|
|
let(:report) { Report.find('signups', cache: true) }
|
|
|
|
|
|
|
|
context 'no data' do
|
|
|
|
context 'cache miss' do
|
|
|
|
before do
|
|
|
|
$redis.expects(:exists).with('signups:data').returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cache an empty data set' do
|
|
|
|
$redis.expects(:setex).with('signups:data', Report.cache_expiry, "")
|
|
|
|
report.data.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cache hit' do
|
|
|
|
before do
|
|
|
|
$redis.expects(:exists).with('signups:data').returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the cached empty report' do
|
|
|
|
User.expects(:count_by_signup_date).never
|
|
|
|
$redis.expects(:setex).never
|
|
|
|
$redis.expects(:get).with('signups:data').returns('')
|
|
|
|
report.data.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with data' do
|
|
|
|
before do
|
2013-03-12 11:57:33 -04:00
|
|
|
Fabricate(:user, created_at: 25.hours.ago)
|
|
|
|
Fabricate(:user, created_at: 1.hour.ago)
|
|
|
|
Fabricate(:user, created_at: 1.hour.ago)
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'cache miss' do
|
|
|
|
before do
|
|
|
|
$redis.expects(:exists).with('signups:data').returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cache the data set' do
|
|
|
|
$redis.expects(:setex).with do |key, expiry, string|
|
2013-03-11 10:40:13 -04:00
|
|
|
string =~ /(\d)+-(\d)+-(\d)+,1/ and string =~ /(\d)+-(\d)+-(\d)+,2/
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
report()
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return correct data' do
|
|
|
|
report.data[0][:y].should == 1
|
|
|
|
report.data[1][:y].should == 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cache hit' do
|
|
|
|
before do
|
|
|
|
$redis.expects(:exists).with('signups:data').returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the cached data' do
|
|
|
|
User.expects(:count_by_signup_date).never
|
|
|
|
$redis.expects(:setex).never
|
|
|
|
$redis.expects(:get).with('signups:data').returns("#{2.days.ago.to_date.to_s},1|#{1.day.ago.to_date.to_s},2")
|
|
|
|
report.data[0][:y].should == 1
|
|
|
|
report.data[1][:y].should == 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
|
|
end
|