Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Jul 15, 2024
1 parent 1cc5070 commit 13a8123
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 13 deletions.
23 changes: 23 additions & 0 deletions spec/non_rails/uber_task/report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

describe UberTask do
describe '.report' do
it 'reports from task' do
reported = false
on_report_called = false

UberTask.run do
UberTask.on_report do
on_report_called = true
end

UberTask.report do
reported = true
end
end

expect(reported).to be(true)
expect(on_report_called).to be(true)
end
end
end
44 changes: 44 additions & 0 deletions spec/non_rails/uber_task/retry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

describe UberTask do
describe '.retry' do
shared_examples 'retries the task' do
it 'retries the task' do
run_count = 0
on_retry_count = 0

UberTask.run(retry_count: 3) do
UberTask.on_retry do
on_retry_count += 1
end

if run_count < 2
run_count += 1
subject
end
end

expect(run_count).to be(2)
expect(on_retry_count).to be(2)
end
end

context 'with block' do
subject { described_class.retry { 'retry reason' } }

it_behaves_like 'retries the task'
end

context 'without block' do
subject { described_class.retry }

it_behaves_like 'retries the task'
end

context 'with wait' do
subject { described_class.retry(wait: 1) }

it_behaves_like 'retries the task'
end
end
end
31 changes: 24 additions & 7 deletions spec/non_rails/uber_task/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

describe UberTask do
describe '.run' do
it 'retries the task' do
run_count = 0
context 'when task succeeds' do
it 'it triggers .on_success hook' do
on_success_called = false

UberTask.run(retry_count: 3) do
if run_count < 2
run_count += 1
UberTask.retry(wait: 1)
UberTask.run do
UberTask.on_success do
on_success_called = true
end
end

expect(on_success_called).to be(true)
end
end

context 'when task fails' do
it 'does not trigger .on_success hook' do
on_success_called = false

expect(run_count).to be(2)
UberTask.run do
UberTask.on_success do
on_success_called = true
end

raise 'error'
end
rescue StandardError
expect(on_success_called).to be(false)
end
end
end
end
35 changes: 29 additions & 6 deletions spec/non_rails/uber_task/skip_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
# frozen_string_literal: true

describe UberTask do
it 'skips the task' do
task_skipped = true
describe '.skip' do
shared_examples 'skips the task' do
it 'skips the task' do
task_skipped = true
on_skip_called = false

UberTask.run do
UberTask.skip
task_skipped = false
UberTask.run do
UberTask.on_skip do
on_skip_called = true
end

subject

task_skipped = false
end

expect(task_skipped).to be(true)
expect(on_skip_called).to be(true)
end
end

context 'with block' do
subject { described_class.skip { 'skip reason' } }

it_behaves_like 'skips the task'
end

expect(task_skipped).to be(true)
context 'without block' do
subject { described_class.skip }

it_behaves_like 'skips the task'
end
end
end

0 comments on commit 13a8123

Please sign in to comment.