Skip to content

Commit

Permalink
show new properties in event serializer. #390
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 17, 2020
1 parent ee4ea05 commit c5d8d7c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ GEM
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
bibtex-ruby (4.4.7)
bibtex-ruby (5.1.1)
latex-decode (~> 0.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bolognese (1.4.5)
bolognese (1.4.6)
activesupport (>= 4.2.5, < 6)
benchmark_methods (~> 0.7)
bibtex-ruby (~> 4.1)
bibtex-ruby (>= 5.1.0)
builder (~> 3.2, >= 3.2.2)
citeproc-ruby (~> 1.1, >= 1.1.10)
colorize (~> 0.8.1)
Expand Down Expand Up @@ -515,7 +515,7 @@ GEM
sprockets (>= 3.0.0)
string_pattern (2.2.1)
regexp_parser (~> 1.3, >= 1.3.0)
strip_attributes (1.9.1)
strip_attributes (1.9.2)
activemodel (>= 3.0, < 7.0)
strong_migrations (0.6.2)
activerecord (>= 5)
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def index
response = Event.query(params[:query],
subj_id: params[:subj_id],
obj_id: params[:obj_id],
source_doi: params[:source_doi],
target_doi: params[:target_doi],
doi: params[:doi_id] || params[:doi],
orcid: params[:orcid],
prefix: params[:prefix],
Expand All @@ -100,6 +102,8 @@ def index
source_id: params[:source_id],
registrant_id: params[:registrant_id],
relation_type_id: params[:relation_type_id],
source_relation_type_id: params[:source_relation_type_id],
target_relation_type_id: params[:target_relation_type_id],
issn: params[:issn],
publication_year: params[:publication_year],
occurred_at: params[:occurred_at],
Expand All @@ -120,7 +124,7 @@ def index
else
total = response.results.total
total_for_pages = page[:cursor].nil? ? [total.to_f, 10000].min : total.to_f
total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0
total_pages = page[:size].positive? ? (total_for_pages / page[:size]).ceil : 0
end

if page[:scroll].present?
Expand Down Expand Up @@ -247,9 +251,9 @@ def load_event
def set_include
if params[:include].present?
@include = params[:include].split(",").map { |i| i.downcase.underscore.to_sym }
@include = @include & [:dois, :doi_for_source, :doi_for_target]
@include = @include & [:doi_for_source, :doi_for_target]
else
@include = [:doi_for_source, :doi_for_target]
@include = []
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,15 @@ def query(query, options={})
must << { range: { occurred_at: { gte: "#{options[:occurred_at].split("-").min}||/y", lte: "#{options[:occurred_at].split("-").max}||/y", format: "yyyy" }}} if options[:occurred_at].present?
must << { terms: { prefix: options[:prefix].split(",") }} if options[:prefix].present?
must << { terms: { doi: options[:doi].downcase.split(",") }} if options[:doi].present?
must << { terms: { source_doi: options[:source_doi].downcase.split(",") }} if options[:source_doi].present?
must << { terms: { target_doi: options[:target_doi].downcase.split(",") }} if options[:target_doi].present?
must << { terms: { orcid: options[:orcid].split(",") }} if options[:orcid].present?
must << { terms: { isni: options[:isni].split(",") }} if options[:isni].present?
must << { terms: { subtype: options[:subtype].split(",") }} if options[:subtype].present?
must << { terms: { source_id: options[:source_id].split(",") }} if options[:source_id].present?
must << { terms: { relation_type_id: options[:relation_type_id].split(",") }} if options[:relation_type_id].present?
must << { terms: { source_relation_type_id: options[:source_relation_type_id].split(",") }} if options[:source_relation_type_id].present?
must << { terms: { target_relation_type_id: options[:target_relation_type_id].split(",") }} if options[:target_relation_type_id].present?
must << { terms: { registrant_id: options[:registrant_id].split(",") }} if options[:registrant_id].present?
must << { terms: { registrant_id: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { issn: options[:issn].split(",") }} if options[:issn].present?
Expand Down
10 changes: 4 additions & 6 deletions app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ class EventSerializer
set_type :events
set_id :uuid

attributes :subj_id, :obj_id, :source_id, :relation_type_id, :total, :message_action, :source_token, :license, :occurred_at, :timestamp


# has_many :dois, record_type: :dois, serializer: DoiSerializer, id_method_name: :doi do |object|
# load_doi(object)
# end
attributes :subj_id, :obj_id, :source_id, :source_doi, :target_doi, :relation_type_id, :source_relation_type_id, :target_relation_type_id, :total, :message_action, :source_token, :license, :occurred_at, :timestamp

attribute :timestamp, &:updated_at

belongs_to :doi_for_source, record_type: :doi, id_method_name: :source_doi, serializer: DoiSerializer
belongs_to :doi_for_target, record_type: :doi, id_method_name: :target_doi, serializer: DoiSerializer
end
70 changes: 39 additions & 31 deletions spec/requests/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require "rails_helper"

describe "/events", type: :request, elasticsearch: true do
let(:provider) { create(:provider, symbol: "DATACITE") }
let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) }

before(:each) do
allow(Time).to receive(:now).and_return(Time.mktime(2015, 4, 8))
allow(Time.zone).to receive(:now).and_return(Time.mktime(2015, 4, 8))
Expand Down Expand Up @@ -526,44 +529,49 @@
end

context "show" do
let!(:event) { create(:event) }
let(:uri) { "/events/#{event.uuid}" }

# context "as admin user" do
# it "JSON" do
# sleep 1
# get uri, nil, headers
# expect(last_response.body).to eq(200)
let(:doi) { create(:doi, client: client, aasm_state: "findable") }
let(:source_doi) { create(:doi, client: client, aasm_state: "findable") }
let!(:event) { create(:event_for_datacite_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{source_doi.doi}", relation_type_id: "is-referenced-by") }

# response = JSON.parse(last_response.body)
# attributes = response.dig("data", "attributes")
# expect(response.dig("data", "relationships", "subj", "data")).to eq("id"=>event.subj_id, "type"=>"objects")
# end
# end
let(:uri) { "/events/#{event.uuid}" }

# context "as staff user" do
# let(:token) { User.generate_token(role_id: "staff_user") }
before do
Doi.import
Event.import
sleep 1
end

# it "JSON" do
# get uri, nil, headers
# expect(last_response.status).to eq(200)
context "as admin user" do
it "JSON" do
get uri, nil, headers

# response = JSON.parse(last_response.body)
# expect(response.dig("data", "relationships", "subj", "data")).to eq("id"=>event.subj_id, "type"=>"objects")
# end
# end
expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'relationTypeId')).to eq("is-referenced-by")
expect(json.dig('data', 'attributes', 'sourceDoi')).to eq(source_doi.doi.downcase)
expect(json.dig('data', 'attributes', 'targetDoi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'sourceRelationTypeId')).to eq("references")
expect(json.dig('data', 'attributes', 'targetRelationTypeId')).to eq("citations")
expect(json.dig('data', 'relationships', 'doiForSource', 'data')).to eq("id"=>source_doi.doi.downcase, "type"=>"doi")
expect(json.dig('data', 'relationships', 'doiForTarget', 'data')).to eq("id"=>doi.doi.downcase, "type"=>"doi")
end
end

# context "as regular user" do
# let(:token) { User.generate_token(role_id: "user") }
context "as regular user" do
let(:token) { User.generate_token(role_id: "user") }

# it "JSON" do
# get uri, nil, headers
# expect(last_response.status).to eq(200)
it "JSON" do
get uri, nil, headers

# response = JSON.parse(last_response.body)
# expect(response.dig("data", "relationships", "subj", "data")).to eq("id"=>event.subj_id, "type"=>"objects")
# end
# end
expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'relationTypeId')).to eq("is-referenced-by")
expect(json.dig('data', 'attributes', 'sourceDoi')).to eq(source_doi.doi.downcase)
expect(json.dig('data', 'attributes', 'targetDoi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'sourceRelationTypeId')).to eq("references")
expect(json.dig('data', 'attributes', 'targetRelationTypeId')).to eq("citations")
expect(json.dig('data', 'relationships', 'doiForSource', 'data')).to eq("id"=>source_doi.doi.downcase, "type"=>"doi")
expect(json.dig('data', 'relationships', 'doiForTarget', 'data')).to eq("id"=>doi.doi.downcase, "type"=>"doi")
end
end

context "event not found" do
let(:uri) { "/events/#{event.uuid}x" }
Expand Down

0 comments on commit c5d8d7c

Please sign in to comment.