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'
}.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



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

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



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

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



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

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



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

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

.build_collection_lite(attributes) ⇒ Object



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

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

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



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

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



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

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

.build_dro_lite(attributes) ⇒ Object



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

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

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



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

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



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

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



205
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
# File 'lib/cocina/rspec/factories.rb', line 205

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



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

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



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

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



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

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



99
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
# File 'lib/cocina/rspec/factories.rb', line 99

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