From a249e294ab7a037424868697908f6bb9bd708703 Mon Sep 17 00:00:00 2001 From: Lauren Malek Date: Sat, 21 Sep 2024 08:24:26 -0400 Subject: [PATCH] Addressing the failing tests for DSpace and DescribeIndexer --- .gitignore | 2 + app/lib/describe_indexer.rb | 11 ++++ app/lib/dspace_indexer.rb | 11 ++++ app/models/version_footer.rb | 61 ++++++++++++++------ spec/fixtures/migration/dspace_migration.csv | 2 - spec/models/version_footer_spec.rb | 8 ++- spec/rails_helper.rb | 1 + spec/requests/catalog_spec.rb | 24 +++++++- spec/spec_helper.rb | 2 - 9 files changed, 96 insertions(+), 26 deletions(-) delete mode 100644 spec/fixtures/migration/dspace_migration.csv diff --git a/.gitignore b/.gitignore index d1403ac5..3b88eee0 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,5 @@ node_modules # https://vitejs.dev/guide/env-and-mode.html#env-files *.local .yardoc/ + +spec/fixtures/migration/dspace_migration.csv diff --git a/app/lib/describe_indexer.rb b/app/lib/describe_indexer.rb index 8827c120..d95f442e 100644 --- a/app/lib/describe_indexer.rb +++ b/app/lib/describe_indexer.rb @@ -61,6 +61,17 @@ def index_one(json) traject_indexer.complete end + def client + @client ||= Blacklight.default_index.connection + end + + def delete!(query:) + client.delete_by_query(query) + client.commit + client.optimize + client + end + private def rss_http_response diff --git a/app/lib/dspace_indexer.rb b/app/lib/dspace_indexer.rb index 82e25b9e..6e130156 100644 --- a/app/lib/dspace_indexer.rb +++ b/app/lib/dspace_indexer.rb @@ -48,4 +48,15 @@ def self.index(_options) i.index i end + + def client + @client ||= Blacklight.default_index.connection + end + + def delete!(query:) + client.delete_by_query(query) + client.commit + client.optimize + client + end end diff --git a/app/models/version_footer.rb b/app/models/version_footer.rb index d8ffa599..7c76b6af 100644 --- a/app/models/version_footer.rb +++ b/app/models/version_footer.rb @@ -14,6 +14,8 @@ # rubocop:disable Style/ClassVars class VersionFooter DEPLOYMENT_LOGFILE_COL_NUMBER = 7 + REVISIONS_LOG_COL = 3 + DEFAULT_SHA = "Unknown SHA" @@stale = true @@git_sha = nil @@ -31,29 +33,52 @@ def self.reset! @@git_sha = nil @@branch = nil @@version = nil - # @@stale = true # potentially add this to prevent @@stale from retaining state from prior test + + @@local_sha = nil + @@revisions = nil + + @@git_sha = nil + @@revisions_head = nil + @@revisions_log = nil end - def self.stale? - return false if @@stale == false - # Only read the file when version information is stale - if File.exist?(revision_file) - local_sha = File.read(revision_file).chomp.gsub(/\)$/, '') - @@stale = local_sha != git_sha - else - @@stale = true - end - @@stale + def self.revisions + return unless File.exist?(revision_file) + + @@revisions ||= File.read(revision_file).chomp + end + + def self.local_sha + return unless revisions + + @@local_sha ||= revisions.gsub(/\)$/, '') + end + + def self.revisions_log + return unless File.exist?(revisions_logfile) + + content = `tail -1 #{revisions_logfile}`.chomp + elements = content.split(" ") + return if elements.length < REVISIONS_LOG_COL + + element = elements[REVISIONS_LOG_COL] + @@revisions_log ||= element.gsub(/\)$/, '') + end + + def self.revisions_head + return unless Rails.env.development? || Rails.env.test? + + @@revisions_head ||= `git rev-parse HEAD`.chomp end def self.git_sha - @@git_sha ||= if File.exist?(revisions_logfile) - `tail -1 #{revisions_logfile}`.chomp.split(" ")[3].gsub(/\)$/, '') - elsif Rails.env.development? || Rails.env.test? - `git rev-parse HEAD`.chomp - else - "Unknown SHA" - end + @@git_sha ||= revisions_log || revisions_head || DEFAULT_SHA + end + + def self.stale? + return false unless local_sha + + local_sha != git_sha end def self.tagged_release? diff --git a/spec/fixtures/migration/dspace_migration.csv b/spec/fixtures/migration/dspace_migration.csv deleted file mode 100644 index 2780475d..00000000 --- a/spec/fixtures/migration/dspace_migration.csv +++ /dev/null @@ -1,2 +0,0 @@ -parent_community,community,collection_name,title,handle,ark_url,doi,curator,redescribed,pdc_describe_id,data_migrated -Princeton Plasma Physics Laboratory,Advanced Projects,Other Projects,"Chatbots as social companions: Perceiving consciousness, human likeness, and social health benefits in machines",88435/dsp01t435gh23t,https://dataspace.princeton.edu/handle/88435/dsp01t435gh23t,"","","","","" diff --git a/spec/models/version_footer_spec.rb b/spec/models/version_footer_spec.rb index f74678ba..5032b562 100644 --- a/spec/models/version_footer_spec.rb +++ b/spec/models/version_footer_spec.rb @@ -4,6 +4,10 @@ # rubocop:disable RSpec/ExampleLength RSpec.describe VersionFooter do + before do + described_class.reset! + end + describe "info" do context "with stale information" do before do @@ -44,9 +48,9 @@ described_class.revisions_logfile = Pathname.new(fixture_paths.first).join("revisions_rollback.log").to_s described_class.reset! end - xit "detects current information" do + it "detects current information" do info = described_class.info - expect(info[:stale]).to be false # may want to reconsider what state stale should be + expect(info[:stale]).to be true expect(info[:sha]).to eq "to" expect(info[:branch]).to eq "rolled" expect(info[:version]).to eq "(Deployment date could not be parsed from: deploy rolled back to release 20211210150445\n.)" diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 30c0d4c4..d25f7d16 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true ENV['RAILS_ENV'] ||= 'test' +ENV['RACK_ENV'] ||= 'test' require 'spec_helper' require File.expand_path('../config/environment', __dir__) diff --git a/spec/requests/catalog_spec.rb b/spec/requests/catalog_spec.rb index caa221ee..6a5d4ee9 100644 --- a/spec/requests/catalog_spec.rb +++ b/spec/requests/catalog_spec.rb @@ -7,10 +7,15 @@ let(:indexer) do DspaceIndexer.new(dspace_fixtures) end + before do indexer.index end + after do + indexer.delete!(query: "*:*") + end + describe "GET /doi/:doi" do let(:doi) { "doi:10.1088/0029-5515/57/1/016034" } let(:document_id) { "84912" } @@ -69,8 +74,7 @@ let(:indexer) { DescribeIndexer.new(rss_url: rss_url_string) } before do - Blacklight.default_index.connection.delete_by_query("*:*") - Blacklight.default_index.connection.commit + indexer.delete!(query: "*:*") stub_request(:get, "https://pdc-describe-prod.princeton.edu/describe/works.rss") .to_return(status: 200, body: rss_feed) stub_request(:get, "https://pdc-describe-prod.princeton.edu/describe/works/6.json") @@ -87,6 +91,22 @@ follow_redirect! expect(response.body).to include("test title") end + + describe "#show" do + it "shows the catalog" do + document = SolrDocument.new(id: "doi-10-34770-r75s-9j74") + get "/catalog/#{document.id}", params: { format: "json" } + expect(response.status).to eq(200) + end + end + + describe "#bibtex" do + it "returns citations" do + document = SolrDocument.new(id: "doi-10-34770-r75s-9j74") + get "/catalog/#{document.id}/bibtex", params: { id: document.id } + expect(response.status).to eq(200) + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 43d84f0f..c466dff5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -ENV['RACK_ENV'] ||= 'test' - require "coveralls" Coveralls.wear!