From 3740d481ddd8eb16f29d0294dc510f7fcf006712 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Thu, 31 Mar 2022 15:29:24 -0500 Subject: [PATCH] Create a factory for a cocina object --- lib/cocina/models/factories.rb | 49 +++++++++++++++++++ .../show/item/overview_component_spec.rb | 15 +----- spec/features/item_source_id_change_spec.rb | 18 +------ spec/jobs/add_workflow_job_spec.rb | 24 +-------- spec/jobs/apply_apo_defaults_job_spec.rb | 31 +----------- spec/jobs/close_version_job_spec.rb | 30 +----------- .../descriptive_metadata_export_job_spec.rb | 34 ++----------- spec/jobs/manage_embargoes_job_spec.rb | 45 ++--------------- spec/rails_helper.rb | 1 + spec/requests/serials_spec.rb | 17 +------ 10 files changed, 66 insertions(+), 198 deletions(-) create mode 100644 lib/cocina/models/factories.rb diff --git a/lib/cocina/models/factories.rb b/lib/cocina/models/factories.rb new file mode 100644 index 0000000000..df1474b57a --- /dev/null +++ b/lib/cocina/models/factories.rb @@ -0,0 +1,49 @@ +# 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:) + { + 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: {} + } + end + # rubocop:enable Metrics/ParameterLists + + def self.build_item(attributes) + Cocina::Models.build(build_item_properties(**ITEM_DEFAULTS.merge(attributes))) + end + end + end +end diff --git a/spec/components/show/item/overview_component_spec.rb b/spec/components/show/item/overview_component_spec.rb index 080d96baef..8a098b7674 100644 --- a/spec/components/show/item/overview_component_spec.rb +++ b/spec/components/show/item/overview_component_spec.rb @@ -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 } diff --git a/spec/features/item_source_id_change_spec.rb b/spec/features/item_source_id_change_spec.rb index 6a58f8f79c..a7e945f6a7 100644 --- a/spec/features/item_source_id_change_spec.rb +++ b/spec/features/item_source_id_change_spec.rb @@ -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: []) } diff --git a/spec/jobs/add_workflow_job_spec.rb b/spec/jobs/add_workflow_job_spec.rb index 915eb00a73..c215facc39 100644 --- a/spec/jobs/add_workflow_job_spec.rb +++ b/spec/jobs/add_workflow_job_spec.rb @@ -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) } diff --git a/spec/jobs/apply_apo_defaults_job_spec.rb b/spec/jobs/apply_apo_defaults_job_spec.rb index 8619a07eec..0b5bebfc00 100644 --- a/spec/jobs/apply_apo_defaults_job_spec.rb +++ b/spec/jobs/apply_apo_defaults_job_spec.rb @@ -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) } diff --git a/spec/jobs/close_version_job_spec.rb b/spec/jobs/close_version_job_spec.rb index 9a2930d2bc..c7732aac57 100644 --- a/spec/jobs/close_version_job_spec.rb +++ b/spec/jobs/close_version_job_spec.rb @@ -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) } diff --git a/spec/jobs/descriptive_metadata_export_job_spec.rb b/spec/jobs/descriptive_metadata_export_job_spec.rb index 94edf44045..89adbcb053 100644 --- a/spec/jobs/descriptive_metadata_export_job_spec.rb +++ b/spec/jobs/descriptive_metadata_export_job_spec.rb @@ -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 @@ -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 diff --git a/spec/jobs/manage_embargoes_job_spec.rb b/spec/jobs/manage_embargoes_job_spec.rb index 472d2984d0..66a44613d4 100644 --- a/spec/jobs/manage_embargoes_job_spec.rb +++ b/spec/jobs/manage_embargoes_job_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 9fc818fcbd..eccb1e8b54 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -9,6 +9,7 @@ # Add additional requires below this line. Rails is not loaded until this point! require 'cocina/rspec' require 'capybara/rspec' +require 'cocina/models/factories' require 'equivalent-xml/rspec_matchers' require 'view_component/test_helpers' require 'webmock/rspec' diff --git a/spec/requests/serials_spec.rb b/spec/requests/serials_spec.rb index a28ae21e5b..9832b7f0d0 100644 --- a/spec/requests/serials_spec.rb +++ b/spec/requests/serials_spec.rb @@ -7,20 +7,7 @@ let(:druid) { 'druid:dc243mg0841' } let(:cocina_model) do - Cocina::Models.build({ - 'label' => 'My Serial', - 'version' => 1, - 'type' => Cocina::Models::ObjectType.object, - 'externalIdentifier' => druid, - 'description' => { - 'title' => [{ 'value' => 'My Serial' }], - 'purl' => "https://purl.stanford.edu/#{druid.delete_prefix('druid:')}" - }, - 'access' => {}, - 'administrative' => { hasAdminPolicy: 'druid:cg532dg5405' }, - 'structural' => {}, - identification: { sourceId: 'sul:1234' } - }) + Cocina::Models::Factories.build(:item, id: druid, label: 'My Serial', title: 'My Serial') end before do @@ -61,7 +48,7 @@ }, 'access' => {}, identification: { sourceId: 'sul:1234' }, - 'administrative' => { 'hasAdminPolicy' => 'druid:cg532dg5405' }, + 'administrative' => { 'hasAdminPolicy' => 'druid:hv992ry2431' }, 'structural' => {} }) end