Skip to content

Commit

Permalink
put method in model for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Feb 14, 2020
1 parent 1fe9b88 commit 0ec8f0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 1 addition & 4 deletions app/jobs/subj_check_by_id_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 10 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }
Expand Down

0 comments on commit 0ec8f0d

Please sign in to comment.