FIX: Fetch stats if it has not been cached.

This commit is contained in:
Guo Xiang Tan
2016-04-21 14:45:16 +08:00
parent e7d1fa8120
commit b4e0c5afe0
7 changed files with 53 additions and 38 deletions

View File

@@ -1,10 +1,16 @@
require 'rails_helper'
describe Jobs::AboutStats do
after do
$redis.flushall
end
it 'caches the stats' do
stats = { "visited" => 10 }
About.any_instance.expects(:stats).returns(stats)
$redis.expects(:setex).with(About.stats_cache_key, 35.minutes, stats.to_json)
stats = About.fetch_stats.to_json
cache_key = About.stats_cache_key
expect($redis.get(cache_key)).to eq(nil)
expect(described_class.new.execute({})).to eq(stats)
expect($redis.get(cache_key)).to eq(stats)
end
end

View File

@@ -1,10 +1,18 @@
require 'rails_helper'
describe Jobs::DashboardStats do
after do
$redis.flushall
end
it 'caches the stats' do
json = { "visited" => 10 }
AdminDashboardData.any_instance.expects(:as_json).returns(json)
$redis.expects(:setex).with(AdminDashboardData.stats_cache_key, 35.minutes, json.to_json)
expect(described_class.new.execute({})).to eq(json)
Timecop.freeze do
stats = AdminDashboardData.fetch_stats.to_json
cache_key = AdminDashboardData.stats_cache_key
expect($redis.get(cache_key)).to eq(nil)
expect(described_class.new.execute({})).to eq(stats)
expect($redis.get(cache_key)).to eq(stats)
end
end
end