Skip to content

Commit

Permalink
Create a factory for a cocina object
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Mar 31, 2022
1 parent 2c5f92c commit df0eb2f
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 284 deletions.
51 changes: 51 additions & 0 deletions lib/cocina/models/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module Cocina
module Models
class Factories
def self.build(type, attributes = {})
case type
when :item
build_item(attributes)
end
end

ITEM_DEFAULTS = {
type: Cocina::Models::ObjectType.object,
id: 'druid:bc234fg5678',
version: 1,
label: 'test object',
title: 'test object',
source_id: 'sul:1234',
admin_policy_id: 'druid:hv992ry2431'
}.freeze

# rubocop:disable Metrics/ParameterLists
def self.build_item_properties(type:, id:, version:, label:, title:, source_id:, admin_policy_id:, catkeys: [])
{
type: type,
externalIdentifier: id,
version: version,
label: label,
access: {},
administrative: { hasAdminPolicy: admin_policy_id },
description: {
title: [{ value: title }],
purl: "https://purl.stanford.edu/#{id.delete_prefix('druid:')}"
},
identification: {
sourceId: source_id
},
structural: {}
}.tap do |props|
props[:identification][:catalogLinks] = catkeys.map { |catkey| { catalog: 'symphony', catalogRecordId: catkey } } if catkeys.present?
end
end
# rubocop:enable Metrics/ParameterLists

def self.build_item(attributes)
Cocina::Models.build(build_item_properties(**ITEM_DEFAULTS.merge(attributes)))
end
end
end
end
15 changes: 1 addition & 14 deletions spec/components/show/item/overview_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@
let(:presenter) { instance_double(ArgoShowPresenter, document: doc, cocina: cocina, change_set: change_set, state_service: state_service) }
let(:change_set) { ItemChangeSet.new(cocina) }
let(:cocina) do
Cocina::Models::DRO.new(externalIdentifier: 'druid:bc234fg5678',
type: Cocina::Models::ObjectType.document,
label: 'my dro',
version: 1,
description: {
title: [{ value: 'my dro' }],
purl: 'https://purl.stanford.edu/bc234fg5678'
},
access: {},
identification: { sourceId: 'sul:1234' },
structural: {},
administrative: {
hasAdminPolicy: 'druid:hv992ry2431'
})
Cocina::Models::Factories.build(:item)
end
let(:rendered) { render_inline(component) }
let(:allows_modification) { true }
Expand Down
18 changes: 1 addition & 17 deletions spec/features/item_source_id_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,7 @@
let(:solr_conn) { blacklight_config.repository_class.new(blacklight_config).connection }
let(:druid) { 'druid:kv840xx0000' }
let(:cocina_model) do
Cocina::Models.build({
'label' => 'My ETD',
'version' => 1,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druid,
'description' => {
'title' => [{ 'value' => 'My ETD' }],
'purl' => "https://purl.stanford.edu/#{druid.delete_prefix('druid:')}"
},
'access' => {
'view' => 'world',
'download' => 'world'
},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
'identification' => { sourceId: 'some:thing' }
})
Cocina::Models::Factories.build(:item, id: druid)
end
let(:state_service) { instance_double(StateService, allows_modification?: true) }
let(:events_client) { instance_double(Dor::Services::Client::Events, list: []) }
Expand Down
24 changes: 2 additions & 22 deletions spec/jobs/add_workflow_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,10 @@
let(:user) { bulk_action.user }

let(:cocina1) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 2,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'description' => { title: [{ value: 'Stored title' }], purl: 'https://purl.stanford.edu/bb111cc2222' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:cocina2) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'description' => { title: [{ value: 'Stored title' }], purl: 'https://purl.stanford.edu/cc111dd2222' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[1])
end

let(:object_client1) { instance_double(Dor::Services::Client::Object, find: cocina1) }
Expand Down
31 changes: 2 additions & 29 deletions spec/jobs/apply_apo_defaults_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,11 @@
let(:bulk_action) { create(:bulk_action) }
let(:user) { bulk_action.user }

let(:identification) do
{
catalogLinks: [{ catalog: 'symphony', catalogRecordId: '123' }],
sourceId: 'sul:1234'
}
end

let(:cocina1) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 2,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'description' => { title: [{ value: 'Stored title' }], purl: 'https://purl.stanford.edu/bb111cc2222' },
'structural' => {},
'identification' => identification
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:cocina2) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'description' => { title: [{ value: 'Stored title' }], purl: 'https://purl.stanford.edu/cc111dd2222' },
'structural' => {},
'identification' => identification
})
Cocina::Models::Factories.build(:item, id: druids[1])
end

let(:object_client1) { instance_double(Dor::Services::Client::Object, find: cocina1, apply_admin_policy_defaults: true) }
Expand Down
30 changes: 2 additions & 28 deletions spec/jobs/close_version_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,10 @@
let(:bulk_action) { create(:bulk_action) }

let(:item1) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 1,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[0].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:item2) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 1,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[1].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[1])
end
let(:object_client1) { instance_double(Dor::Services::Client::Object, find: item1, version: version_client) }
let(:object_client2) { instance_double(Dor::Services::Client::Object, find: item2, version: version_client) }
Expand Down
34 changes: 3 additions & 31 deletions spec/jobs/descriptive_metadata_export_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,11 @@
let(:user) { instance_double(User, to_s: 'jcoyne85') }

let(:item1) do
Cocina::Models::DRO.new(externalIdentifier: 'druid:bc123df4567',
type: Cocina::Models::ObjectType.document,
label: 'Test DRO',
version: 1,
description: {
title: [{ value: 'Test DRO #1' }],
purl: 'https://purl.stanford.edu/bc123df4567'
},
access: {},
administrative: {
hasAdminPolicy: 'druid:hv992ry2431'
},
identification: { sourceId: 'sul:4444' },
structural: {})
Cocina::Models::Factories.build(:item, id: 'druid:bc123df4567', source_id: 'sul:4444')
end

let(:item2) do
Cocina::Models::DRO.new(externalIdentifier: 'druid:bd123fg5678',
type: Cocina::Models::ObjectType.document,
label: 'Test DRO',
version: 1,
description: {
title: [{ value: 'Test DRO #2' }],
purl: 'https://purl.stanford.edu/bd123fg5678'
},
access: {},
administrative: {
hasAdminPolicy: 'druid:hv992ry2431'
},
identification: {
sourceId: 'sul:123'
},
structural: {})
Cocina::Models::Factories.build(:item, id: 'druid:bd123fg5678', title: 'Test DRO #2')
end

context 'when happy path' do
Expand All @@ -79,7 +51,7 @@
expect(csv[0][0]).to eq 'druid:bc123df4567'
expect(csv[1][0]).to eq 'druid:bd123fg5678'
expect(csv[0][1]).to eq 'sul:4444'
expect(csv[1][1]).to eq 'sul:123'
expect(csv[1][1]).to eq 'sul:1234'
expect(csv[1]['title1:value']).to eq 'Test DRO #2'
end
end
Expand Down
45 changes: 3 additions & 42 deletions spec/jobs/manage_embargoes_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,13 @@
let(:rights) { ['world', '', 'stanford-nd'] }
let(:buffer) { StringIO.new }
let(:item1) do
Cocina::Models.build({
'label' => 'My Item1',
'version' => 2,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'description' => {
'title' => [{ 'value' => 'My Item1' }],
'purl' => "https://purl.stanford.edu/#{druids[0].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:item2) do
Cocina::Models.build({
'label' => 'My Item2',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'description' => {
'title' => [{ 'value' => 'My Item2' }],
'purl' => "https://purl.stanford.edu/#{druids[1].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[1])
end
let(:item3) do
Cocina::Models.build({
'label' => 'My Item3',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[2],
'description' => {
'title' => [{ 'value' => 'My Item3' }],
'purl' => "https://purl.stanford.edu/#{druids[2].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[2])
end

let(:csv_file) do
Expand Down
30 changes: 2 additions & 28 deletions spec/jobs/prepare_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,10 @@
let(:user) { bulk_action.user }

let(:cocina1) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 2,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[0].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:cocina2) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[1].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[1])
end

let(:object_client1) { instance_double(Dor::Services::Client::Object, find: cocina1) }
Expand Down
30 changes: 2 additions & 28 deletions spec/jobs/purge_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,10 @@
let(:bulk_action) { create(:bulk_action) }

let(:cocina1) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 2,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[0],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[0].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[0])
end
let(:cocina2) do
Cocina::Models.build({
'label' => 'My Item',
'version' => 3,
'type' => Cocina::Models::ObjectType.object,
'externalIdentifier' => druids[1],
'description' => {
'title' => [{ 'value' => 'My Item' }],
'purl' => "https://purl.stanford.edu/#{druids[1].delete_prefix('druid:')}"
},
'access' => {},
'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' },
'structural' => {},
identification: { sourceId: 'sul:1234' }
})
Cocina::Models::Factories.build(:item, id: druids[1])
end

let(:object_client1) { instance_double(Dor::Services::Client::Object, find: cocina1) }
Expand Down
Loading

0 comments on commit df0eb2f

Please sign in to comment.