From 8a72165b7456bfecd5a64cfde5a7b0d92a759608 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 26 Jan 2020 20:46:26 +0100 Subject: [PATCH] added initial test support for graphql. #401 --- Gemfile | 5 ++-- Gemfile.lock | 5 +++- app/graphql/schema.graphql | 42 ++++++++++++++++++++++++++- app/graphql/types/query_type.rb | 4 ++- spec/graphql/printout_spec.rb | 9 ++++++ spec/graphql/types/doi_item_spec.rb | 14 +++++++++ spec/graphql/types/query_type_spec.rb | 42 +++++++++++++++++++++++++++ spec/rails_helper.rb | 2 ++ 8 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 spec/graphql/printout_spec.rb create mode 100644 spec/graphql/types/doi_item_spec.rb create mode 100644 spec/graphql/types/query_type_spec.rb diff --git a/Gemfile b/Gemfile index b6cc70dd8..e26ed9879 100644 --- a/Gemfile +++ b/Gemfile @@ -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] diff --git a/Gemfile.lock b/Gemfile.lock index c1015fe5a..f4f82ec9f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 7f83f8dd9..3363b108b 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -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( """ @@ -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 diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 26f17227b..f679a526f 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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 diff --git a/spec/graphql/printout_spec.rb b/spec/graphql/printout_spec.rb new file mode 100644 index 000000000..d830cfa7e --- /dev/null +++ b/spec/graphql/printout_spec.rb @@ -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 diff --git a/spec/graphql/types/doi_item_spec.rb b/spec/graphql/types/doi_item_spec.rb new file mode 100644 index 000000000..5f5893dee --- /dev/null +++ b/spec/graphql/types/doi_item_spec.rb @@ -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 diff --git a/spec/graphql/types/query_type_spec.rb b/spec/graphql/types/query_type_spec.rb new file mode 100644 index 000000000..8311b2ad8 --- /dev/null +++ b/spec/graphql/types/query_type_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 7722396d5..070cc31b2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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