Skip to content

Commit

Permalink
added initial test support for graphql. #401
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 26, 2020
1 parent 9c858e9 commit 8a72165
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ gem 'strong_migrations', '~> 0.6.0'
group :development, :test do
gem "rspec-rails", "~> 3.8", ">= 3.8.2"
gem "rspec-benchmark", "~> 0.4.0"
gem 'rubocop', '~> 0.77.0'
gem "rspec-graphql_matchers", "~> 1.1"
gem "rubocop", "~> 0.77.0"
gem 'rubocop-performance', '~> 1.5', '>= 1.5.1'
gem 'rubocop-rails', '~> 2.4'
gem "rubocop-rails", "~> 2.4"
gem "better_errors"
gem "binding_of_caller"
gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ GEM
rspec-expectations (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-graphql_matchers (1.1)
graphql (>= 1.8, < 2.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
Expand All @@ -468,7 +470,7 @@ GEM
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-performance (1.5.2)
rubocop (>= 0.71.0)
rubocop-rails (2.4.1)
rubocop-rails (2.4.2)
rack (>= 1.1)
rubocop (>= 0.72.0)
ruby-enum (0.7.2)
Expand Down Expand Up @@ -618,6 +620,7 @@ DEPENDENCIES
rails (~> 5.2.0)
rake (~> 12.0)
rspec-benchmark (~> 0.4.0)
rspec-graphql_matchers (~> 1.1)
rspec-rails (~> 3.8, >= 3.8.2)
rubocop (~> 0.77.0)
rubocop-performance (~> 1.5, >= 1.5.1)
Expand Down
42 changes: 41 additions & 1 deletion app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3962,7 +3962,26 @@ type Query {
query: String
): PublicationConnectionWithMeta!
service(id: ID!): Service!
services(first: Int = 25, query: String): [Service!]!
services(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String

"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
clientId: String
first: Int = 25

"""
Returns the last _n_ elements from the list.
"""
last: Int
providerId: String
query: String
): ServiceConnectionWithMeta!
softwareSourceCode(id: ID!): Software!
softwareSourceCodes(
"""
Expand Down Expand Up @@ -4235,6 +4254,27 @@ type Service implements DoiItem {
version: String
}

"""
The connection type for Dataset.
"""
type ServiceConnectionWithMeta {
"""
A list of edges.
"""
edges: [DatasetEdge]

"""
A list of nodes.
"""
nodes: [Dataset]

"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
totalCount: Int!
}

type Software implements DoiItem & MetricInterface {
"""
The count of DOI events that represents citations
Expand Down
4 changes: 3 additions & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ def physical_object(id:)
set_doi(id)
end

field :services, [ServiceType], null: false do
field :services, ServiceConnectionWithMetaType, null: false, connection: true, max_page_size: 1000 do
argument :query, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :first, Int, required: false, default_value: 25
end

Expand Down
9 changes: 9 additions & 0 deletions spec/graphql/printout_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "rails_helper"

describe LupoSchema do
it "printout is up-to-date" do
current_defn = LupoSchema.to_definition
printout_defn = File.read(Rails.root.join("app/graphql/schema.graphql"))
expect(current_defn).to eq(printout_defn)
end
end
14 changes: 14 additions & 0 deletions spec/graphql/types/doi_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "rails_helper"

describe DoiItem do
describe "fields" do
subject { described_class }

it { is_expected.to have_field(:id).of_type(!types.ID) }
it { is_expected.to have_field(:type).of_type("String!") }
it { is_expected.to have_field(:creators).of_type("[Person!]") }
it { is_expected.to have_field(:titles).of_type("[Title!]") }
it { is_expected.to have_field(:publicationYear).of_type("Int") }
it { is_expected.to have_field(:publisher).of_type("String") }
end
end
42 changes: 42 additions & 0 deletions spec/graphql/types/query_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "rails_helper"

describe QueryType do
describe "fields" do
subject { described_class }

it { is_expected.to have_field(:dataset).of_type("Dataset!") }
it { is_expected.to have_field(:datasets).of_type("DatasetConnectionWithMeta!") }
it { is_expected.to have_field(:publication).of_type("Publication!") }
it { is_expected.to have_field(:publications).of_type("PublicationConnectionWithMeta!") }
it { is_expected.to have_field(:service).of_type("Service!") }
it { is_expected.to have_field(:services).of_type("ServiceConnectionWithMeta!") }
end

describe "query", elasticsearch: true do
let!(:datasets) { create_list(:doi, 3, aasm_state: "findable") }

before do
Doi.import
sleep 1
end

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

it "returns all datasets" 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, "id")).to eq(datasets.first.identifier)
end
end
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
config.include StripAttributes::Matchers
config.include RSpec::Benchmark::Matchers
config.include Rack::Test::Methods, type: :request
config.include RSpec::GraphqlMatchers::TypesHelper

# don't use transactions, use database_clear gem via support file
config.use_transactional_fixtures = false

Expand Down

0 comments on commit 8a72165

Please sign in to comment.