test/dirmon_entry_test.rb in rocketjob-1.3.0 vs test/dirmon_entry_test.rb in rocketjob-2.0.0.rc1
- old
+ new
@@ -1,20 +1,37 @@
require_relative 'test_helper'
-require_relative 'jobs/test_job'
# 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 :full_file_name, String
+ key :upload_file_name, String
def perform
- # Do something with the file name stored in :full_file_name
+ # Do something with the file name stored in :upload_file_name
end
end
+ class SumJob < RocketJob::Job
+ @@result = nil
+ # For temp test data
+ def self.result
+ @@result
+ end
+
+ def perform(a, b)
+ @@result = a + b
+ end
+ end
+
describe RocketJob::DirmonEntry do
describe '.config' do
it 'support multiple databases' do
assert_equal 'test_rocketjob', RocketJob::DirmonEntry.collection.db.name
end
@@ -55,35 +72,37 @@
after do
RocketJob::DirmonEntry.whitelist_paths.each { |path| RocketJob::DirmonEntry.delete_whitelist_path(path) }
end
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')
+ path = Pathname('test/files').realpath.to_s
+ assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/files')
assert_equal [path], RocketJob::DirmonEntry.whitelist_paths
end
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')
+ path = Pathname('test/files').realpath.to_s
+ assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/files')
+ assert_equal path, RocketJob::DirmonEntry.add_whitelist_path('test/files')
assert_equal path, RocketJob::DirmonEntry.add_whitelist_path(path)
assert_equal [path], RocketJob::DirmonEntry.whitelist_paths
end
end
- describe '#fail_with_exception!' do
+ describe '#fail!' do
before do
- @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: 'test/files/**', arguments: [1])
+ @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::OneArgumentJob', pattern: 'test/files/**', arguments: [1])
@dirmon_entry.enable!
+ assert @dirmon_entry.valid?, @dirmon_entry.errors.messages.ai
end
+
after do
@dirmon_entry.destroy if @dirmon_entry && @dirmon_entry.new_record?
end
it 'fail with message' do
- @dirmon_entry.fail_with_exception!('myworker:2323', 'oh no')
+ @dirmon_entry.fail!('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
@@ -92,121 +111,98 @@
begin
blah
rescue Exception => exc
exception = exc
end
- @dirmon_entry.fail_with_exception!('myworker:2323', exception)
+ @dirmon_entry.fail!('myworker:2323', exception)
assert_equal true, @dirmon_entry.failed?
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
describe '#validate' do
it 'existance' do
- assert entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob')
+ assert entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::OneArgumentJob')
assert_equal false, entry.valid?
- assert_equal ["can't be blank"], entry.errors[:pattern], entry.errors.inspect
+ assert_equal ["can't be blank"], entry.errors[:pattern], entry.errors.messages.ai
end
- 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
-
describe 'job_class_name' do
it 'ensure presence' do
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.inspect
+ 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: 'Jobs::TestJob',
- pattern: 'test/files/**',
- perform_method: :result
- )
- assert_equal true, entry.valid?, entry.errors.inspect
- assert_equal [], entry.errors[:arguments], entry.errors.inspect
+ 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: 'Jobs::TestJob',
- pattern: 'test/files/**'
- )
- assert_equal false, entry.valid?
- assert_equal ['There must be 1 argument(s)'], entry.errors[:arguments], entry.errors.inspect
+ 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 'return false if the job name is bad' do
+ it 'fail if the job name is bad' do
assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'Jobs::Tests::Names::Things',
- pattern: 'test/files/**'
- )
- assert_equal false, entry.valid?
- assert_equal [], entry.errors[:arguments], entry.errors.inspect
+ 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 'arguments with perform_method' do
+ it 'invalid without 2 arguments' do
assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'Jobs::TestJob',
- pattern: 'test/files/**',
- perform_method: :sum
- )
- assert_equal false, entry.valid?
- assert_equal ['There must be 2 argument(s)'], entry.errors[:arguments], entry.errors.inspect
+ 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' do
+ it 'valid with 2 arguments' do
assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'Jobs::TestJob',
- pattern: 'test/files/**',
- arguments: [1]
- )
- assert entry.valid?, entry.errors.inspect
+ job_class_name: 'DirmonEntryTest::SumJob',
+ pattern: 'test/files/**',
+ arguments: [1, 2]
+ )
+ assert entry.valid?, entry.errors.messages.ai
end
-
- it 'valid with perform_method' do
- assert entry = RocketJob::DirmonEntry.new(
- job_class_name: 'Jobs::TestJob',
- pattern: 'test/files/**',
- perform_method: :sum,
- arguments: [1, 2]
- )
- assert entry.valid?, entry.errors.inspect
- end
end
describe 'with valid entry' do
before do
+ RocketJob::Jobs::DirmonJob.delete_all
@archive_directory = '/tmp/archive_directory'
@archive_path = Pathname.new(@archive_directory)
@archive_path.mkpath
@archive_path = @archive_path.realdirpath
@entry = RocketJob::DirmonEntry.new(
pattern: 'test/files/**/*',
- job_class_name: 'Jobs::TestJob',
+ job_class_name: 'RocketJob::Jobs::DirmonJob',
arguments: [{}],
- properties: {priority: 23, perform_method: :event},
+ properties: {priority: 23},
archive_directory: @archive_directory
)
- @job = Jobs::TestJob.new(
+ @job = DirmonEntryTest::OneArgumentJob.new(
@entry.properties.merge(
- arguments: @entry.arguments,
- properties: @entry.properties,
- perform_method: @entry.perform_method
+ arguments: @entry.arguments,
+ properties: @entry.properties
)
)
@file = Tempfile.new('archive')
@file_name = @file.path
@pathname = Pathname.new(@file_name)
@@ -215,10 +211,11 @@
@archive_real_name = @archive_path.join("#{@job.id}_#{File.basename(@file_name)}").to_s
end
after do
@file.delete if @file
+ RocketJob::Jobs::DirmonJob.delete_all
end
describe '#archive_pathname' do
it 'with archive directory' do
assert_equal File.dirname(@archive_real_name), @entry.archive_pathname(@pathname).to_s
@@ -241,21 +238,21 @@
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 full_file_name property' do
+ 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.full_file_name, job.arguments
+ assert_equal archive_real_name, job.upload_file_name, job.arguments
end
it 'handles non hash argument and missing property' do
@job.arguments = [1]
assert_raises ArgumentError do
@@ -263,33 +260,32 @@
end
end
end
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
-
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
describe '#later' do
- it 'enqueue job' do
- @entry.arguments = [{}]
- @entry.perform_method = :event
- job = @entry.later(@pathname)
+ 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]
assert job.queued?
+ 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
end
describe '#each' do
it 'without archive path' do