-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_virtual_objects_job_spec.rb
101 lines (82 loc) · 3.49 KB
/
create_virtual_objects_job_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CreateVirtualObjectsJob, type: :job do
let(:child1_id) { 'druid:child1' }
let(:child2_id) { 'druid:child2' }
let(:parent_id) { 'druid:mk420bs7601' }
let(:result) { create(:background_job_result) }
let(:service) { instance_double(ConstituentService, add: nil) }
let(:virtual_objects) { [{ parent_id: parent_id, child_ids: [child1_id, child2_id] }] }
before do
allow(ConstituentService).to receive(:new).with(parent_druid: parent_id).and_return(service)
allow(BackgroundJobResult).to receive(:find).and_return(result)
allow(result).to receive(:processing!)
end
context 'with no errors' do
before do
described_class.perform_now(virtual_objects: virtual_objects,
background_job_result: result)
end
it 'marks the job as processing' do
expect(result).to have_received(:processing!).once
end
it 'invokes the constituent service to do the virtual object creation' do
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id]).once
end
it 'marks the job as complete' do
expect(result).to be_complete
end
it 'has no output' do
expect(result.output).to be_blank
end
end
context 'with errors returned by constituent service' do
before do
allow(service).to receive(:add).and_return(parent_id => ['One thing was not combinable', 'And another'])
described_class.perform_now(virtual_objects: virtual_objects,
background_job_result: result)
end
it 'marks the job as processing' do
expect(result).to have_received(:processing!).once
end
it 'invokes the constituent service to do the virtual object creation' do
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id]).once
end
it 'marks the job as complete' do
expect(result).to be_complete
end
it 'has output with errors' do
expect(result.output[:errors].first[parent_id]).to match_array(['One thing was not combinable', 'And another'])
end
end
context 'with a mix of errors and successful results' do
let(:other_child1_id) { 'druid:bar' }
let(:other_child2_id) { 'druid:baz' }
let(:other_parent_id) { 'druid:foo' }
let(:virtual_objects) do
[
{ parent_id: parent_id, child_ids: [child1_id, child2_id] },
{ parent_id: other_parent_id, child_ids: [other_child1_id, other_child2_id] }
]
end
before do
allow(ConstituentService).to receive(:new).with(parent_druid: other_parent_id).and_return(service)
allow(service).to receive(:add).and_return(nil, parent_id => ['One thing was not combinable', 'And another'])
described_class.perform_now(virtual_objects: virtual_objects,
background_job_result: result)
end
it 'marks the job as processing' do
expect(result).to have_received(:processing!).once
end
it 'invokes the constituent service to do the virtual object creation' do
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id]).once
expect(service).to have_received(:add).with(child_druids: [other_child1_id, other_child2_id]).once
end
it 'marks the job as complete' do
expect(result).to be_complete
end
it 'has output with errors' do
expect(result.output[:errors].first[parent_id]).to match_array(['One thing was not combinable', 'And another'])
end
end
end