Skip to content

Commit

Permalink
Merge pull request #905 from sul-dlss/autosave-release-tag
Browse files Browse the repository at this point in the history
Save release tags when updating
  • Loading branch information
justinlittman authored Aug 5, 2024
2 parents becff90 + ac9f32c commit dfd7ca4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/models/purl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Purl < ApplicationRecord
through: :constituent_memberships, source: :child
has_many :parent_memberships, class_name: 'ConstituentMembership', inverse_of: :child, dependent: :destroy
has_many :parents, through: :parent_memberships, source: :parent
has_many :release_tags, dependent: :destroy
has_many :release_tags, dependent: :destroy, autosave: true # Allows updated tags to save
has_one :public_json, dependent: :destroy

accepts_nested_attributes_for :public_json, update_only: true
Expand Down
91 changes: 58 additions & 33 deletions spec/requests/v1/purls_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@
end
end

context 'with an existing unversioned item' do
let(:purl_object) { create(:purl) }
let(:druid) { purl_object.druid }
context 'with an existing item' do
let(:purl_druid_path) { purl_object.purl_druid_path }
let(:meta_path) { Pathname.new(purl_druid_path) / 'meta.json' }
let(:purl_object) { create(:purl, druid:) }

before do
FileUtils.rm_r(purl_druid_path) if File.directory?(purl_druid_path)
Expand All @@ -74,45 +73,71 @@
FileUtils.rm_rf(purl_druid_path)
end

it 'puts a Kafka message on the queue for indexing' do
expect { put("/v1/purls/#{druid}/release_tags", params: data, headers:) }.to change(meta_path, :exist?)
.from(false).to(true)
expect(response).to have_http_status(:accepted)
context 'with an existing unversioned item' do
let(:purl_object) { create(:purl) }
let(:druid) { purl_object.druid }

it 'puts a Kafka message on the queue for indexing' do
expect { put("/v1/purls/#{druid}/release_tags", params: data, headers:) }.to change(meta_path, :exist?)
.from(false).to(true)
expect(response).to have_http_status(:accepted)

expect(Racecar).to have_received(:produce_sync)
.with(key: druid, topic: 'testing_topic', value: purl_object.as_public_json.to_json)
expect(Racecar).to have_received(:produce_sync)
.with(key: druid, topic: 'testing_topic', value: purl_object.as_public_json.to_json)
end
end
end

context 'with an existing versioned item' do
let(:purl_object) { create(:purl, druid:) }
let(:druid) { 'druid:bc123df4567' }
let(:purl_druid_path) { purl_object.purl_druid_path }
let(:meta_path) { Pathname.new(purl_druid_path) / 'meta.json' }
context 'with an existing versioned item' do
let(:druid) { 'druid:bc123df4567' }

let(:stacks_path) { Pathname.new('tmp/stacks') }
let(:stacks_meta_path) { stacks_path / 'bc/123/df/4567/bc123df4567/versions/meta.json' }
let(:stacks_path) { Pathname.new('tmp/stacks') }
let(:stacks_meta_path) { stacks_path / 'bc/123/df/4567/bc123df4567/versions/meta.json' }

before do
allow(Settings.features).to receive(:legacy_purl).and_return(true)
allow(Settings.filesystems).to receive(:stacks_root).and_return(stacks_path.to_s)
before do
allow(Settings.features).to receive(:legacy_purl).and_return(true)
allow(Settings.filesystems).to receive(:stacks_root).and_return(stacks_path.to_s)

FileUtils.mkdir_p(purl_druid_path)
FileUtils.mkdir_p(stacks_meta_path.dirname)
end
FileUtils.mkdir_p(stacks_meta_path.dirname)
end

after do
FileUtils.rm_rf(stacks_path)
FileUtils.rm_rf(purl_druid_path)
end
after do
FileUtils.rm_rf(stacks_path)
end

it 'puts a Kafka message on the queue for indexing' do
expect { put("/v1/purls/#{druid}/release_tags", params: data, headers:) }.to change(meta_path, :exist?)
.from(false).to(true).and change(stacks_meta_path, :exist?).from(false).to(true)
expect(response).to have_http_status(:accepted)

it 'puts a Kafka message on the queue for indexing' do
expect { put("/v1/purls/#{druid}/release_tags", params: data, headers:) }.to change(meta_path, :exist?)
.from(false).to(true).and change(stacks_meta_path, :exist?).from(false).to(true)
expect(response).to have_http_status(:accepted)
expect(Racecar).to have_received(:produce_sync)
.with(key: druid, topic: 'testing_topic', value: purl_object.as_public_json.to_json)
end
end

expect(Racecar).to have_received(:produce_sync)
.with(key: druid, topic: 'testing_topic', value: purl_object.as_public_json.to_json)
context 'with an existing versioned item that is being unreleased' do
let(:druid) { 'druid:bc123df4567' }
let(:data) { { actions: { 'index' => [], 'delete' => ['Searchworks'] } }.to_json }
let(:stacks_path) { Pathname.new('tmp/stacks') }
let(:stacks_meta_path) { stacks_path / 'bc/123/df/4567/bc123df4567/versions/meta.json' }

before do
allow(Settings.features).to receive(:legacy_purl).and_return(true)
allow(Settings.filesystems).to receive(:stacks_root).and_return(stacks_path.to_s)

FileUtils.mkdir_p(stacks_meta_path.dirname)
purl_object.release_tags.create(name: "Searchworks", release_type: true)
end

after do
FileUtils.rm_rf(stacks_path)
end

it 'puts a Kafka message on the queue for indexing' do
expect { put("/v1/purls/#{druid}/release_tags", params: data, headers:) }.to change(meta_path, :exist?)
.from(false).to(true).and change(stacks_meta_path, :exist?).from(false).to(true)
expect(response).to have_http_status(:accepted)
expect(purl_object.reload.release_tags.first.release_type).to be false
end
end
end

Expand Down

0 comments on commit dfd7ca4

Please sign in to comment.