From 92572fef27e3ca1f731fd2120cac61b349cf9770 Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Mon, 15 Jul 2024 21:48:43 +0300 Subject: [PATCH] Improve test coverage (#22) --- spec/non_rails/uber_task/report_spec.rb | 23 +++++++++++++ spec/non_rails/uber_task/retry_spec.rb | 44 +++++++++++++++++++++++++ spec/non_rails/uber_task/run_spec.rb | 31 +++++++++++++---- spec/non_rails/uber_task/skip_spec.rb | 35 ++++++++++++++++---- 4 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 spec/non_rails/uber_task/report_spec.rb create mode 100644 spec/non_rails/uber_task/retry_spec.rb diff --git a/spec/non_rails/uber_task/report_spec.rb b/spec/non_rails/uber_task/report_spec.rb new file mode 100644 index 0000000..c49bd2a --- /dev/null +++ b/spec/non_rails/uber_task/report_spec.rb @@ -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 diff --git a/spec/non_rails/uber_task/retry_spec.rb b/spec/non_rails/uber_task/retry_spec.rb new file mode 100644 index 0000000..ee2f81d --- /dev/null +++ b/spec/non_rails/uber_task/retry_spec.rb @@ -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 diff --git a/spec/non_rails/uber_task/run_spec.rb b/spec/non_rails/uber_task/run_spec.rb index b4e7f62..d131347 100644 --- a/spec/non_rails/uber_task/run_spec.rb +++ b/spec/non_rails/uber_task/run_spec.rb @@ -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 diff --git a/spec/non_rails/uber_task/skip_spec.rb b/spec/non_rails/uber_task/skip_spec.rb index 659a36c..0dd093a 100644 --- a/spec/non_rails/uber_task/skip_spec.rb +++ b/spec/non_rails/uber_task/skip_spec.rb @@ -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