Skip to content

Commit

Permalink
tests for graphql calls. #422
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 22, 2020
1 parent 97847c4 commit 462309f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/graphql/types/doi_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ module DoiItem
argument :style, String, required: false, default_value: "apa"
argument :locale, String, required: false, default_value: "en-US"
end
field :reference_count, Int, null: true, description: "Total number of references."
field :citation_count, Int, null: true, description: "Total number of citations."
field :view_count, Int, null: true, description: "Total number of views."
field :download_count, Int, null: true, description: "Total number of downloads."
field :citations_over_time, [YearTotalType], null: true, description: "Citations by year."
field :views_over_time, [YearMonthTotalType], null: true, description: "Views by month."
field :downloads_over_time, [YearMonthTotalType], null: true, description: "Downloads by month."

field :citations, [CreativeWorkType], null: true, description: "Citations."
field :references, [CreativeWorkType], null: true, description: "References."

def type
object.types["schemaOrg"]
end
Expand Down Expand Up @@ -80,6 +83,16 @@ def formatted_citation(style: nil, locale: nil)
end
bibliography.first.gsub(url, doi_link(url))
end

def citations
ids = object.citation_events.map {|e| e.source_doi }
ElasticsearchLoader.for(Doi).load_many(ids)
end

def references
ids = object.reference_events.map { |e| e.target_doi }
ElasticsearchLoader.for(Doi).load_many(ids)
end

def doi_link(url)
"<a href='#{url}'>#{url}</a>"
Expand Down
3 changes: 3 additions & 0 deletions spec/graphql/types/doi_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
it { is_expected.to have_field(:publicationYear).of_type("Int") }
it { is_expected.to have_field(:publisher).of_type("String") }
it { is_expected.to have_field(:citationCount).of_type("Int") }
it { is_expected.to have_field(:referenceCount).of_type("Int") }
it { is_expected.to have_field(:viewCount).of_type("Int") }
it { is_expected.to have_field(:downloadCount).of_type("Int") }
it { is_expected.to have_field(:citationsOverTime).of_type("[YearTotal!]") }
it { is_expected.to have_field(:viewsOverTime).of_type("[YearMonthTotal!]") }
it { is_expected.to have_field(:downloadsOverTime).of_type("[YearMonthTotal!]") }
it { is_expected.to have_field(:citations).of_type("[DoiItem!]") }
it { is_expected.to have_field(:references).of_type("[DoiItem!]") }
end
end
49 changes: 48 additions & 1 deletion spec/graphql/types/query_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
end
end

describe "query for citationCount", elasticsearch: true do
describe "query with citations", elasticsearch: true do
let(:client) { create(:client) }
let(:doi) { create(:doi, client: client, aasm_state: "findable") }
let(:source_doi) { create(:doi, client: client, aasm_state: "findable") }
Expand All @@ -65,6 +65,10 @@
year
total
}
citations {
id
publicationYear
}
}
}
})
Expand All @@ -77,6 +81,49 @@
expect(response.dig("data", "datasets", "nodes").length).to eq(3)
expect(response.dig("data", "datasets", "nodes", 0, "citationCount")).to eq(2)
expect(response.dig("data", "datasets", "nodes", 0, "citationsOverTime")).to eq([{"total"=>1, "year"=>2015}, {"total"=>1, "year"=>2016}])
expect(response.dig("data", "datasets", "nodes", 0, "citations").length).to eq(2)
expect(response.dig("data", "datasets", "nodes", 0, "citations").first).to eq("id"=>"https://handle.test.datacite.org/#{source_doi.doi.downcase}", "publicationYear"=>2011)
end
end

describe "query with references", elasticsearch: true do
let(:client) { create(:client) }
let(:doi) { create(:doi, client: client, aasm_state: "findable") }
let(:target_doi) { create(:doi, client: client, aasm_state: "findable") }
let(:target_doi2) { create(:doi, client: client, aasm_state: "findable") }
let!(:reference_event) { create(:event_for_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{target_doi.doi}", relation_type_id: "references") }
let!(:reference_event2) { create(:event_for_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{target_doi2.doi}", relation_type_id: "references") }

before do
Doi.import
Event.import
sleep 1
end

let(:query) do
%(query {
datasets {
totalCount
nodes {
id
referenceCount
references {
id
publicationYear
}
}
}
})
end

it "returns all datasets with counts" do
response = LupoSchema.execute(query).as_json

expect(response.dig("data", "datasets", "totalCount")).to eq(3)
expect(response.dig("data", "datasets", "nodes").length).to eq(3)
expect(response.dig("data", "datasets", "nodes", 0, "referenceCount")).to eq(2)
expect(response.dig("data", "datasets", "nodes", 0, "references").length).to eq(2)
expect(response.dig("data", "datasets", "nodes", 0, "references").first).to eq("id"=>"https://handle.test.datacite.org/#{target_doi.doi.downcase}", "publicationYear"=>2011)
end
end
end

0 comments on commit 462309f

Please sign in to comment.