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 optimistic_lock_token to valkyrie resources on query #475

Merged
merged 1 commit into from
Jul 30, 2018
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
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
27 changes: 27 additions & 0 deletions lib/valkyrie/specs/shared_specs/locking_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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
27 changes: 27 additions & 0 deletions spec/valkyrie/persistence/solr/query_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
# 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
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"

describe "optimistic locking" do
let(:query_service) { adapter.query_service } unless defined? query_service
let(:persister) { adapter.persister }

before do
class CustomLockingResource < Valkyrie::Resource
enable_optimistic_locking
attribute :id, Valkyrie::Types::ID.optional
attribute :title
end
end

after do
Object.send(:remove_const, :CustomLockingResource)
end

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
end