Commit 6cafb687 authored by Ruben Davila's avatar Ruben Davila

Avoid the total time spent to have a negative value.

parent 09b02e66
......@@ -15,7 +15,7 @@ module TimeTrackable
end
def spend_time=(seconds)
return unless seconds
return if invalid_time_spent?(seconds)
new_time_spent = seconds.zero? ? -(total_time_spent) : seconds
timelogs.new(time_spent: new_time_spent)
......@@ -26,4 +26,14 @@ module TimeTrackable
def total_time_spent
timelogs.sum(:time_spent)
end
private
def invalid_time_spent?(seconds)
return true unless seconds
# time to subtract exceeds the total time spent
return true if seconds < 0 && (seconds.abs > total_time_spent)
false
end
end
......@@ -384,4 +384,36 @@ describe Issue, "Issuable" do
expect(issue.assignee_or_author?(user)).to eq(false)
end
end
describe '#spend_time' do
let(:issue) { create(:issue) }
context 'adding time' do
it 'should update the total time spent' do
issue.update_attributes!(spend_time: 1800)
expect(issue.total_time_spent).to eq(1800)
end
end
context 'substracting time' do
before do
issue.update_attributes!(spend_time: 1800)
end
it 'should update the total time spent' do
issue.update_attributes!(spend_time: -900)
expect(issue.total_time_spent).to eq(900)
end
context 'when time to substract exceeds the total time spent' do
it 'should not alter the total time spent' do
issue.update_attributes!(spend_time: -3600)
expect(issue.total_time_spent).to eq(1800)
end
end
end
end
end
......@@ -226,6 +226,14 @@ describe SlashCommands::InterpretService, services: true do
end
end
shared_examples 'spend command with negative time' do
it 'populates spend_time: "-1800" if content contains /spend -30m' do
_, updates = service.execute(content, issuable)
expect(updates).to eq(spend_time: -1800)
end
end
shared_examples 'remove_estimation command' do
it 'populates time_estimate: "0" if content contains /remove_estimation' do
_, updates = service.execute(content, issuable)
......@@ -503,6 +511,11 @@ describe SlashCommands::InterpretService, services: true do
let(:issuable) { issue }
end
it_behaves_like 'spend command with negative time' do
let(:content) { '/spend -30m' }
let(:issuable) { issue }
end
it_behaves_like 'empty command' do
let(:content) { '/spend' }
let(:issuable) { issue }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment