test/dirmon_entry_test.rb in rocketjob-1.1.2 vs test/dirmon_entry_test.rb in rocketjob-1.1.3

- old
+ new

@@ -1,83 +1,85 @@ require_relative 'test_helper' require_relative 'jobs/test_job' # Unit Test for RocketJob::Job class DirmonEntryTest < Minitest::Test - context RocketJob::DirmonEntry do - context '.config' do - should 'support multiple databases' do + describe RocketJob::DirmonEntry do + describe '.config' do + it 'support multiple databases' do assert_equal 'test_rocketjob', RocketJob::DirmonEntry.collection.db.name end end - context '#job_class' do - context 'with a nil job_class_name' do - should 'return nil' do + describe '#job_class' do + describe 'with a nil job_class_name' do + it 'return nil' do entry = RocketJob::DirmonEntry.new assert_equal(nil, entry.job_class) end end - context 'with an unknown job_class_name' do - should 'return nil' do + describe 'with an unknown job_class_name' do + it 'return nil' do entry = RocketJob::DirmonEntry.new(job_class_name: 'FakeJobThatDoesNotExistAnyWhereIPromise') assert_equal(nil, entry.job_class) end end - context 'with a valid job_class_name' do - should 'return job class' do + describe 'with a valid job_class_name' do + it 'return job class' do entry = RocketJob::DirmonEntry.new(job_class_name: 'RocketJob::Job') assert_equal(RocketJob::Job, entry.job_class) + assert_equal 0, entry.arguments.size + assert_equal 0, entry.properties.size end end end - context '.whitelist_paths' do - should 'default to []' do + describe '.whitelist_paths' do + it 'default to []' do assert_equal [], RocketJob::DirmonEntry.whitelist_paths end end - context '.add_whitelist_path' do - teardown do + describe '.add_whitelist_path' do + after do RocketJob::DirmonEntry.whitelist_paths.each { |path| RocketJob::DirmonEntry.delete_whitelist_path(path) } end - should 'convert relative path to an absolute one' do + it 'convert relative path to an absolute one' do path = Pathname('test/jobs').realpath.to_s assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/jobs') assert_equal [path], RocketJob::DirmonEntry.whitelist_paths end - should 'prevent duplicates' do + it 'prevent duplicates' do path = Pathname('test/jobs').realpath.to_s assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/jobs') assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/jobs') assert_equal path, RocketJob::DirmonEntry.add_whitelist_path(path) assert_equal [path], RocketJob::DirmonEntry.whitelist_paths end end - context '#fail_with_exception!' do - setup do + describe '#fail_with_exception!' do + before do @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: '/abc/**', arguments: [1]) @dirmon_entry.enable! end - teardown do + after do @dirmon_entry.destroy if @dirmon_entry && @dirmon_entry.new_record? end - should 'fail with message' do + it 'fail with message' do @dirmon_entry.fail_with_exception!('myworker:2323', 'oh no') assert_equal true, @dirmon_entry.failed? assert_equal 'RocketJob::DirmonEntryException', @dirmon_entry.exception.class_name assert_equal 'oh no', @dirmon_entry.exception.message end - should 'fail with exception' do + it 'fail with exception' do exception = nil begin blah rescue Exception => exc exception = exc @@ -88,97 +90,97 @@ assert_equal exception.class.name.to_s, @dirmon_entry.exception.class_name assert @dirmon_entry.exception.message.include?('undefined local variable or method'), @dirmon_entry.attributes.inspect end end - context '#validate' do - should 'existance' do + describe '#validate' do + it 'existance' do assert entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob') assert_equal false, entry.valid? assert_equal ["can't be blank"], entry.errors[:pattern], entry.errors.inspect end - context 'perform_method' do - context 'with an invalid method' do - should 'add errors to the entry' do + describe 'perform_method' do + describe 'with an invalid method' do + it 'add errors to the entry' do entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', perform_method: :missing_perform_method) assert_equal false, entry.valid? assert_equal ['Method not implemented by Jobs::TestJob'], entry.errors[:perform_method], entry.errors.inspect end end end - context 'job_class_name' do - should 'ensure presence' do + describe 'job_class_name' do + it 'ensure presence' do assert entry = RocketJob::DirmonEntry.new(pattern: '/abc/**') assert_equal false, entry.valid? assert_equal ["can't be blank", 'job_class_name must be defined and must be derived from RocketJob::Job'], entry.errors[:job_class_name], entry.errors.inspect end end - context 'arguments' do - should 'allow no arguments' do + describe 'arguments' do + it 'allow no arguments' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', pattern: '/abc/**', perform_method: :result ) assert_equal true, entry.valid?, entry.errors.inspect assert_equal [], entry.errors[:arguments], entry.errors.inspect end - should 'ensure correct number of arguments' do + it 'ensure correct number of arguments' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', pattern: '/abc/**' ) assert_equal false, entry.valid? assert_equal ['There must be 1 argument(s)'], entry.errors[:arguments], entry.errors.inspect end - should 'return false if the job name is bad' do + it 'return false if the job name is bad' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::Tests::Names::Things', pattern: '/abc/**' ) assert_equal false, entry.valid? assert_equal [], entry.errors[:arguments], entry.errors.inspect end end - should 'arguments with perform_method' do + it 'arguments with perform_method' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', pattern: '/abc/**', perform_method: :sum ) assert_equal false, entry.valid? assert_equal ['There must be 2 argument(s)'], entry.errors[:arguments], entry.errors.inspect end - should 'valid' do + it 'valid' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', pattern: '/abc/**', arguments: [1] ) assert entry.valid?, entry.errors.inspect end - should 'valid with perform_method' do + it 'valid with perform_method' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', pattern: '/abc/**', perform_method: :sum, arguments: [1, 2] ) assert entry.valid?, entry.errors.inspect end end - context 'with valid entry' do - setup do + describe 'with valid entry' do + before do @archive_directory = '/tmp/archive_directory' @entry = RocketJob::DirmonEntry.new( pattern: 'abc/*', job_class_name: 'Jobs::TestJob', arguments: [{input: 'yes'}], @@ -192,58 +194,58 @@ File.open(@file_name, 'w') { |file| file.write('Hello World') } assert File.exists?(@file_name) @archive_file_name = File.join(@archive_directory, "#{@job.id}_#{File.basename(@file_name)}") end - teardown do + after do @file.delete if @file end - context '#archive_pathname' do - should 'with archive directory' do + describe '#archive_pathname' do + it 'with archive directory' do assert_equal @archive_directory.to_s, @entry.archive_pathname.to_s end - should 'without archive directory' do + it 'without archive directory' do @entry.archive_directory = nil assert_equal '_archive', @entry.archive_pathname.to_s end end - context '#archive_file' do - should 'archive file' do + describe '#archive_file' do + it 'archive file' do assert_equal @archive_file_name, @entry.send(:archive_file, @job, Pathname.new(@file_name)) assert File.exists?(@archive_file_name), @archive_file_name end end - context '#upload_default' do - should 'upload' do + describe '#upload_default' do + it 'upload' do @entry.send(:upload_default, @job, @pathname) assert_equal File.absolute_path(@archive_file_name), @job.arguments.first[:full_file_name], @job.arguments end end - context '#upload_file' do - should 'upload using #file_store_upload' do + describe '#upload_file' do + it 'upload using #file_store_upload' do @job.define_singleton_method(:file_store_upload) do |file_name| self.description = "FILE:#{file_name}" end @entry.send(:upload_file, @job, @pathname) assert_equal "FILE:#{@file_name}", @job.description end - should 'upload using #upload' do + it 'upload using #upload' do @job.define_singleton_method(:upload) do |file_name| self.description = "FILE:#{file_name}" end @entry.send(:upload_file, @job, @pathname) assert_equal "FILE:#{@file_name}", @job.description end end - context '#later' do - should 'enqueue job' do + describe '#later' do + it 'enqueue job' do @entry.arguments = [{}] @entry.perform_method = :event job = @entry.later(@pathname) assert_equal File.join(@archive_directory, "#{job.id}_#{File.basename(@file_name)}"), job.arguments.first[:full_file_name] assert job.queued?