From 17e55d0558154bf8b0edadedc36271d1c7301e25 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Fri, 3 Nov 2023 12:53:39 -0700 Subject: [PATCH] Make fixture generator able to re-process without ASpace access. --- app/services/aspace_fixture_generator.rb | 22 +++++++++++++++---- .../services/aspace_fixture_generator_spec.rb | 11 ++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/services/aspace_fixture_generator.rb b/app/services/aspace_fixture_generator.rb index ab2d9424..979fa113 100644 --- a/app/services/aspace_fixture_generator.rb +++ b/app/services/aspace_fixture_generator.rb @@ -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 @@ -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) @@ -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 diff --git a/spec/services/aspace_fixture_generator_spec.rb b/spec/services/aspace_fixture_generator_spec.rb index 6c630153..1480ade2 100644 --- a/spec/services/aspace_fixture_generator_spec.rb +++ b/spec/services/aspace_fixture_generator_spec.rb @@ -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