Skip to content

Commit

Permalink
fix activities api. #533
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 28, 2020
1 parent a31bc31 commit 7f4fe69
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def index
response = Activity.find_by_id(params[:ids], page: page, sort: sort)
else
response = Activity.query(params[:query],
uid: params[:doi_id] || params[:provider_id] || params[:client_id] || params[:repository_id],
uid: params[:datacite_doi_id] || params[:provider_id] || params[:client_id] || params[:repository_id],
page: page,
sort: sort,
scroll_id: params[:scroll_id])
Expand Down
4 changes: 4 additions & 0 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def as_indexed_json(options={})
}
end

def self.query_fields
['uid^10', 'username^5', 'action', 'changes', 'was_derived_from', 'was_attributed_to', 'was_generated_by']
end

def self.query_aggregations
{}
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ def query(query, options={})
filter << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { terms: { client_id: options[:client_id].split(",") }} if options[:client_id].present?
filter << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present?
elsif self.name == "Activity"
if query.present?
must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }]
else
must = [{ match_all: {} }]
end

filter << { terms: { uid: options[:uid].to_s.split(",") }} if options[:uid].present?
end

# ES query can be optionally defined in different ways
Expand Down
87 changes: 86 additions & 1 deletion spec/requests/activities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

describe "activities for doi", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }
let!(:other_doi) { create(:doi, client: client) }

before do
DataciteDoi.import
Expand All @@ -18,9 +19,93 @@

context "without username" do
it "returns the activities" do
get "/activities", nil, headers
get "/dois/#{doi.doi.downcase}/activities", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("data").length).to eq(1)
expect(json.dig("meta", "total")).to eq(1)
expect(json.dig("data", 0, "attributes", "action")).to eq("create")
expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft")

expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil
expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present
expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present
expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present
end
end
end

describe "activities for repository", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }
let!(:other_doi) { create(:doi, client: client) }

before do
DataciteDoi.import
Activity.import
sleep 2
end

context "repository" do
it "returns the activities" do
get "/repositories/#{client.symbol.downcase}/activities", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("data").length).to eq(1)
expect(json.dig("meta", "total")).to eq(1)
expect(json.dig("data", 0, "attributes", "action")).to eq("create")

expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil
expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present
expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present
expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present
end
end
end

describe "activities for provider", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }
let!(:other_doi) { create(:doi, client: client) }

before do
DataciteDoi.import
Activity.import
sleep 2
end

context "provider" do
it "returns the activities" do
get "/providers/#{provider.symbol.downcase}/activities", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("data").length).to eq(1)
expect(json.dig("meta", "total")).to eq(1)
expect(json.dig("data", 0, "attributes", "action")).to eq("create")

expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil
expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present
expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present
expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present
end
end
end

describe "query activities", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }
let!(:other_doi) { create(:doi, client: client) }

before do
DataciteDoi.import
Activity.import
sleep 2
end

context "query" do
it "returns the activities" do
get "/activities?query=#{doi.doi.downcase}", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("data").length).to eq(1)
expect(json.dig("meta", "total")).to eq(1)
expect(json.dig("data", 0, "attributes", "action")).to eq("create")
expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft")

Expand Down

0 comments on commit 7f4fe69

Please sign in to comment.