Module: Bulkrax::EntrySpecHelper

Defined in:
lib/bulkrax/entry_spec_helper.rb

Overview

The purpose of this module is to provide some testing facilities for those that include the Bulkrax gem in their application.

This module came about through a desire to expose a quick means of vetting the accuracy of the different parsers.

Constant Summary collapse

ENTRY_TYPE_TO_METHOD_NAME_MAP =
{
  entry: :entry_class,
  collection: :collection_entry_class,
  file_set: :file_set_entry_class
}.freeze
DEFAULT_ENTRY_CLASS_TO_SYMBOL_MAP =
{
  'Bulkrax::OaiEntry' => :oai,
  'Bulkrax::XmlEntry' => :xml,
  'Bulkrax::CsvEntry' => :csv
}.freeze

Class Method Summary collapse

Class Method Details

.build_csv_entry_for(importer:, data:, identifier:, entry_class:, **_options) ⇒ Bulkrax::CsvEntry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

As a foible of this implementation, you’ll need to include along a CSV to establish the columns that you’ll parse (e.g. the first row

Parameters:

  • data (Hash<Symbol,String>)

    we’re expecting a hash with keys that are symbols and then values that are strings.

Returns:



134
135
136
137
138
139
140
# File 'lib/bulkrax/entry_spec_helper.rb', line 134

def self.build_csv_entry_for(importer:, data:, identifier:, entry_class:, **_options)
  entry_class.new(
    importerexporter: importer,
    identifier: identifier,
    raw_metadata: data
  )
end

.build_oai_entry_for(importer:, data:, identifier:, entry_class:, **options) ⇒ Bulkrax::OaiEntry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • data (String)

    we’re expecting a string that is well-formed XML for OAI parsing.

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bulkrax/entry_spec_helper.rb', line 148

def self.build_oai_entry_for(importer:, data:, identifier:, entry_class:, **options)
  # The raw record assumes we take the XML data, parse it and then send that to the
  # OAI::GetRecordResponse object.
  doc = XML::Parser.string(data)
  raw_record = OAI::GetRecordResponse.new(doc.parse)

   = {
    importer.parser.source_identifier.to_s => identifier,
    "data" => data,
    "collections" => options.fetch(:raw_metadata_collections, []),
    "children" => options.fetch(:raw_metadata_children, [])
  }

  entry_class.new(
    raw_record: raw_record,
    importerexporter: importer,
    identifier: identifier,
    raw_metadata: 
  )
end

.build_xml_entry_for(importer:, data:, identifier:, entry_class:, **options) ⇒ Bulkrax::XmlEntry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • data (String)

    we’re expecting a string that is well-formed XML.

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bulkrax/entry_spec_helper.rb', line 175

def self.build_xml_entry_for(importer:, data:, identifier:, entry_class:, **options)
   = {
    importer.parser.source_identifier.to_s => identifier,
    "data" => data,
    "collections" => options.fetch(:raw_metadata_collections, []),
    "children" => options.fetch(:raw_metadata_children, [])
  }

  entry_class.new(
    importerexporter: importer,
    identifier: identifier,
    raw_metadata: 
  )
end

.entry_class_to_symbol_mapObject

Present implementations of entry classes tend to inherit from the below listed class names. We’re not looking to register all descendents of the Bulkrax::Entry class, but instead find the ancestor where there is significant deviation.



93
94
95
# File 'lib/bulkrax/entry_spec_helper.rb', line 93

def self.entry_class_to_symbol_map
  @entry_class_to_symbol_map || DEFAULT_ENTRY_CLASS_TO_SYMBOL_MAP
end

.entry_class_to_symbol_map=(value) ⇒ Object



97
98
99
# File 'lib/bulkrax/entry_spec_helper.rb', line 97

def self.entry_class_to_symbol_map=(value)
  @entry_class_to_symbol_map = value
end

.entry_for(data:, identifier:, parser_class_name:, type: :entry, **options) ⇒ Bulkrax::Entry

The purpose of this method is encapsulate the logic of creating the appropriate Bulkrax::Entry object based on the given data, identifier, and parser_class_name.

From that entry, you should be able to test how Bulkrax::Entry#build_metadata populates the Bulkrax::Entry#parsed_metadata variable. Other uses may emerge.

Parameters:

  • data (Object)

    the data that we use to populate the raw metadata. Due to implementation details of each entry, the data will be of different formats.

  • identifier (String, Integer)

    The identifier of the entry. This might also be found in the metadata of the entry, but for instantiation purposes we need this value.

  • parser_class_name (String)

    The name of the parser class you’re wanting to test.

  • type (Sybmol) (defaults to: :entry)

    The type of entry (e.g. :entry, :collection, :file_set) for testing.

  • options (Hash<Symbol,Object>)

    these are to be passed along into the instantiation of the various classes. See implementation details.

Returns:

Since:

  • v5.0.1



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
# File 'lib/bulkrax/entry_spec_helper.rb', line 35

def self.entry_for(data:, identifier:, parser_class_name:, type: :entry, **options)
  importer = importer_for(parser_class_name: parser_class_name, **options)
  entry_type_method_name = ENTRY_TYPE_TO_METHOD_NAME_MAP.fetch(type)
  entry_class = importer.parser.public_send(entry_type_method_name)

  # Using an instance of the entry_class to dispatch to different
  entry_for_dispatch = entry_class.new

  # Using the {is_a?} test we get the benefit of inspecting an object's inheritance path
  # (e.g. ancestry).  The logic, as implemented, also provides a mechanism for folks in their
  # applications to add a {class_name_entry_for}; something that I suspect isn't likely
  # but given the wide variety of underlying needs I could see happening and I want to encourage
  # patterned thinking to fold that different build method into this structure.
  key = entry_class_to_symbol_map.keys.detect { |class_name| entry_for_dispatch.is_a?(class_name.constantize) }

  # Yes, we'll raise an error if we didn't find a corresponding key.  And that's okay.
  symbol = entry_class_to_symbol_map.fetch(key)

  send("build_#{symbol}_entry_for",
       importer: importer,
       identifier: identifier,
       entry_class: entry_class,
       data: data,
       **options)
end

.exporter_for(parser_class_name:, parser_fields: {}, **options) ⇒ Bulkrax::Exporter

Parameters:

  • parser_class_name (String)
  • parser_fields (Hash<String,Hash>) (defaults to: {})

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/bulkrax/entry_spec_helper.rb', line 67

def self.exporter_for(parser_class_name:, parser_fields: {}, **options)
  Bulkrax::Exporter.new(
    name: options.fetch(:exporter_name, "Test importer for identifier"),
    user: options.fetch(:exporter_user, User.new(email: "[email protected]")),
    limit: options.fetch(:exporter_limit, 1),
    parser_klass: parser_class_name,
    field_mapping: options.fetch(:exporter_field_mappings) { Bulkrax.field_mappings.fetch(parser_class_name) },
    parser_fields: parser_fields
  )
end