Class: Cocina::RSpec::Factories

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina/rspec/factories.rb

Overview

Factories for Cocina model objects.

Defined Under Namespace

Modules: Methods

Constant Summary collapse

SUPPORTED_TYPES =
%i[
  admin_policy
  admin_policy_with_metadata
  admin_policy_lite
  collection
  collection_with_metadata
  collection_lite
  dro
  dro_with_metadata
  dro_lite
  request_admin_policy
  request_collection
  request_dro
].freeze
WITH_METADATA_SUFFIX =
'_with_metadata'
DRO_DEFAULTS =
{
  type: Cocina::Models::ObjectType.object,
  id: 'druid:bc234fg5678',
  version: 1,
  label: 'factory DRO label',
  title: 'factory DRO title',
  source_id: 'sul:1234',
  admin_policy_id: 'druid:hv992ry2431'
}.freeze
REQUEST_DRO_DEFAULTS =
DRO_DEFAULTS.except(:id)
COLLECTION_DEFAULTS =
{
  type: Cocina::Models::ObjectType.collection,
  id: 'druid:bb222ff5555',
  version: 1,
  label: 'factory collection label',
  title: 'factory collection title',
  admin_policy_id: 'druid:hv992ry2431',
  source_id: 'sulcollection:1234'
}.freeze
REQUEST_COLLECTION_DEFAULTS =
COLLECTION_DEFAULTS.except(:id)
ADMIN_POLICY_DEFAULTS =
{
  type: Cocina::Models::ObjectType.admin_policy,
  id: 'druid:cb432gf8765',
  version: 1,
  label: 'factory APO label',
  title: 'factory APO title',
  admin_policy_id: 'druid:hv992ry2431',
  agreement_id: 'druid:hp308wm0436'
}.freeze
REQUEST_ADMIN_POLICY_DEFAULTS =
ADMIN_POLICY_DEFAULTS.except(:id)

Class Method Summary collapse

Class Method Details

.build(type, attributes = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/cocina/rspec/factories.rb', line 36

def self.build(type, attributes = {})
  raise "Unsupported factory type #{type}" unless supported_type?(type)

  build_type = type.to_s.delete_suffix(WITH_METADATA_SUFFIX)

  fixture = public_send("build_#{build_type}".to_sym, attributes) # rubocop:disable Lint/SymbolConversion
  return fixture unless type.end_with?(WITH_METADATA_SUFFIX)

  Cocina::Models.(fixture, 'abc123')
end

.build_admin_policy(attributes) ⇒ Object



185
186
187
# File 'lib/cocina/rspec/factories.rb', line 185

def self.build_admin_policy(attributes)
  Cocina::Models.build(build_admin_policy_properties(**ADMIN_POLICY_DEFAULTS.merge(attributes)))
end

.build_admin_policy_lite(attributes) ⇒ Object



189
190
191
# File 'lib/cocina/rspec/factories.rb', line 189

def self.build_admin_policy_lite(attributes)
  Cocina::Models.build_lite(build_admin_policy_properties(**ADMIN_POLICY_DEFAULTS.merge(attributes)))
end

.build_admin_policy_properties(id:, **kwargs) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/cocina/rspec/factories.rb', line 197

def self.build_admin_policy_properties(id:, **kwargs)
  build_request_admin_policy_properties(**kwargs)
    .merge(externalIdentifier: id)
    .tap do |props|
    props[:description][:purl] = "https://purl.stanford.edu/#{id.delete_prefix('druid:')}"
  end
end

.build_collection(attributes) ⇒ Object

rubocop:enable Metrics/ParameterLists



173
174
175
# File 'lib/cocina/rspec/factories.rb', line 173

def self.build_collection(attributes)
  Cocina::Models.build(build_collection_properties(**COLLECTION_DEFAULTS.merge(attributes)))
end

.build_collection_lite(attributes) ⇒ Object



177
178
179
# File 'lib/cocina/rspec/factories.rb', line 177

def self.build_collection_lite(attributes)
  Cocina::Models.build_lite(build_collection_properties(**COLLECTION_DEFAULTS.merge(attributes)))
end

.build_collection_properties(id:, **kwargs) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/cocina/rspec/factories.rb', line 137

def self.build_collection_properties(id:, **kwargs)
  build_request_collection_properties(**kwargs)
    .merge(externalIdentifier: id)
    .tap do |props|
    props[:description][:purl] = "https://purl.stanford.edu/#{id.delete_prefix('druid:')}"
  end
end

.build_dro(attributes) ⇒ Object



91
92
93
# File 'lib/cocina/rspec/factories.rb', line 91

def self.build_dro(attributes)
  Cocina::Models.build(build_dro_properties(**DRO_DEFAULTS.merge(attributes)))
end

.build_dro_lite(attributes) ⇒ Object



95
96
97
# File 'lib/cocina/rspec/factories.rb', line 95

def self.build_dro_lite(attributes)
  Cocina::Models.build_lite(build_dro_properties(**DRO_DEFAULTS.merge(attributes)))
end

.build_dro_properties(id:, **kwargs) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/cocina/rspec/factories.rb', line 83

def self.build_dro_properties(id:, **kwargs)
  build_request_dro_properties(**kwargs)
    .merge(externalIdentifier: id)
    .tap do |props|
    props[:description][:purl] = "https://purl.stanford.edu/#{id.delete_prefix('druid:')}"
  end
end

.build_request_admin_policy(attributes) ⇒ Object



193
194
195
# File 'lib/cocina/rspec/factories.rb', line 193

def self.build_request_admin_policy(attributes)
  Cocina::Models.build_request(build_request_admin_policy_properties(**REQUEST_ADMIN_POLICY_DEFAULTS.merge(attributes)))
end

.build_request_admin_policy_properties(type:, version:, label:, title:, admin_policy_id:, agreement_id:, use_statement: nil, copyright: nil, license: nil, registration_workflow: nil, collections_for_registration: nil, without_description: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/cocina/rspec/factories.rb', line 206

def self.build_request_admin_policy_properties(type:, version:, label:, title:,
                                               admin_policy_id:, agreement_id:,
                                               use_statement: nil, copyright: nil, license: nil,
                                               registration_workflow: nil, collections_for_registration: nil,
                                               without_description: false)
  {
    type: type,
    version: version,
    label: label,
    administrative: {
      hasAdminPolicy: admin_policy_id,
      hasAgreement: agreement_id,
      accessTemplate: {
        view: 'world',
        download: 'world'
      }
    },
    description: {
      title: [{ value: title }]
    }
  }.tap do |props|
    props[:administrative][:accessTemplate][:useAndReproductionStatement] = use_statement if use_statement
    props[:administrative][:accessTemplate][:copyright] = copyright if copyright
    props[:administrative][:accessTemplate][:license] = license if license
    props[:administrative][:registrationWorkflow] = registration_workflow if registration_workflow
    props[:administrative][:collectionsForRegistration] = collections_for_registration if collections_for_registration
    props.delete(:description) if without_description
  end
end

.build_request_collection(attributes) ⇒ Object



181
182
183
# File 'lib/cocina/rspec/factories.rb', line 181

def self.build_request_collection(attributes)
  Cocina::Models.build_request(build_request_collection_properties(**REQUEST_COLLECTION_DEFAULTS.merge(attributes)))
end

.build_request_collection_properties(type:, version:, label:, title:, admin_policy_id:, source_id: nil, catkeys: [], folio_instance_hrids: []) ⇒ Object

rubocop:disable Metrics/ParameterLists



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cocina/rspec/factories.rb', line 146

def self.build_request_collection_properties(type:, version:, label:, title:, admin_policy_id:, source_id: nil, catkeys: [], folio_instance_hrids: [])
  {
    type: type,
    version: version,
    label: label,
    access: {},
    administrative: { hasAdminPolicy: admin_policy_id },
    description: {
      title: [{ value: title }]
    },
    identification: {}
  }.tap do |props|
    if catkeys.present?
      props[:identification][:catalogLinks] = catkeys.map.with_index do |catkey, index|
        { catalog: 'symphony', catalogRecordId: catkey, refresh: index.zero? }
      end
    end
    if folio_instance_hrids.present?
      props[:identification][:catalogLinks] = folio_instance_hrids.map.with_index do |folio_id, index|
        { catalog: 'folio', catalogRecordId: folio_id, refresh: index.zero? }
      end
    end
    props[:identification][:sourceId] = source_id if source_id
  end
end

.build_request_dro(attributes) ⇒ Object

rubocop:enable Metrics/ParameterLists



133
134
135
# File 'lib/cocina/rspec/factories.rb', line 133

def self.build_request_dro(attributes)
  Cocina::Models.build_request(build_request_dro_properties(**REQUEST_DRO_DEFAULTS.merge(attributes)))
end

.build_request_dro_properties(type:, version:, label:, title:, source_id:, admin_policy_id:, barcode: nil, catkeys: [], folio_instance_hrids: [], collection_ids: []) ⇒ Object

rubocop:disable Metrics/ParameterLists



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cocina/rspec/factories.rb', line 100

def self.build_request_dro_properties(type:, version:, label:, title:, source_id:, admin_policy_id:,
                                      barcode: nil, catkeys: [], folio_instance_hrids: [], collection_ids: [])
  {
    type: type,
    version: version,
    label: label,
    access: {},
    administrative: { hasAdminPolicy: admin_policy_id },
    description: {
      title: [{ value: title }]
    },
    identification: {
      sourceId: source_id
    },
    structural: {
      isMemberOf: collection_ids
    }
  }.tap do |props|
    if catkeys.present?
      props[:identification][:catalogLinks] = catkeys.map.with_index do |catkey, index|
        { catalog: 'symphony', catalogRecordId: catkey, refresh: index.zero? }
      end
    end
    if folio_instance_hrids.present?
      props[:identification][:catalogLinks] = folio_instance_hrids.map.with_index do |folio_id, index|
        { catalog: 'folio', catalogRecordId: folio_id, refresh: index.zero? }
      end
    end
    props[:identification][:barcode] = barcode if barcode
  end
end

.supported_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cocina/rspec/factories.rb', line 30

def self.supported_type?(type)
  SUPPORTED_TYPES.include?(type)
end