From 0ec8f0d2e128accb1764dc2b4b05e2f8c8365a68 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Fri, 14 Feb 2020 14:17:05 +0100 Subject: [PATCH] put method in model for better testing --- app/jobs/subj_check_by_id_job.rb | 5 +---- app/models/event.rb | 13 ++++++++++--- spec/models/event_spec.rb | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/jobs/subj_check_by_id_job.rb b/app/jobs/subj_check_by_id_job.rb index 1ed34a303..e08dac885 100644 --- a/app/jobs/subj_check_by_id_job.rb +++ b/app/jobs/subj_check_by_id_job.rb @@ -2,9 +2,6 @@ class SubjCheckByIdJob < ActiveJob::Base queue_as :lupo_background def perform(event, options = {}) - subj_prefix = event[:subj_id][/(10\.\d{4,5})/, 1] - if Prefix.where(prefix: subj_prefix).exists? - Event.find_by(id: event[:id]).update_attribute(:state_event, "subj_id_error") - end + Event.label_state_event(event) end end diff --git a/app/models/event.rb b/app/models/event.rb index a8b22e16d..ab09dca77 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -541,24 +541,31 @@ def self.subj_id_check(options = {}) size = (options[:size] || 1000).to_i cursor = [options[:from_id], options[:until_id]] - response = Event.query(nil, source_id: "datacite-crossref", page: { size: 1, cursor: [] }) + response = Event.query(nil, source_id: "datacite-crossref", page: { size: 1, cursor: [] }) Rails.logger.warn "[DoubleCheck] #{response.results.total} events for source datacite-crossref." # walk through results using cursor if response.results.total.positive? while response.results.results.length.positive? - response = Event.query(nil, source_id: "datacite-crossref", page: { size: size, cursor: cursor }) + response = Event.query(nil, source_id: "datacite-crossref",page: { size: size, cursor: cursor }) break unless response.results.results.length.positive? Rails.logger.warn "[DoubleCheck] DoubleCheck #{response.results.results.length} events starting with _id #{response.results.to_a.first[:_id]}." cursor = response.results.to_a.last[:sort] - events = response.results.results.map { |item| { id: item.id, subj_id: item.subj_id } } + events = response.results.results.map { |item| { uuid: item.uuid, subj_id: item.subj_id } } SubjCheckJob.perform_later(events, options) end end end + def self.label_state_event(event) + subj_prefix = event[:subj_id][/(10\.\d{4,5})/, 1] + unless Prefix.where(prefix: subj_prefix).exists? + Event.find_by(uuid: event[:uuid]).update_attribute(:state_event, "crossref_citations_error") + end + end + def metric_type if relation_type_id.to_s =~ /(requests|investigations)/ arr = relation_type_id.split("-", 4) diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 95b03cc78..e7f4101a8 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -38,6 +38,21 @@ expect(published).not_to eq(2011) end + it "label_state_event with not existent prefix" do + expect(Event.find_by(uuid: subject.uuid ).state_event).to be_nil + Event.label_state_event({uuid:subject.uuid , subj_id:subject.subj_id}) + expect(Event.find_by(uuid: subject.uuid ).state_event).to eq("crossref_citations_error") + end + + context "prefix exists, then dont to change" do + let!(:prefix) { create(:prefix, prefix: "10.5061") } + it "label_state_event with existent prefix" do + expect(Event.find_by(uuid: subject.uuid ).state_event).to be_nil + Event.label_state_event({uuid:subject.uuid , subj_id:subject.subj_id}) + expect(Event.find_by(uuid: subject.uuid ).state_event).to be_nil + end + end + # context "double_crossref_check", elasticsearch: true do # let(:provider) { create(:provider, symbol: "DATACITE") } # let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) }