Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for stale files in the update directory path #2332

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/jobs/scsb_import_full_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class ScsbImportFullJob < ApplicationJob
def perform
delete_stale_files

Event.record do |event|
event.dump = created_dump
event.save!
Expand All @@ -17,4 +19,9 @@ def dump_type
def created_dump
Dump.create!(dump_type:)
end

def delete_stale_files
FileUtils.rm Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.zip")
FileUtils.rm Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.xml")
end
end
6 changes: 6 additions & 0 deletions spec/fixtures/scsb_updates/scsb_update_20240108_183400_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0'?>
<collection xmlns='http://www.loc.gov/MARC21/slim'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<record><leader>02152cam a2200493 i 4500</leader><controlfield tag='001'>SCSB-2170814</controlfield><controlfield tag='003'>NNC</controlfield><controlfield tag='005'>20240105113043.0</controlfield><controlfield tag='008'>810220s1976 txu 000 0beng </controlfield><controlfield tag='009'>39998</controlfield><datafield ind1=' ' ind2=' ' tag='010'><subfield code='a'> 76620048 </subfield></datafield><datafield ind1=' ' ind2=' ' tag='035'><subfield code='a'>(OCoLC)ocm03111350</subfield></datafield><datafield ind1=' ' ind2=' ' tag='040'><subfield code='a'>DLC</subfield><subfield code='b'>eng</subfield><subfield code='c'>DLC</subfield><subfield code='d'>OCL</subfield><subfield code='d'>OCLCQ</subfield><subfield code='d'>BAKER</subfield><subfield code='d'>BTCTA</subfield><subfield code='d'>YDXCP</subfield><subfield code='d'>OCLCG</subfield><subfield code='d'>NLE</subfield><subfield code='d'>HEBIS</subfield><subfield code='d'>OCLCO</subfield><subfield code='d'>OCLCQ</subfield><subfield code='d'>OCLCF</subfield><subfield code='d'>OCLCO</subfield><subfield code='d'>FC@</subfield><subfield code='d'>OCLCA</subfield><subfield code='d'>NLC</subfield><subfield code='d'>TYC</subfield><subfield code='d'>OCLCQ</subfield><subfield code='d'>OCL</subfield><subfield code='d'>UKMGB</subfield><subfield code='d'>OCLCQ</subfield><subfield code='d'>OCLCO</subfield><subfield code='d'>OCL</subfield><subfield code='d'>NIU</subfield><subfield code='d'>GZN</subfield><subfield code='d'>OCLCO</subfield><subfield code='d'>OCLCL</subfield></datafield><datafield ind1=' ' ind2=' ' tag='015'><subfield code='a'>781538947</subfield><subfield code='2'>can</subfield></datafield><datafield ind1=' ' ind2=' ' tag='020'><subfield code='a'>087959022X</subfield></datafield><datafield ind1=' ' ind2=' ' tag='020'><subfield code='a'>9780879590222</subfield></datafield><datafield ind1=' ' ind2=' ' tag='035'><subfield code='a'>(OCoLC)3111350</subfield></datafield><datafield ind1='0' ind2='0' tag='050'><subfield code='a'>PR5366</subfield><subfield code='b'>.L38 1976</subfield></datafield><datafield ind1='0' ind2='0' tag='082'><subfield code='a'>822/.9/12</subfield><subfield code='2'>18</subfield></datafield><datafield ind1='1' ind2='4' tag='082'><subfield code='a'>B</subfield></datafield><datafield ind1=' ' ind2=' ' tag='084'><subfield code='a'>cci1icc</subfield><subfield code='2'>lacc</subfield></datafield><datafield ind1=' ' ind2=' ' tag='049'><subfield code='a'>ZCUA</subfield></datafield><datafield ind1='1' ind2=' ' tag='100'><subfield code='a'>Laurence, Dan H.</subfield></datafield><datafield ind1='1' ind2='0' tag='245'><subfield code='a'>Shaw, books, and libraries /</subfield><subfield code='c'>Dan H. Laurence.</subfield></datafield><datafield ind1=' ' ind2=' ' tag='260'><subfield code='a'>[Austin] :</subfield><subfield code='b'>Humanities Research Center, University of Texas at Austin,</subfield><subfield code='c'>©1976.</subfield></datafield><datafield ind1=' ' ind2=' ' tag='300'><subfield code='a'>28 pages ;</subfield><subfield code='c'>23 cm</subfield></datafield><datafield ind1=' ' ind2=' ' tag='336'><subfield code='a'>text</subfield><subfield code='b'>txt</subfield><subfield code='2'>rdacontent</subfield></datafield><datafield ind1=' ' ind2=' ' tag='337'><subfield code='a'>unmediated</subfield><subfield code='b'>n</subfield><subfield code='2'>rdamedia</subfield></datafield><datafield ind1=' ' ind2=' ' tag='338'><subfield code='a'>volume</subfield><subfield code='b'>nc</subfield><subfield code='2'>rdacarrier</subfield></datafield><datafield ind1='1' ind2=' ' tag='490'><subfield code='a'>Bibliographical monograph series ;</subfield><subfield code='v'>no. 9</subfield></datafield><datafield ind1=' ' ind2=' ' tag='500'><subfield code='a'>&quot;Five hundred copies have been printed on Curtis Rag. The type is Intertype Baskerville. Design by William R. Holman&quot;--Colophon.</subfield></datafield><datafield ind1='1' ind2='0' tag='600'><subfield code='a'>Shaw, Bernard,</subfield><subfield code='d'>1856-1950</subfield><subfield code='v'>Books and reading.</subfield></datafield><datafield ind1=' ' ind2='0' tag='650'><subfield code='a'>Dramatists, Irish</subfield><subfield code='y'>20th century</subfield><subfield code='v'>Biography.</subfield></datafield><datafield ind1=' ' ind2='0' tag='650'><subfield code='a'>Dramatists, English</subfield><subfield code='y'>20th century</subfield><subfield code='x'>Biography.</subfield></datafield><datafield ind1='1' ind2='7' tag='600'><subfield code='a'>Shaw, Bernard,</subfield><subfield code='d'>1856-1950</subfield><subfield code='2'>fast</subfield><subfield code='1'>https://id.oclc.org/worldcat/entity/E39PBJfmqrHfjT8qvwDyYWVKVC</subfield><subfield code='0'>(OCoLC)fst00030129</subfield></datafield><datafield ind1=' ' ind2='7' tag='650'><subfield code='a'>Dramatists, English</subfield><subfield code='2'>fast</subfield><subfield code='0'>(OCoLC)fst00897581</subfield></datafield><datafield ind1=' ' ind2='7' tag='650'><subfield code='a'>Dramatists, Irish</subfield><subfield code='2'>fast</subfield><subfield code='0'>(OCoLC)fst00897608</subfield></datafield><datafield ind1=' ' ind2='7' tag='648'><subfield code='a'>1900-1999</subfield><subfield code='2'>fast</subfield></datafield><datafield ind1=' ' ind2=' ' tag='653'><subfield code='a'>American cinema films</subfield><subfield code='a'>Shaw, Bernard</subfield><subfield code='a'>1856-1950</subfield></datafield><datafield ind1=' ' ind2='7' tag='655'><subfield code='a'>Biographies</subfield><subfield code='2'>fast</subfield><subfield code='0'>(OCoLC)fst01919896</subfield></datafield><datafield ind1='1' ind2=' ' tag='700'><subfield code='a'>Holman, William Roger,</subfield><subfield code='e'>designer.</subfield></datafield><datafield ind1='0' ind2='8' tag='776'><subfield code='i'>Online version:</subfield><subfield code='a'>Laurence, Dan H.</subfield><subfield code='t'>Shaw, books, and libraries.</subfield><subfield code='d'>[Austin] : Humanities Research Center, University of Texas at Austin, ©1976</subfield><subfield code='w'>(OCoLC)560755727</subfield></datafield><datafield ind1=' ' ind2='0' tag='830'><subfield code='a'>Bibliographical monograph ;</subfield><subfield code='v'>no. 9.</subfield></datafield><datafield ind1='0' ind2='0' tag='852'><subfield code='h'>PR5366 .L38 1976</subfield><subfield code='0'>2137624</subfield><subfield code='b'>scsbcul</subfield></datafield><datafield ind1=' ' ind2=' ' tag='876'><subfield code='0'>2137624</subfield><subfield code='a'>3304030</subfield><subfield code='j'>Available</subfield><subfield code='p'>CU55398863</subfield><subfield code='t'>1</subfield><subfield code='x'>Shared</subfield><subfield code='z'>CU</subfield><subfield code='l'>RECAP</subfield></datafield></record>
</collection>
24 changes: 24 additions & 0 deletions spec/jobs/scsb_import_full_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,28 @@
expect(event.dump.dump_type.constant).to eq 'PARTNER_RECAP_FULL'
expect(Scsb::PartnerUpdates).to have_received(:full)
end

describe 'when there stale files in the update directory path' do
let(:update_directory_path) { Rails.root.join("tmp", "specs", "update_directory") }

before do
FileUtils.mkdir_p(update_directory_path)
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('SCSB_PARTNER_UPDATE_DIRECTORY').and_return(update_directory_path)
FileUtils.cp('spec/fixtures/scsb_updates/CUL_20210429_192300.zip', update_directory_path)
FileUtils.cp('spec/fixtures/scsb_updates/NYPL_20210430_015000.zip', update_directory_path)
FileUtils.cp('spec/fixtures/scsb_updates/HL_20210716_063500.zip', update_directory_path)
FileUtils.cp('spec/fixtures/scsb_updates/scsb_update_20240108_183400_1.xml', update_directory_path)
end

it 'removes stale files' do
expect { described_class.perform_now }.to change { Event.count }.by(1)

expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'CUL_20210429_192300.zip'))).to be false
expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'NYPL_20210430_015000.zip'))).to be false
expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'HL_20210716_063500.zip'))).to be false
expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'scsb_update_20240108_183400_1.xml'))).to be false
expect(Dir.exist?(Rails.root.join("tmp", "specs", "update_directory"))).to be true
end
end
end
Loading