Skip to content

Commit

Permalink
Add optimistic_lock_token to valkyrie resources on query
Browse files Browse the repository at this point in the history
connected to #286
connected to #453

Co-authored-by: Adam Wead <[email protected]>
Co-authored-by: Michael Tribone <[email protected]>
  • Loading branch information
3 people committed Jul 30, 2018
1 parent dc17ade commit aa77738
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/valkyrie/persistence/solr/orm_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def internal_resource
end

def attributes
attribute_hash.merge("id" => id, internal_resource: internal_resource, created_at: created_at, updated_at: updated_at)
attribute_hash.merge("id" => id, internal_resource: internal_resource, created_at: created_at, updated_at: updated_at, optimistic_lock_token: version)
end

def created_at
Expand All @@ -35,6 +35,10 @@ def updated_at
DateTime.parse(solr_document["timestamp"] || solr_document.fetch("created_at_dtsi").to_s).utc
end

def version
solr_document.fetch('_version_', nil)
end

def id
solr_document.fetch('id').sub(/^id-/, '')
end
Expand Down
28 changes: 28 additions & 0 deletions lib/valkyrie/specs/shared_specs/locking_query.rb
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
44 changes: 44 additions & 0 deletions spec/valkyrie/persistence/solr/query_service_spec.rb
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

0 comments on commit aa77738

Please sign in to comment.