From 452dee9e7fc10d6e5d4c7cc9e88f07a35dd5ee70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20K=C5=ABjalis?= Date: Thu, 25 Jul 2024 09:49:12 +0300 Subject: [PATCH] Use rspec for testing --- .gitignore | 2 + .rspec | 3 + Gemfile | 1 + Gemfile.lock | 15 ++ Rakefile | 13 +- spec/spec_helper.rb | 16 +++ test/chained_job/clean_up_queue_test.rb | 79 ----------- test/chained_job/config_test.rb | 57 -------- test/chained_job/helpers_test.rb | 25 ---- test/chained_job/process_test.rb | 141 ------------------- test/chained_job/start_chains_test.rb | 55 -------- test/chained_job/store_job_arguments_test.rb | 67 --------- test/chained_job_test.rb | 10 -- 13 files changed, 44 insertions(+), 440 deletions(-) create mode 100644 .rspec create mode 100644 spec/spec_helper.rb delete mode 100644 test/chained_job/clean_up_queue_test.rb delete mode 100644 test/chained_job/config_test.rb delete mode 100644 test/chained_job/helpers_test.rb delete mode 100644 test/chained_job/process_test.rb delete mode 100644 test/chained_job/start_chains_test.rb delete mode 100644 test/chained_job/store_job_arguments_test.rb delete mode 100644 test/chained_job_test.rb diff --git a/.gitignore b/.gitignore index cace5e8..7b3e813 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /pkg/ + +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index aba2a3c..5be4d28 100644 --- a/Gemfile +++ b/Gemfile @@ -6,4 +6,5 @@ gemspec group :test do gem 'mock_redis' + gem 'rspec' end diff --git a/Gemfile.lock b/Gemfile.lock index b4a5aec..c39521c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,10 +6,24 @@ PATH GEM remote: https://rubygems.org/ specs: + diff-lcs (1.5.1) minitest (5.15.0) mock_redis (0.30.0) ruby2_keywords rake (12.3.3) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) ruby2_keywords (0.0.5) PLATFORMS @@ -21,6 +35,7 @@ DEPENDENCIES minitest (~> 5.0) mock_redis rake (~> 12.0) + rspec BUNDLED WITH 2.2.33 diff --git a/Rakefile b/Rakefile index be37595..f06948d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,15 @@ # frozen_string_literal: true require 'bundler/gem_tasks' -require 'rake/testtask' +require 'rspec/core/rake_task' -Rake::TestTask.new(:test) do |task| - task.test_files = FileList['test/**/*_test.rb'] - task.libs += %w(test lib) -end +RSpec::Core::RakeTask.new(:spec) + +require 'rubocop/rake_task' + +RuboCop::RakeTask.new -task default: :test +task default: %i[spec rubocop] task :version do |t| puts ChainedJob::VERSION diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..9d043ff --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'chained_job' + +RSpec.configure do |config| + config.example_status_persistence_file_path = '.rspec_status' + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end + + config.mock_with :rspec do |c| + c.allow_message_expectations_on_nil = true + end +end diff --git a/test/chained_job/clean_up_queue_test.rb b/test/chained_job/clean_up_queue_test.rb deleted file mode 100644 index fee1b87..0000000 --- a/test/chained_job/clean_up_queue_test.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require 'minitest/autorun' - -class ChainedJob::CleanUpQueueTest < Minitest::Test - ARRAY_OF_JOB_ARGUMENTS = %w(1 2 3).freeze - - # rubocop:disable Metrics/AbcSize - def test_queue_clean_up - assert_equal(redis.lrange(redis_key(job_tag_1), 0, -1), ARRAY_OF_JOB_ARGUMENTS) - assert_equal(redis.lrange(redis_key(job_tag_2), 0, -1), ARRAY_OF_JOB_ARGUMENTS) - - tested_class.run(job_class) - - assert_equal(redis.lrange(redis_key(job_tag_1), 0, -1), []) - assert_equal(redis.lrange(redis_key(job_tag_2), 0, -1), []) - end - # rubocop:enable Metrics/AbcSize - - private - - def tested_class - ChainedJob::CleanUpQueue - end - - def job_class - 'DummyJob' - end - - def redis_key(job_tag) - "chained_job:#{job_class}:#{job_tag}" - end - - def tag_list - "#{job_key}:tags" - end - - def job_key - "chained_job:#{job_class}" - end - - def job_tag_1 - 'Tag_One' - end - - def job_tag_2 - 'Tag_Two' - end - - def setup - setup_redis - setup_tag_list - setup_arguments_queue - end - - def setup_redis - ChainedJob.configure do |config| - config.redis = redis - end - end - - def redis - @redis ||= MockRedis.new - end - - def setup_tag_list - redis.sadd(tag_list, job_tag_1) - redis.sadd(tag_list, job_tag_2) - end - - def setup_arguments_queue - redis.rpush(redis_key(job_tag_1), ARRAY_OF_JOB_ARGUMENTS) - redis.rpush(redis_key(job_tag_2), ARRAY_OF_JOB_ARGUMENTS) - end - - def teardown - ChainedJob.instance_variable_set(:@config, nil) - end -end diff --git a/test/chained_job/config_test.rb b/test/chained_job/config_test.rb deleted file mode 100644 index 42e4892..0000000 --- a/test/chained_job/config_test.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -require 'chained_job/config' -require 'mock_redis' -require 'minitest/autorun' - -class ChainedJob::ConfigTest < Minitest::Test - def test_default_debug_value - assert_equal true, default_config.debug - end - - def test_default_arguments_batch_size - assert_equal 1_000, default_config.arguments_batch_size - end - - def test_default_arguments_queue_expiration - assert_equal 604_800, default_config.arguments_queue_expiration - end - - def test_default_redis_configuration - assert_raises(ChainedJob::ConfigurationError, 'Redis is not configured') do - ChainedJob.redis - end - end - - def test_default_logger - assert_kind_of ::Logger, default_config.logger - end - - def test_configure - ChainedJob.configure do |config| - config.debug = false - config.arguments_batch_size = 2_000 - config.arguments_queue_expiration = 1_209_600 - config.redis = redis_config - end - - assert_equal false, ChainedJob.config.debug - assert_equal 2_000, ChainedJob.config.arguments_batch_size - assert_equal 1_209_600, ChainedJob.config.arguments_queue_expiration - assert_equal redis_config, ChainedJob.redis - end - - private - - def setup - ChainedJob.instance_variable_set(:@config, nil) - end - - def default_config - @default_config ||= ChainedJob::Config.new - end - - def redis_config - @redis_config ||= MockRedis.new - end -end diff --git a/test/chained_job/helpers_test.rb b/test/chained_job/helpers_test.rb deleted file mode 100644 index 8ba6b8d..0000000 --- a/test/chained_job/helpers_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require 'chained_job/helpers' -require 'minitest/autorun' - -class ChainedJob::HelpersTest < Minitest::Test - def test_redis_key_fetching - job_key = 'chained_job:DummyJob' - job_tag = '1595252432.198516' - - assert_equal "#{job_key}:#{job_tag}", ChainedJob::Helpers.redis_key(job_key, job_tag) - end - - def test_job_key_fetching - job_class = 'DummyJob' - - assert_equal "chained_job:#{job_class}", ChainedJob::Helpers.job_key(job_class) - end - - def test_tag_list_fetching - prefix = 'DummyJob' - - assert_equal "#{prefix}:tags", ChainedJob::Helpers.tag_list(prefix) - end -end diff --git a/test/chained_job/process_test.rb b/test/chained_job/process_test.rb deleted file mode 100644 index cbdc9c9..0000000 --- a/test/chained_job/process_test.rb +++ /dev/null @@ -1,141 +0,0 @@ -# frozen_string_literal: true - -require 'chained_job' -require 'mock_redis' -require 'minitest/autorun' - -class ChainedJob::ProcessTest < Minitest::Test - DEFAULT_JOB_TAG = '1595253473.6297688' - - # rubocop:disable Metrics/AbcSize - def test_process - redis.rpush(redis_key, [Marshal.dump(1)]) - - job_instance.expect(:class, job_class, []) - job_instance.expect(:class, job_class, []) - job_instance.expect(:class, job_class, []) - job_instance.expect(:process, nil, [1]) - job_class.expect(:perform_later, nil, [{}, 1, DEFAULT_JOB_TAG]) - - tested_class.run({}, job_instance, job_instance.class, 1, DEFAULT_JOB_TAG) - - job_instance.verify - end - - def test_empty_arguments_queue - job_instance.expect(:class, job_class, []) - job_instance.expect(:class, job_class, []) - job_instance.expect(:class, job_class, []) - tested_class.run({}, job_instance, job_instance.class, 1, DEFAULT_JOB_TAG) - - job_instance.verify - end - - def test_handle_retry_with_single_args - array_of_arguments = [10] - - redis.rpush(redis_key, ChainedJob::Helpers.serialize(array_of_arguments)) - - job_instance.expect(:class, job_class.to_s, []) - job_instance.expect(:class, job_class.to_s, []) - - job_instance.expect(:process, 100) do - raise(RuntimeError, 'Service temporary timeout error') - end - - job_instance.expect(:try, true, [:handle_retry?]) - - assert_raises RuntimeError do - tested_class.run({}, job_instance, job_instance.class, 1, DEFAULT_JOB_TAG) - end - - job_instance.verify - - # arguments were set back to redis - args_redis = redis.lrange(redis_key, 0, -1).map { |e| Marshal.load(e) } - assert_equal(args_redis, array_of_arguments.reverse) - end - - def test_handle_retry_with_multiple_args - array_of_arguments = [101, 102] - - redis.rpush(redis_key, ChainedJob::Helpers.serialize(array_of_arguments)) - - job_instance.expect(:class, job_class.to_s, []) - job_instance.expect(:class, job_class.to_s, []) - - job_instance.expect(:process, 100) do - raise(RuntimeError, 'Service temporary timeout error') - end - - job_instance.expect(:try, true, [:handle_retry?]) - - assert_raises RuntimeError do - tested_class.run({}, job_instance, job_instance.class, 1, DEFAULT_JOB_TAG) - end - - job_instance.verify - - # arguments were set back to redis - args_redis = redis.lrange(redis_key, 0, -1).map { |e| Marshal.load(e) } - assert_equal(args_redis, array_of_arguments.reverse) - end - - def test_handle_retry_with_args_of_array - array_of_arguments = [[1, 2], [3, 4]] - - redis.rpush(redis_key, ChainedJob::Helpers.serialize(array_of_arguments)) - - job_instance.expect(:class, job_class.to_s, []) - job_instance.expect(:class, job_class.to_s, []) - - job_instance.expect(:process, [1, 2]) do - raise(RuntimeError, 'Service temporary timeout error') - end - - job_instance.expect(:try, true, [:handle_retry?]) - - assert_raises RuntimeError do - tested_class.run({}, job_instance, job_instance.class, 1, DEFAULT_JOB_TAG) - end - - job_instance.verify - - # arguments were set back to redis - args_redis = redis.lrange(redis_key, 0, -1).map { |e| Marshal.load(e) } - assert_equal(args_redis, array_of_arguments.reverse) - end - # rubocop:enable Metrics/AbcSize - - private - - def setup - ChainedJob.configure do |config| - config.redis = redis - end - end - - def teardown - ChainedJob.instance_variable_set(:@config, nil) - end - - def job_instance - @job_instance ||= MiniTest::Mock.new - end - - def redis - @redis ||= MockRedis.new - end - - def redis_key - "chained_job:#{job_class}:#{DEFAULT_JOB_TAG}" - end - - def job_class - @job_class ||= MiniTest::Mock.new - end - - def tested_class - ChainedJob::Process - end -end diff --git a/test/chained_job/start_chains_test.rb b/test/chained_job/start_chains_test.rb deleted file mode 100644 index 2a3673a..0000000 --- a/test/chained_job/start_chains_test.rb +++ /dev/null @@ -1,55 +0,0 @@ -# frozen_string_literal: true - -require 'mock_redis' -require 'minitest/autorun' - -class ChainedJob::StartChainsTest < Minitest::Test - ARRAY_OF_JOB_ARGUMENTS = %w(1 2 3).freeze - - def test_start_chains - job_tag = current_time.to_f.to_s - - with_frozen_time(current_time) do - job_class.expect(:perform_later, nil, [{}, 0, job_tag]) - job_class.expect(:perform_later, nil, [{}, 1, job_tag]) - - tested_class.run({}, job_class, job_class, ARRAY_OF_JOB_ARGUMENTS, 2) - - job_class.verify - end - end - - def test_empty_array_of_job_arguments - with_frozen_time(current_time) do - tested_class.run({}, job_class, job_class, [], 1) - end - end - - private - - def with_frozen_time(time) - Time.stub(:now, time) { yield } - end - - def current_time - @current_time ||= Time.now - end - - def setup - ChainedJob.configure do |config| - config.redis = MockRedis.new - end - end - - def teardown - ChainedJob.instance_variable_set(:@config, nil) - end - - def job_class - @job_class ||= MiniTest::Mock.new - end - - def tested_class - ChainedJob::StartChains - end -end diff --git a/test/chained_job/store_job_arguments_test.rb b/test/chained_job/store_job_arguments_test.rb deleted file mode 100644 index b88e7e6..0000000 --- a/test/chained_job/store_job_arguments_test.rb +++ /dev/null @@ -1,67 +0,0 @@ -# frozen_string_literal: true - -require 'minitest/autorun' - -class ChainedJob::StoreJobArgumentsTest < Minitest::Test - ARRAY_OF_JOB_ARGUMENTS = %w(1 2 3).freeze - - def test_redis_store - tested_class.run(job_class, job_tag, ARRAY_OF_JOB_ARGUMENTS) - - assert_equal(redis.lrange(redis_key, 0, -1), stored_job_arguments) - end - - def test_set_tag_list - tag_list = "chained_job:#{job_class}:tags" - - tested_class.run(job_class, job_tag, ARRAY_OF_JOB_ARGUMENTS) - - assert_equal(redis.spop(tag_list), job_tag) - end - - def test_key_expiration - tested_class.run(job_class, job_tag, ARRAY_OF_JOB_ARGUMENTS) - - assert_equal(redis.ttl(redis_key), ChainedJob.config.arguments_queue_expiration) - end - - private - - def tested_class - ChainedJob::StoreJobArguments - end - - def job_class - 'DummyJob' - end - - def job_tag - current_time.to_f.to_s - end - - def stored_job_arguments - ARRAY_OF_JOB_ARGUMENTS.map { |argument| Marshal.dump(argument) } - end - - def redis_key - "chained_job:#{job_class}:#{job_tag}" - end - - def current_time - @current_time ||= Time.now - end - - def setup - ChainedJob.configure do |config| - config.redis = redis - end - end - - def redis - @redis ||= MockRedis.new - end - - def teardown - ChainedJob.instance_variable_set(:@config, nil) - end -end diff --git a/test/chained_job_test.rb b/test/chained_job_test.rb deleted file mode 100644 index deeb2ba..0000000 --- a/test/chained_job_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -require 'chained_job' -require 'minitest/autorun' - -class ChainedJobTest < Minitest::Test - def test_that_it_has_a_version_number - refute_nil ::ChainedJob::VERSION - end -end