Skip to content

Instantly share code, notes, and snippets.

@cutalion
Created September 7, 2015 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cutalion/30fc4788d637a8c1892a to your computer and use it in GitHub Desktop.
Save cutalion/30fc4788d637a8c1892a to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe UserReport do
# Jun | Aug | Sep
#-----|--|--|--|--|--|--|--> time
# * | * * |
#
# Short periods - weeks
# Stars - timelogs
describe "#to_h" do
let(:user) { create :user }
let(:jul30) { Time.parse('2015-07-30 10:00') }
let(:aug6) { Time.parse('2015-08-06 10:00') }
let(:aug11) { Time.parse('2015-08-11 10:00') }
before do
project = create :project, title: 'Destroy Death Star'
Timecop.travel jul30 do
create :timelog, user: user, hours: 1, project: project
end
Timecop.travel aug6 do
create :timelog, user: user, hours: 2, project: project
end
Timecop.travel aug11 do
create :timelog, user: user, hours: 3, project: project
end
end
around do |ex|
Timecop.travel Time.parse('2015-08-11 10:00') do
ex.run
end
end
it "should calculate day totals for current week and month" do
report = UserReport.new(user, aug11.monday, aug11.sunday).to_h
expect(report).to eq(
{
:by_day => {
"Mon"=> 0,
"Tue"=> 3,
"Wed"=> 0,
"Thu"=> 0,
"Fri"=> 0,
"Sat"=> 0,
"Sun"=> 0,
},
:by_project =>[
["Destroy Death Star", { :month => 5, :week => 3 }]
],
:tasks=>[
{:project=>"Destroy Death Star", :hours => 3, :description=>"Hard working" }
],
:total_week=>3.0,
:total_month=>5.0
}
)
end
end
describe "#to_h" do
let(:user) { create :user }
let(:aug31) { Time.parse('2015-08-31 10:00') }
let(:sep01) { Time.parse('2015-09-01 10:00') }
before do
projectA = create :project, title: 'A'
projectB = create :project, title: 'B'
Timecop.travel aug31 do
create :timelog, user: user, hours: 1, project: projectA
end
Timecop.travel sep01 do
create :timelog, user: user, hours: 2, project: projectB
end
end
around do |ex|
Timecop.travel Time.parse('2015-09-06 10:00') do
ex.run
end
end
it "should calculate day totals for current week and month" do
report = UserReport.new(user, sep01.monday, sep01.sunday).to_h
expect(report).to eq(
{
:by_day => {
"Mon"=> 1,
"Tue"=> 2,
"Wed"=> 0,
"Thu"=> 0,
"Fri"=> 0,
"Sat"=> 0,
"Sun"=> 0,
},
:by_project =>[
["A", { :month => 0, :week => 1 }],
["B", { :month => 2, :week => 2 }],
],
:tasks=>[
{:project=>"A", :hours => 3, :description=>"Hard working" }
],
:total_week=>3.0,
:total_month=>2.0
}
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment