spec/statused_routines_spec.rb in lev-4.3.2 vs spec/statused_routines_spec.rb in lev-5.0.0

- old
+ new

@@ -3,154 +3,165 @@ class StatusedRoutine lev_routine protected def exec - status.set_progress(9, 10) + job.set_progress(9, 10) end end RSpec.describe 'Statused Routines' do - subject(:status) { Lev::Status.new } + subject(:job) { Lev::BackgroundJob.new } context 'in a routine' do - it 'queues the status object on queue' do - uuid = StatusedRoutine.perform_later - status = Lev::Status.find(uuid) + it 'queues the job object on queue' do + id = StatusedRoutine.perform_later + job = Lev::BackgroundJob.find(id) - expect(status['state']).to eq(Lev::Status::STATE_QUEUED) + expect(job.status).to eq(Lev::BackgroundJob::STATE_QUEUED) end context 'inline activejob mode' do before { ::ActiveJob::Base.queue_adapter = :inline } after { ::ActiveJob::Base.queue_adapter = :test } - it 'sets status to working when called' do - expect_any_instance_of(Lev::Status).to receive(:working!) + it 'sets job to working when called' do + expect_any_instance_of(Lev::BackgroundJob).to receive(:working!) StatusedRoutine.perform_later end - it 'completes the status object on completion, returning other data' do - uuid = StatusedRoutine.perform_later - status = Lev::Status.find(uuid) - expect(status['state']).to eq(Lev::Status::STATE_COMPLETED) - expect(status['progress']).to eq(0.9) + it 'completes the job object on completion, returning other data' do + id = StatusedRoutine.perform_later + job = Lev::BackgroundJob.find(id) + expect(job.status).to eq(Lev::BackgroundJob::STATE_COMPLETED) + expect(job.progress).to eq(0.9) end end end describe '#save' do + it 'saves the hash given and writes them to the job' do + job.save(something: 'else') + expect(job.something).to eq('else') + end + end + + describe '#add_error' do + it 'adds the error object data to the job object' do + errors = Lev::Error.new(code: 'bad', message: 'awful') + job.add_error(errors) + expect(job.errors).to eq([{ is_fatal: false, + code: 'bad', + message: 'awful' }]) + end + end + + describe '#save' do it 'prevents the use of reserved keys' do expect { - status.save(progress: 'blocked') + job.save(progress: 'blocked') }.to raise_error(Lev::IllegalArgument) expect { - status.save(uuid: 'blocked') + job.save(id: 'blocked') }.to raise_error(Lev::IllegalArgument) expect { - status.save(state: 'blocked') + job.save(status: 'blocked') }.to raise_error(Lev::IllegalArgument) expect { - status.save(errors: 'blocked') + job.save(errors: 'blocked') }.to raise_error(Lev::IllegalArgument) end - it 'saves the hash given and writes them to the status' do - status.save(something: 'else') - expect(status.get('something')).to eq('else') + it 'saves the hash given and writes them to the job' do + job.save(something: 'else') + expect(job).to respond_to(:something) + expect(job.something).to eq('else') end end - describe '#add_error' do - it 'adds the error object data to the status object' do - errors = Lev::Error.new(code: 'bad', message: 'awful') - status.add_error(errors) - expect(status.get('errors')).to eq([{ 'is_fatal' => false, - 'code' => 'bad', - 'message' => 'awful' }]) - end - end - - describe 'dynamic status setters/getters' do + describe 'dynamic job setters/getters' do it 'is queued' do - expect(status).not_to be_queued - status.queued! - expect(status).to be_queued + expect(job).not_to be_queued + job.queued! + expect(job).to be_queued end it 'is working' do - expect(status).not_to be_working - status.working! - expect(status).to be_working + expect(job).not_to be_working + job.working! + expect(job).to be_working end it 'is completed' do - expect(status).not_to be_completed - status.completed! - expect(status).to be_completed + expect(job).not_to be_completed + job.completed! + expect(job).to be_completed end it 'is failed' do - expect(status).not_to be_failed - status.failed! - expect(status).to be_failed + expect(job).not_to be_failed + job.failed! + expect(job).to be_failed end it 'is killed' do - expect(status).not_to be_killed - status.killed! - expect(status).to be_killed + expect(job).not_to be_killed + job.killed! + expect(job).to be_killed end + + it 'is unknown' do + expect(job).to be_unknown + end end describe '#set_progress' do - it 'sets the progress key on the status object' do - status.set_progress(8, 10) - progress = status.get('progress') - expect(progress).to eq(0.8) + it 'sets the progress key on the job object' do + job.set_progress(8, 10) + expect(job.progress).to eq(0.8) end context 'when `out_of` is supplied' do it 'requires a positive `at` float or integer' do expect { - status.set_progress(nil, 1) + job.set_progress(nil, 1) }.to raise_error(Lev::IllegalArgument) expect { - status.set_progress(-1, 1) + job.set_progress(-1, 1) }.to raise_error(Lev::IllegalArgument) expect { - status.set_progress(2, 5) + job.set_progress(2, 5) }.not_to raise_error end it 'requires `out_of` to be greater than `at`' do expect { - status.set_progress(15, 8) + job.set_progress(15, 8) }.to raise_error(Lev::IllegalArgument) expect { - status.set_progress(5, 10) + job.set_progress(5, 10) }.not_to raise_error end end context 'without out_of specified' do it 'requires `at` to be a float between 0.0 and 1.0' do expect { - status.set_progress(1.1) + job.set_progress(1.1) }.to raise_error(Lev::IllegalArgument) expect { - status.set_progress(-1) + job.set_progress(-1) }.to raise_error(Lev::IllegalArgument) expect { - status.set_progress(0.78) + job.set_progress(0.78) }.not_to raise_error end end end