From a585723b296b6f8937288e9d114362ffa0717392 Mon Sep 17 00:00:00 2001 From: Eduardo Martin Rojo Date: Fri, 20 Sep 2019 11:45:47 +0100 Subject: [PATCH] By adding the study uuid while importing, the plate transfer was failing while copying the fact that stored the study. This was happening because the transfer method was not copying the literal property from the original fact. Added a validation to fact to check that you cannot have a fact that is not a literal and does not contain a relation either. --- app/models/fact.rb | 3 +++ lib/actions/plate_transfer.rb | 2 +- spec/lib/actions/plate_transfer_spec.rb | 12 ++++++++++++ spec/models/fact_spec.rb | 26 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 spec/models/fact_spec.rb diff --git a/app/models/fact.rb b/app/models/fact.rb index 986249ab..465b34cd 100644 --- a/app/models/fact.rb +++ b/app/models/fact.rb @@ -8,6 +8,9 @@ class Fact < ActiveRecord::Base scope :with_fact, -> (predicate, object) { where(:predicate => predicate, :object => object)} scope :from_remote_asset, ->() { where(:is_remote? => true) } + validates :object_asset_id, presence: true, unless: :literal? + validates :object_asset_id, presence: false, if: :literal? + def set_to_remove_by(step) update_attributes!(:to_remove_by => step) end diff --git a/lib/actions/plate_transfer.rb b/lib/actions/plate_transfer.rb index 45bee622..e9af676c 100644 --- a/lib/actions/plate_transfer.rb +++ b/lib/actions/plate_transfer.rb @@ -76,7 +76,7 @@ def self.transfer_with_asset_creation(plate, destination, updates=nil) destination_well = Asset.new updates.create_assets([destination_well]) source_well.facts.each do |fact| - updates.add(destination_well, fact.predicate, fact.object_value) + updates.add(destination_well, fact.predicate, fact.object_value, literal: fact.literal) end updates.add(destination_well, 'barcodeType', 'NoBarcode') updates.add(destination_well, 'aliquotType', aliquot.object) if aliquot && !source_well.has_predicate?('aliquotType') diff --git a/spec/lib/actions/plate_transfer_spec.rb b/spec/lib/actions/plate_transfer_spec.rb index fe9cefb9..7c4a83a8 100644 --- a/spec/lib/actions/plate_transfer_spec.rb +++ b/spec/lib/actions/plate_transfer_spec.rb @@ -33,6 +33,18 @@ expect(updates.to_h[:add_facts].select{|t| t[1]=='concentration'}.map{|t| t[2]}).to eq(["1.3"]) expect(updates.to_h[:add_facts].select{|t| t[1]=='a'}.map{|t| t[2]}).to eq(["Well", "Well"]) end + it 'can copy facts with uuid values' do + source = create :asset + well = create(:asset) + + source.facts << create(:fact, predicate: 'contains', object_asset: well) + well.facts << create(:fact, predicate: 'study', object: SecureRandom.uuid, literal: true) + + destination = create :asset + updates = Actions::PlateTransfer.transfer_plates(source,destination) + expect(updates.to_h[:set_errors].nil?).to eq(true) + end + end end end diff --git a/spec/models/fact_spec.rb b/spec/models/fact_spec.rb new file mode 100644 index 00000000..ce4e283a --- /dev/null +++ b/spec/models/fact_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe Fact do + context 'when it defines a literal' do + it 'can store a uuid' do + fact = build(:fact, predicate: 'pred', object: SecureRandom.uuid, object_asset_id: nil, literal: true) + expect(fact).to be_valid + end + it 'can store a string' do + fact = build(:fact, predicate: 'pred', object: 'str', object_asset_id: nil, literal: true) + expect(fact).to be_valid + end + end + context 'when defines a relation' do + let(:asset) { create :asset } + it 'cannot store anything without object_asset_id defined' do + fact = build(:fact, predicate: 'pred', object: "str", object_asset_id: asset, literal: false) + expect(fact).not_to be_valid + end + it 'cannot store any string if object_asset_id defined' do + fact = build(:fact, predicate: 'pred', object: "str", object_asset_id: asset, literal: false) + expect(fact).not_to be_valid + end + end + +end