test/dirmon_entry_test.rb in rocketjob-2.1.3 vs test/dirmon_entry_test.rb in rocketjob-3.0.0.alpha
- old
+ new
@@ -1,18 +1,13 @@
require_relative 'test_helper'
# Unit Test for RocketJob::Job
class DirmonEntryTest < Minitest::Test
- class OneArgumentJob < RocketJob::Job
- def perform(arg)
- end
- end
-
class WithFullFileNameJob < RocketJob::Job
# Dirmon will store the filename in this property when starting the job
- key :upload_file_name, String
+ field :upload_file_name, type: String
def perform
# Do something with the file name stored in :upload_file_name
end
end
@@ -31,11 +26,11 @@
end
describe RocketJob::DirmonEntry do
describe '.config' do
it 'support multiple databases' do
- assert_equal 'test_rocketjob', RocketJob::DirmonEntry.collection.db.name
+ assert_equal 'rocketjob_test', RocketJob::DirmonEntry.collection.database.name
end
end
describe '#job_class' do
describe 'with a nil job_class_name' do
@@ -54,11 +49,10 @@
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
@@ -88,11 +82,11 @@
end
end
describe '#fail!' do
before do
- @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::OneArgumentJob', pattern: 'test/files/**', arguments: [1])
+ @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::WithFullFileNameJob', pattern: 'test/files/**')
@dirmon_entry.enable!
assert @dirmon_entry.valid?, @dirmon_entry.errors.messages.ai
end
after do
@@ -121,11 +115,11 @@
end
end
describe '#validate' do
it 'existance' do
- assert entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::OneArgumentJob')
+ assert entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::WithFullFileNameJob')
assert_equal false, entry.valid?
assert_equal ["can't be blank"], entry.errors[:pattern], entry.errors.messages.ai
end
describe 'job_class_name' do
@@ -133,56 +127,10 @@
assert entry = RocketJob::DirmonEntry.new(pattern: 'test/files/**')
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.messages.ai
end
end
-
- describe 'arguments' do
- it 'allow no arguments' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'DirmonEntryTest::WithFullFileNameJob',
- pattern: 'test/files/**'
- )
- assert entry.valid?, entry.errors.messages.ai
- end
-
- it 'ensure correct number of arguments' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'DirmonEntryTest::OneArgumentJob',
- pattern: 'test/files/**'
- )
- refute entry.valid?
- assert_equal ['There must be 1 argument(s)'], entry.errors[:arguments], entry.errors.messages.ai
- end
-
- it 'fail if the job name is bad' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'Jobs::Tests::Names::Things',
- pattern: 'test/files/**'
- )
- refute entry.valid?
- assert_equal [], entry.errors[:arguments], entry.errors.messages.ai
- end
- end
-
- it 'invalid without 2 arguments' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'DirmonEntryTest::SumJob',
- pattern: 'test/files/**'
- )
- refute entry.valid?
- assert_equal ['There must be 2 argument(s)'], entry.errors[:arguments], entry.errors.messages.ai
- end
-
- it 'valid with 2 arguments' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'DirmonEntryTest::SumJob',
- pattern: 'test/files/**',
- arguments: [1, 2]
- )
- assert entry.valid?, entry.errors.messages.ai
- end
end
describe 'with valid entry' do
before do
RocketJob::Jobs::DirmonJob.delete_all
@@ -191,19 +139,15 @@
@archive_path.mkpath
@archive_path = @archive_path.realdirpath
@entry = RocketJob::DirmonEntry.new(
pattern: 'test/files/**/*',
job_class_name: 'RocketJob::Jobs::DirmonJob',
- arguments: [{}],
properties: {priority: 23},
archive_directory: @archive_directory
)
- @job = DirmonEntryTest::OneArgumentJob.new(
- @entry.properties.merge(
- arguments: @entry.arguments,
- properties: @entry.properties
- )
+ @job = DirmonEntryTest::WithFullFileNameJob.new(
+ @entry.properties
)
@file = Tempfile.new('archive')
@file_name = @file.path
@pathname = Pathname.new(@file_name)
File.open(@file_name, 'w') { |file| file.write('Hello World') }
@@ -233,34 +177,22 @@
assert File.exist?(@archive_real_name)
end
end
describe '#upload_default' do
- it 'sets full_file_name in Hash argument' do
- @entry.send(:upload_default, @job, @pathname)
- assert_equal @archive_real_name, @job.arguments.first[:full_file_name], @job.arguments
- end
-
it 'sets upload_file_name property' do
@entry = RocketJob::DirmonEntry.new(
pattern: 'test/files/**/*',
job_class_name: 'DirmonEntryTest::WithFullFileNameJob',
archive_directory: @archive_directory
)
assert @entry.valid?, @entry.errors.messages
job = @entry.job_class.new
@entry.send(:upload_default, job, @pathname)
archive_real_name = @archive_path.join("#{job.id}_#{File.basename(@file_name)}").to_s
- assert_equal archive_real_name, job.upload_file_name, job.arguments
+ assert_equal archive_real_name, job.upload_file_name
end
-
- it 'handles non hash argument and missing property' do
- @job.arguments = [1]
- assert_raises ArgumentError do
- @entry.send(:upload_default, @job, @pathname)
- end
- end
end
describe '#upload_file' do
it 'upload using #upload' do
@job.define_singleton_method(:upload) do |file_name|
@@ -271,17 +203,21 @@
end
end
describe '#later' do
it 'enqueues job' do
- @entry.arguments = [{}]
- job = @entry.later(@pathname)
- assert_equal Pathname.new(@archive_directory).join("#{job.id}_#{File.basename(@file_name)}").realdirpath.to_s, job.arguments.first[:full_file_name]
+ @entry = RocketJob::DirmonEntry.new(
+ pattern: 'test/files/**/*',
+ job_class_name: 'DirmonEntryTest::WithFullFileNameJob',
+ archive_directory: @archive_directory
+ )
+ job = @entry.later(@pathname)
+ assert_equal Pathname.new(@archive_directory).join("#{job.id}_#{File.basename(@file_name)}").realdirpath.to_s, job.upload_file_name
assert job.queued?
+ assert_equal 50, job.priority
end
it 'fails with bad job class name' do
- @entry.arguments = [{}]
@entry.job_class_name = 'Blah'
assert_raises ArgumentError do
@entry.later(@pathname)
end
end