Even if this class looks very simple now, we're trying a few things related to these stats. Having a class for it makes future changes easier and, if there weren't any future changes, at least it makes current experiments easier. Note we keep the method `participants_by_geozone` to return a hash because we're caching the stats and storing GeozoneStats objects would need a lot more memory and we would get an error.
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
require "rails_helper"
|
|
|
|
describe GeozoneStats do
|
|
let(:winterfell) { create(:geozone, name: "Winterfell") }
|
|
let(:riverlands) { create(:geozone, name: "Riverlands") }
|
|
|
|
describe "#name" do
|
|
let(:stats) { GeozoneStats.new(winterfell, []) }
|
|
|
|
it "returns the geozone name" do
|
|
expect(stats.name).to eq "Winterfell"
|
|
end
|
|
end
|
|
|
|
describe "#count" do
|
|
before do
|
|
2.times { create(:user, geozone: winterfell) }
|
|
1.times { create(:user, geozone: riverlands) }
|
|
end
|
|
|
|
let(:winterfell_stats) { GeozoneStats.new(winterfell, User.all) }
|
|
let(:riverlands_stats) { GeozoneStats.new(riverlands, User.all) }
|
|
|
|
it "counts participants from the geozone" do
|
|
expect(winterfell_stats.count).to eq 2
|
|
expect(riverlands_stats.count).to eq 1
|
|
end
|
|
end
|
|
|
|
describe "#percentage" do
|
|
before do
|
|
2.times { create(:user, geozone: winterfell) }
|
|
1.times { create(:user, geozone: riverlands) }
|
|
end
|
|
|
|
let(:winterfell_stats) { GeozoneStats.new(winterfell, User.all) }
|
|
let(:riverlands_stats) { GeozoneStats.new(riverlands, User.all) }
|
|
|
|
it "calculates percentage relative to the amount of participants" do
|
|
expect(winterfell_stats.percentage).to eq 66.667
|
|
expect(riverlands_stats.percentage).to eq 33.333
|
|
end
|
|
end
|
|
end
|