Skip to content

Commit

Permalink
Make fixture generator able to re-process without ASpace access.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon authored and hackartisan committed Nov 6, 2023
1 parent 144b992 commit 17e55d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 18 additions & 4 deletions app/services/aspace_fixture_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ class AspaceFixtureGenerator
"C1372" => ["aspace_C1372_c47202-68234"],
"C0171" => [],
"C0958" => ["aspace_C0958_c06413-76702"],
"TC040" => ["aspace_TC040_c00002"],
"TC040" => ["aspace_TC040_c00002", "aspace_TC040_c00034"],
"AC198.10" => ["aspace_AC198.10_c6"]
}.freeze

attr_reader :client, :ead_ids, :component_map, :fixture_dir
def initialize(client: Aspace::Client.new, ead_ids: EAD_IDS, component_map: COMPONENT_MAP, fixture_dir: Rails.root.join("spec", "fixtures", "aspace", "generated"))
attr_reader :ead_ids, :component_map, :fixture_dir
def initialize(client: nil, ead_ids: EAD_IDS, component_map: COMPONENT_MAP, fixture_dir: Rails.root.join("spec", "fixtures", "aspace", "generated"))
@client = client
@ead_ids = ead_ids
@component_map = component_map
Expand All @@ -172,6 +172,10 @@ def regenerate!

private

def client
@client ||= Aspace::Client.new
end

# Filter an EAD to just the components in the component map and write it to a
# separate file.
def process(fixture_file)
Expand Down Expand Up @@ -200,11 +204,21 @@ def select_components(fixture_file, components)
def fixture_files
@fixture_files ||=
ead_ids.lazy.map do |eadid|
uri, repo_code = client.ead_url_for_eadid(eadid: eadid)&.first
uri, repo_code = uri_and_repo(eadid: eadid)
EADContainer.new(eadid: eadid, content: get_content(uri, eadid), repository: repo_code)
end
end

def uri_and_repo(eadid:)
file = fixture_dir.glob("**/*.EAD.xml").find { |x| x.to_s.ends_with?("#{eadid}.EAD.xml") }
if file.present?
# Don't have to query the client for the repo, it's in the directory name.
[nil, file.dirname.basename.to_s]
else
client.ead_url_for_eadid(eadid: eadid)&.first
end
end

# Only fetch content from ASpace if the file doesn't already exist.
# @note This was added because sometimes these take a long time to fetch, and
# all we want to do is process the original to have more or less components
Expand Down
11 changes: 11 additions & 0 deletions spec/services/aspace_fixture_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
# EAD.
expect(content.search("//c").length).to eq 2
expect(content.search("//c").map { |x| x["id"] }).to eq ["aspace_C1588_c1", "aspace_C1588_c2"]

allow(Aspace::Client).to receive(:new).and_raise(ArchivesSpace::ConnectionError)
# Running it again works without hitting client.
fixture_generator = described_class.new(
client: nil,
ead_ids: ["1"],
component_map: { "1" => ["aspace_C1588_c2"] },
fixture_dir: Rails.root.join("tmp", "fixture_tests")
)

expect { fixture_generator.regenerate! }.not_to raise_error
end
end
end

0 comments on commit 17e55d0

Please sign in to comment.