-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimistic_lock_token to valkyrie resources on query
connected to #286 connected to #453 Co-authored-by: Adam Wead <[email protected]> Co-authored-by: Michael Tribone <[email protected]>
- Loading branch information
1 parent
dc17ade
commit aa77738
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
RSpec.shared_examples 'a Valkyrie locking query provider' do | ||
before do | ||
raise 'adapter must be set with `let(:adapter)`' unless | ||
defined? adapter | ||
class CustomLockingQueryResource < Valkyrie::Resource | ||
enable_optimistic_locking | ||
attribute :id, Valkyrie::Types::ID.optional | ||
attribute :title | ||
end | ||
end | ||
after do | ||
Object.send(:remove_const, :CustomLockingQueryResource) | ||
end | ||
|
||
let(:query_service) { adapter.query_service } unless defined? query_service | ||
let(:persister) { adapter.persister } | ||
subject { adapter.query_service } | ||
|
||
it "retrieves the lock token and casts it to optimistic_lock_token attribute" do | ||
resource = CustomLockingQueryResource.new(title: "My Title") | ||
resource = persister.save(resource: resource) | ||
resource = query_service.find_by(id: resource.id) | ||
# we can't know the value in the general case | ||
expect(resource.optimistic_lock_token).not_to be_empty | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,58 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'valkyrie/specs/shared_specs' | ||
require 'valkyrie/specs/shared_specs/locking_query' | ||
|
||
RSpec.describe Valkyrie::Persistence::Solr::QueryService do | ||
before do | ||
client = RSolr.connect(url: SOLR_TEST_URL) | ||
client.delete_by_query("*:*") | ||
client.commit | ||
|
||
class CustomLockingResource < Valkyrie::Resource | ||
enable_optimistic_locking | ||
attribute :id, Valkyrie::Types::ID.optional | ||
attribute :title | ||
end | ||
end | ||
let(:adapter) { Valkyrie::Persistence::Solr::MetadataAdapter.new(connection: client) } | ||
let(:client) { RSolr.connect(url: SOLR_TEST_URL) } | ||
it_behaves_like "a Valkyrie query provider" | ||
it_behaves_like "a Valkyrie locking query provider" | ||
|
||
after do | ||
Object.send(:remove_const, :CustomLockingResource) | ||
end | ||
|
||
let(:query_service) { adapter.query_service } unless defined? query_service | ||
let(:persister) { adapter.persister } | ||
|
||
context "with optimistic locking enabled" do | ||
it "retrieves the lock token and casts it to optimistic_lock_token attribute" do | ||
resource = persister.save(resource: CustomLockingResource.new(title: "My Title")) | ||
resource = query_service.find_by(id: resource.id) | ||
query_doc = (query_service.connection.get 'select', :params => {:q => "id:#{resource.id}"})["response"]["docs"].first | ||
expect(resource.optimistic_lock_token.first).to eq query_doc["_version_"] | ||
end | ||
end | ||
|
||
context "without locking enabled" do | ||
before do | ||
class CustomNonlockingResource < Valkyrie::Resource | ||
enable_optimistic_locking | ||
attribute :id, Valkyrie::Types::ID.optional | ||
attribute :title | ||
end | ||
end | ||
after do | ||
Object.send(:remove_const, :CustomNonlockingResource) | ||
end | ||
|
||
it "retrieves the resource without an optimistic_lock_token attribute" do | ||
resource = persister.save(resource: CustomNonlockingResource.new(title: "My Title")) | ||
resource = query_service.find_by(id: resource.id) | ||
query_doc = (query_service.connection.get 'select', :params => {:q => "id:#{resource.id}"})["response"]["docs"].first | ||
expect(resource.optimistic_lock_token.first).to eq query_doc["_version_"] | ||
end | ||
end | ||
end |