From 4d51683e10082c3493be349597ff4731474c24a8 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 22 Aug 2022 10:06:57 -0400 Subject: [PATCH 1/7] Add subjects field to client/repository model --- db/migrate/20220809193636_add_subjects_to_datacentre.rb | 5 +++++ db/schema.rb | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20220809193636_add_subjects_to_datacentre.rb diff --git a/db/migrate/20220809193636_add_subjects_to_datacentre.rb b/db/migrate/20220809193636_add_subjects_to_datacentre.rb new file mode 100644 index 000000000..83e000016 --- /dev/null +++ b/db/migrate/20220809193636_add_subjects_to_datacentre.rb @@ -0,0 +1,5 @@ +class AddSubjectsToDatacentre < ActiveRecord::Migration[6.1] + def change + add_column :datacentre, :subjects, :json + end +end diff --git a/db/schema.rb b/db/schema.rb index 7b7950a34..a7e26c4ae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,7 +9,9 @@ # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_07_07_142354) do + +ActiveRecord::Schema.define(version: 2022_08_09_193636) do + create_table "active_storage_attachments", charset: "utf8mb4", force: :cascade do |t| t.string "name", limit: 191, null: false t.string "record_type", null: false @@ -168,6 +168,7 @@ t.json "service_contact" t.string "globus_uuid", limit: 191 t.text "analytics_dashboard_url" + t.json "subjects" t.index ["allocator"], name: "FK6695D60546EBD781" t.index ["globus_uuid"], name: "index_datacentre_on_globus_uuid" t.index ["re3data_id"], name: "index_datacentre_on_re3data_id" From 1cfd4c9966e33667f8b69e24992845edf75760a2 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 24 Oct 2022 11:07:38 -0400 Subject: [PATCH 2/7] Add json schema validation for client.subjects. Use it in client_spec --- Gemfile | 1 + Gemfile.lock | 92 +++++++++++-------- app/models/client.rb | 15 +++ app/models/schemas/client/subjects.json | 23 +++++ ...220809193636_add_subjects_to_datacentre.rb | 2 + db/schema.rb | 3 +- spec/models/client_spec.rb | 46 ++++++++++ 7 files changed, 145 insertions(+), 37 deletions(-) create mode 100644 app/models/schemas/client/subjects.json diff --git a/Gemfile b/Gemfile index b3b4a22b6..31564b0b0 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ source "https://rubygems.org" gem "aasm", "~> 5.0", ">= 5.0.1" gem "active_model_serializers", "~> 0.10.0" +gem "activerecord_json_validator", "~> 1.3" gem "apollo-federation", "1.1.3" gem "audited", "~> 4.8" gem "aws-sdk-s3", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 3a2503d2b..edc67b6a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,9 @@ GEM activerecord (6.1.7) activemodel (= 6.1.7) activesupport (= 6.1.7) + activerecord_json_validator (1.3.0) + activerecord (>= 4.2.0, < 7) + json-schema (~> 2.8) activestorage (6.1.7) actionpack (= 6.1.7) activejob (= 6.1.7) @@ -78,20 +81,20 @@ GEM audited (4.10.0) activerecord (>= 4.2, < 6.2) aws-eventstream (1.2.0) - aws-partitions (1.653.0) - aws-sdk-core (3.166.0) + aws-partitions (1.690.0) + aws-sdk-core (3.168.4) aws-eventstream (~> 1, >= 1.0.2) aws-partitions (~> 1, >= 1.651.0) aws-sigv4 (~> 1.5) jmespath (~> 1, >= 1.6.1) - aws-sdk-kms (1.59.0) + aws-sdk-kms (1.61.0) aws-sdk-core (~> 3, >= 3.165.0) aws-sigv4 (~> 1.1) - aws-sdk-s3 (1.117.1) + aws-sdk-s3 (1.117.2) aws-sdk-core (~> 3, >= 3.165.0) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.4) - aws-sdk-sqs (1.52.0) + aws-sdk-sqs (1.52.1) aws-sdk-core (~> 3, >= 3.165.0) aws-sigv4 (~> 1.1) aws-sigv4 (1.5.2) @@ -134,7 +137,7 @@ GEM rdf-rdfxml (~> 3.1) rdf-turtle (~> 3.1) thor (>= 0.19) - bootsnap (1.13.0) + bootsnap (1.15.0) msgpack (~> 1.2) builder (3.2.4) bullet (6.1.5) @@ -175,7 +178,7 @@ GEM crack (0.4.5) rexml crass (1.0.6) - crawler_detect (1.2.1) + crawler_detect (1.2.2) qonfig (~> 0.24) csl (1.6.0) namae (~> 1.0) @@ -191,6 +194,7 @@ GEM activerecord (>= 5.a) database_cleaner-core (~> 2.0.0) database_cleaner-core (2.0.1) + date (3.3.3) ddtrace (0.32.0) msgpack debug_inspector (1.1.0) @@ -228,7 +232,7 @@ GEM multi_json equivalent-xml (0.6.0) nokogiri (>= 1.4.3) - erubi (1.11.0) + erubi (1.12.0) excon (0.71.1) facets (3.1.0) factory_bot (4.11.1) @@ -238,7 +242,7 @@ GEM railties (>= 3.0.0) faker (1.9.6) i18n (>= 0.7) - faraday (0.17.5) + faraday (0.17.6) multipart-post (>= 1.2, < 3) faraday-encoding (0.0.5) faraday @@ -256,7 +260,7 @@ GEM flipper (~> 0.17.2) gender_detector (0.1.2) unicode_utils (>= 1.3.0) - git (1.12.0) + git (1.13.0) addressable (~> 2.8) rchardet (~> 1.8) globalid (1.0.0) @@ -287,11 +291,11 @@ GEM i18n (1.12.0) concurrent-ruby (~> 1.0) i18n_data (0.8.0) - iso-639 (0.3.5) + iso-639 (0.3.6) iso8601 (0.9.1) - jmespath (1.6.1) - json (2.6.2) - json-canonicalization (0.3.0) + jmespath (1.6.2) + json (2.6.3) + json-canonicalization (0.3.1) json-ld (3.2.3) htmlentities (~> 4.3) json-canonicalization (~> 0.3) @@ -302,11 +306,13 @@ GEM json-ld-preloaded (3.2.2) json-ld (~> 3.2) rdf (~> 3.2) + json-schema (2.8.1) + addressable (>= 2.4) jsonapi-renderer (0.2.2) jsonlint (0.3.0) oj (~> 3) optimist (~> 3) - jwt (2.5.0) + jwt (2.6.0) kaminari (1.2.2) activesupport (>= 4.1.0) kaminari-actionview (= 1.2.2) @@ -331,7 +337,7 @@ GEM rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - logger (1.5.1) + logger (1.5.3) lograge (0.11.2) actionpack (>= 4) activesupport (>= 4) @@ -345,9 +351,12 @@ GEM nokogiri (>= 1.5.9) macaddr (1.7.2) systemu (~> 2.6.5) - mail (2.7.1) + mail (2.8.0) mini_mime (>= 0.1.1) - mailgun-ruby (1.2.5) + net-imap + net-pop + net-smtp + mailgun-ruby (1.2.6) rest-client (>= 2.0.2) marcel (1.0.2) maremma (4.9.8) @@ -369,9 +378,9 @@ GEM mimemagic (0.3.10) nokogiri (~> 1) rake - mini_magick (4.11.0) + mini_magick (4.12.0) mini_mime (1.1.2) - minitest (5.16.3) + minitest (5.17.0) money (6.16.0) i18n (>= 0.6.4, <= 2) msgpack (1.6.0) @@ -381,6 +390,15 @@ GEM namae (1.1.1) net-http-persistent (4.0.1) connection_pool (~> 2.2) + net-imap (0.3.4) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.1) + timeout + net-smtp (0.3.3) + net-protocol netrc (0.11.0) nio4r (2.5.8) nokogiri (1.13.10-arm64-darwin) @@ -389,13 +407,13 @@ GEM racc (~> 1.4) nokogiri (1.13.10-x86_64-linux) racc (~> 1.4) - oj (3.13.21) + oj (3.13.23) oj_mimic_json (1.0.1) optimist (3.0.1) parallel (1.22.1) parallel_tests (3.13.0) parallel - parser (3.1.2.1) + parser (3.2.0.0) ast (~> 2.4.1) postrank-uri (1.0.24) addressable (>= 2.4.0) @@ -411,8 +429,8 @@ GEM docopt (~> 0.5) sysrandom qonfig (0.28.0) - racc (1.6.1) - rack (2.2.4) + racc (1.6.2) + rack (2.2.5) rack-accept (0.4.5) rack (>= 0.4) rack-cors (1.1.1) @@ -477,7 +495,7 @@ GEM rdf-xsd (3.2.1) rdf (~> 3.2) rexml (~> 3.2) - regexp_parser (2.6.0) + regexp_parser (2.6.1) request_store (1.5.1) rack (>= 1.4) rest-client (2.1.0) @@ -514,28 +532,28 @@ GEM rspec-mocks (~> 3.9.0) rspec-support (~> 3.9.0) rspec-support (3.9.4) - rubocop (1.37.1) + rubocop (1.43.0) json (~> 2.3) parallel (~> 1.10) - parser (>= 3.1.2.1) + parser (>= 3.2.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.23.0, < 2.0) + rubocop-ast (>= 1.24.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.23.0) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.24.1) parser (>= 3.1.1.0) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) - rubocop-performance (1.15.0) + rubocop-performance (1.15.2) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) - rubocop-rails (2.17.2) + rubocop-rails (2.17.4) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.33.0, < 2.0) - rubocop-rspec (2.14.2) + rubocop-rspec (2.16.0) rubocop (~> 1.33) ruby-progressbar (1.11.0) ruby_dep (1.5.0) @@ -560,7 +578,7 @@ GEM slack-notifier (2.4.0) sort_alphabetical (1.1.0) unicode_utils (>= 1.2.2) - sparql (3.2.4) + sparql (3.2.5) builder (~> 3.2) ebnf (~> 2.2, >= 2.3.1) logger (~> 1.5) @@ -600,6 +618,7 @@ GEM test-prof (0.10.2) thor (1.2.1) tilt (2.0.11) + timeout (0.3.1) turnout (2.5.0) i18n (>= 0.7, < 2) rack (>= 1.3, < 3) @@ -610,7 +629,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.8.2) - unicode-display_width (2.3.0) + unicode-display_width (2.4.2) unicode-types (1.8.0) unicode_utils (1.4.0) uniform_notifier (1.16.0) @@ -626,7 +645,7 @@ GEM websocket-extensions (0.1.5) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (2.6.2) + zeitwerk (2.6.6) PLATFORMS universal-darwin-21 @@ -635,6 +654,7 @@ PLATFORMS DEPENDENCIES aasm (~> 5.0, >= 5.0.1) active_model_serializers (~> 0.10.0) + activerecord_json_validator (~> 1.3) apollo-federation (= 1.1.3) audited (~> 4.8) aws-sdk-s3 diff --git a/app/models/client.rb b/app/models/client.rb index b6382e092..817083a39 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class Client < ApplicationRecord + SUBJECTS_JSON_SCHEMA = "#{Rails.root}/app/models/schemas/client/subjects.json" audited except: %i[ system_email service_contact @@ -51,6 +52,12 @@ class Client < ApplicationRecord attr_accessor :password_input, :target_id attr_reader :from_salesforce + validates :subjects, if: :subjects?, + json: { + message: ->(errors) { errors }, + schema: SUBJECTS_JSON_SCHEMA + } + validates_presence_of :symbol, :name, :system_email validates_uniqueness_of :symbol, message: "This Client ID has already been taken" @@ -188,6 +195,14 @@ class Client < ApplicationRecord indexes :deleted_at, type: :date indexes :analytics_dashboard_url, type: :text indexes :cumulative_years, type: :integer, index: "false" + indexes :subjects, type: :object, properties: { + subjectScheme: { type: :keyword }, + subject: { type: :keyword }, + schemeUri: { type: :keyword }, + valueUri: { type: :keyword }, + lang: { type: :keyword }, + classificationCode: { type: :keyword }, + } # include parent objects indexes :provider, diff --git a/app/models/schemas/client/subjects.json b/app/models/schemas/client/subjects.json new file mode 100644 index 000000000..d53fe884e --- /dev/null +++ b/app/models/schemas/client/subjects.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "array", + "minItems": 0, + "items": { + "type": "object", + "properties": { + "classificationCode": { "type": "string" }, + "schemeUri": { "type": "string" }, + "valueUri": { "type": "string" }, + "lang": { "type": "string" }, + "subject": { "type": "string" }, + "subjectScheme": { "type": "string" } + }, + "required": [ + "classificationCode", + "schemeUri", + "subject", + "subjectScheme" + ], + "additionalProperties": false + } +} diff --git a/db/migrate/20220809193636_add_subjects_to_datacentre.rb b/db/migrate/20220809193636_add_subjects_to_datacentre.rb index 83e000016..20a15864f 100644 --- a/db/migrate/20220809193636_add_subjects_to_datacentre.rb +++ b/db/migrate/20220809193636_add_subjects_to_datacentre.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddSubjectsToDatacentre < ActiveRecord::Migration[6.1] def change add_column :datacentre, :subjects, :json diff --git a/db/schema.rb b/db/schema.rb index a7e26c4ae..cb5e5d0c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,7 +13,6 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2022_08_09_193636) do - create_table "active_storage_attachments", charset: "utf8mb4", force: :cascade do |t| t.string "name", limit: 191, null: false t.string "record_type", null: false diff --git a/spec/models/client_spec.rb b/spec/models/client_spec.rb index f713d2bc4..3bc4ea790 100644 --- a/spec/models/client_spec.rb +++ b/spec/models/client_spec.rb @@ -14,6 +14,52 @@ it { is_expected.to strip_attribute(:domains) } end + describe "subjects" do + let(:client) { build(:client, provider: provider) } + let(:valid_subjects) do + { + classificationCode: "6.1", + schemeUri: "http://www.oecd.org/science/inno/38235147.pdf", + subject: "History and archaeology", + subjectScheme: "Fields of Science and Technology (FOS)", + } + end + + it "valid hash" do + client.subjects = [valid_subjects] + expect(client).to be_valid + expect(client.errors.details).to be_empty + expect(client.subjects).to eq([ + valid_subjects.with_indifferent_access + ]) + end + + it "invalid hash if missing classificationCode" do + client.subjects = [valid_subjects.except(:classificationCode)] + expect(client).to_not be_valid + end + + it "invalid hash if missing subject" do + client.subjects = [valid_subjects.except(:subject)] + expect(client).to_not be_valid + end + + it "invalid hash if missing schemeUri" do + client.subjects = [valid_subjects.except(:schemeUri)] + expect(client).to_not be_valid + end + + it "invalid hash if missing subjectScheme" do + client.subjects = [valid_subjects.except(:subjectScheme)] + expect(client).to_not be_valid + end + + it "invalid hash if missing classificationCode" do + client.subjects = [valid_subjects.except(:classificationCode)] + expect(client).to_not be_valid + end + end + describe "to_jsonapi" do xit "works" do params = client.to_jsonapi From 786bfec10bf944b377e62601f3f7b3da026e0fb6 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Thu, 3 Nov 2022 10:18:25 -0400 Subject: [PATCH 3/7] Remove commented code in repositories_spec --- spec/requests/repositories_spec.rb | 34 ------------------------------ 1 file changed, 34 deletions(-) diff --git a/spec/requests/repositories_spec.rb b/spec/requests/repositories_spec.rb index 5e0a55e44..e3a86632d 100644 --- a/spec/requests/repositories_spec.rb +++ b/spec/requests/repositories_spec.rb @@ -86,36 +86,6 @@ end end - # # Test suite for GET /clients - # describe 'GET /clients query' do - # before { get "/clients?query=#{query}", headers: headers } - # - # it 'returns clients' do - # expect(json).not_to be_empty - # expect(json['data'].size).to eq(11) - # end - # - # it 'returns status code 200' do - # expect(response).to have_http_status(200) - # end - # end - - # describe 'GET /clients?ids=', elasticsearch: true do - # before do - # sleep 1 - # get "/clients?ids=#{ids}", headers: headers - # end - - # it 'returns clients' do - # expect(json).not_to be_empty - # expect(json['data'].size).to eq(10) - # end - - # it 'returns status code 200' do - # expect(response).to have_http_status(200) - # end - # end - describe "GET /repositories/:id" do context "when the record exists" do it "returns the repository" do @@ -634,8 +604,6 @@ sleep 1 expect(last_response.status).to eq(200) - # expect(Doi.query(nil, client_id: client.symbol.downcase).results.total).to eq(0) - # expect(Doi.query(nil, client_id: target.symbol.downcase).results.total).to eq(3) end it "transfered all DOIs consortium" do @@ -644,8 +612,6 @@ sleep 1 expect(last_response.status).to eq(200) - # expect(Doi.query(nil, client_id: client.symbol.downcase).results.total).to eq(0) - # expect(Doi.query(nil, client_id: target.symbol.downcase).results.total).to eq(3) end end end From d0ebdb9808e6c91f09bca2da8741bb7ffc5c1809 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Thu, 17 Nov 2022 15:46:53 -0500 Subject: [PATCH 4/7] Connect client.subjects with repositories REST endpoint --- app/controllers/repositories_controller.rb | 5 +++ app/models/schemas/client/subjects.json | 38 +++++++++++++++------- app/serializers/repository_serializer.rb | 17 +++++++++- spec/requests/repositories_spec.rb | 33 +++++++++++++++++++ 4 files changed, 81 insertions(+), 12 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index a1b1d5570..b3fdc11a5 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -369,6 +369,8 @@ def safe_params "salesforceId", "fromSalesforce", "analyticsDashboardUrl", + :subjects, + { subjects: %i[subject subjectScheme schemeUri valueUri lang classificationCode] }, ], keys: { "systemEmail" => :system_email, @@ -383,6 +385,9 @@ def safe_params "repositoryType" => :repository_type, "serviceContact" => :service_contact, "analyticsDashboardUrl" => :analytics_dashboard_url, + "subjectScheme" => :subject_scheme, + "valueUri" => :value_uri, + "classificationCode" => :classification_code, }, ) end diff --git a/app/models/schemas/client/subjects.json b/app/models/schemas/client/subjects.json index d53fe884e..7179c2243 100644 --- a/app/models/schemas/client/subjects.json +++ b/app/models/schemas/client/subjects.json @@ -5,18 +5,34 @@ "items": { "type": "object", "properties": { - "classificationCode": { "type": "string" }, - "schemeUri": { "type": "string" }, - "valueUri": { "type": "string" }, - "lang": { "type": "string" }, - "subject": { "type": "string" }, - "subjectScheme": { "type": "string" } + "classification_code": { "type": "string" }, + "classificationCode": { "type": "string" }, + "scheme_uri": { "type": "string" }, + "schemeUri": { "type": "string" }, + "value_uri": { "type": "string" }, + "valueUri": { "type": "string" }, + "lang": { "type": "string" }, + "subject": { "type": "string" }, + "subject_scheme": { "type": "string" }, + "subjectScheme": { "type": "string" } }, - "required": [ - "classificationCode", - "schemeUri", - "subject", - "subjectScheme" + "oneOf": [ + { + "required": [ + "classification_code", + "scheme_uri", + "subject", + "subject_scheme" + ] + }, + { + "required": [ + "classificationCode", + "schemeUri", + "subject", + "subjectScheme" + ] + } ], "additionalProperties": false } diff --git a/app/serializers/repository_serializer.rb b/app/serializers/repository_serializer.rb index 001f8ea34..6765db9b8 100644 --- a/app/serializers/repository_serializer.rb +++ b/app/serializers/repository_serializer.rb @@ -28,7 +28,8 @@ class RepositorySerializer :from_salesforce, :created, :updated, - :analytics_dashboard_url + :analytics_dashboard_url, + :subjects belongs_to :provider, record_type: :providers has_many :prefixes, record_type: :prefixes @@ -96,6 +97,20 @@ class RepositorySerializer end end + attribute :subjects, + + Proc.new { |object, params| } do |object| + if object.subjects? + Array.wrap(object.subjects).map { |subject| + subject.transform_keys! do |key| + key.to_s.camelcase(:lower) + end + } + else + [] + end + end + attribute :globus_uuid, if: Proc.new { |object, params| diff --git a/spec/requests/repositories_spec.rb b/spec/requests/repositories_spec.rb index e3a86632d..c9f9a9717 100644 --- a/spec/requests/repositories_spec.rb +++ b/spec/requests/repositories_spec.rb @@ -367,6 +367,39 @@ end end + context "update subjects" do + let(:subjects) do + [{ "subject" => "80505 Web Technologies (excl. Web Search)", + "schemeUri" => "http://www.abs.gov.au/ausstats/abs@.nsf/0/6BB427AB9696C225CA2574180004463E", + "subjectScheme" => "FOR", + "lang" => "en", + "classificationCode" => "001" }] + end + let(:update_attributes) do + { + "data" => { + "type" => "repositories", + "attributes" => { + "subjects" => subjects, + }, + }, + } + end + + it "updates the repository" do + put "/repositories/#{client.symbol}", update_attributes, headers + expect(json.dig("data", "attributes", "subjects")).to eq([ + { "lang" => "en", + "schemeUri" => "http://www.abs.gov.au/ausstats/abs@.nsf/0/6BB427AB9696C225CA2574180004463E", + "subject" => "80505 Web Technologies (excl. Web Search)", + "subjectScheme" => "FOR", + "classificationCode" => "001" + } + ]) + end + end + + context "transfer repository" do let(:bearer) { User.generate_token } let(:staff_headers) do From 353903adda2ac46ee7dea640e48b9f67254586ba Mon Sep 17 00:00:00 2001 From: jrhoads Date: Tue, 10 Jan 2023 17:04:11 -0500 Subject: [PATCH 5/7] Auto-update some specs for the current year --- spec/concerns/countable_spec.rb | 63 +++++++--------------- spec/graphql/types/repository_type_spec.rb | 5 +- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/spec/concerns/countable_spec.rb b/spec/concerns/countable_spec.rb index c656862be..d59ec7759 100644 --- a/spec/concerns/countable_spec.rb +++ b/spec/concerns/countable_spec.rb @@ -4,6 +4,17 @@ describe "Providers", type: :controller, elasticsearch: true do subject { ProvidersController.new } + before(:all) do + current_year = Date.today.year + @CUMULTATIVE_BY_YEAR = (2015..current_year).to_a.map do |year| + { "count" => 3, "id" => "#{year}", "title" => "#{year}" } + end + @CUMULTATIVE_BY_YEAR_WITH_DELETES = (2015..2017).to_a.map do |year| + { "count" => 2, "id" => "#{year}", "title" => "#{year}" } + end | (2018..current_year).to_a.map do |year| + { "count" => 1, "id" => "#{year}", "title" => "#{year}" } + end + end describe "provider_count" do before do @@ -14,17 +25,8 @@ it "counts all providers" do Provider.import sleep 2 - expect(subject.provider_count).to eq( - [ - { "count" => 3, "id" => "2015", "title" => "2015" }, - { "count" => 3, "id" => "2016", "title" => "2016" }, - { "count" => 3, "id" => "2017", "title" => "2017" }, - { "count" => 3, "id" => "2018", "title" => "2018" }, - { "count" => 3, "id" => "2019", "title" => "2019" }, - { "count" => 3, "id" => "2020", "title" => "2020" }, - { "count" => 3, "id" => "2021", "title" => "2021" }, - { "count" => 3, "id" => "2022", "title" => "2022" }, - ], + expect(subject.provider_count).to match_array( + @CUMULTATIVE_BY_YEAR ) end @@ -33,17 +35,8 @@ @providers.last.update(deleted_at: "2015-06-14") Provider.import sleep 2 - expect(subject.provider_count).to eq( - [ - { "count" => 1, "id" => "2018", "title" => "2018" }, - { "count" => 1, "id" => "2019", "title" => "2019" }, - { "count" => 1, "id" => "2020", "title" => "2020" }, - { "count" => 1, "id" => "2021", "title" => "2021" }, - { "count" => 1, "id" => "2022", "title" => "2022" }, - { "count" => 2, "id" => "2015", "title" => "2015" }, - { "count" => 2, "id" => "2016", "title" => "2016" }, - { "count" => 2, "id" => "2017", "title" => "2017" }, - ], + expect(subject.provider_count).to match_array( + @CUMULTATIVE_BY_YEAR_WITH_DELETES ) end end @@ -57,17 +50,8 @@ it "counts all clients" do Client.import sleep 2 - expect(subject.client_count).to eq( - [ - { "count" => 3, "id" => "2015", "title" => "2015" }, - { "count" => 3, "id" => "2016", "title" => "2016" }, - { "count" => 3, "id" => "2017", "title" => "2017" }, - { "count" => 3, "id" => "2018", "title" => "2018" }, - { "count" => 3, "id" => "2019", "title" => "2019" }, - { "count" => 3, "id" => "2020", "title" => "2020" }, - { "count" => 3, "id" => "2021", "title" => "2021" }, - { "count" => 3, "id" => "2022", "title" => "2022" }, - ], + expect(subject.client_count).to match_array( + @CUMULTATIVE_BY_YEAR ) end @@ -76,17 +60,8 @@ @clients.last.update(deleted_at: "2015-06-14") Client.import sleep 2 - expect(subject.client_count).to eq( - [ - { "count" => 1, "id" => "2018", "title" => "2018" }, - { "count" => 1, "id" => "2019", "title" => "2019" }, - { "count" => 1, "id" => "2020", "title" => "2020" }, - { "count" => 1, "id" => "2021", "title" => "2021" }, - { "count" => 1, "id" => "2022", "title" => "2022" }, - { "count" => 2, "id" => "2015", "title" => "2015" }, - { "count" => 2, "id" => "2016", "title" => "2016" }, - { "count" => 2, "id" => "2017", "title" => "2017" }, - ], + expect(subject.client_count).to match_array( + @CUMULTATIVE_BY_YEAR_WITH_DELETES ) end end diff --git a/spec/graphql/types/repository_type_spec.rb b/spec/graphql/types/repository_type_spec.rb index 85332230c..7f09faa59 100644 --- a/spec/graphql/types/repository_type_spec.rb +++ b/spec/graphql/types/repository_type_spec.rb @@ -4,6 +4,9 @@ require "rails_helper" describe RepositoryType do + before(:all) do + @current_year = Date.today.year + end describe "fields" do subject { described_class } @@ -677,7 +680,7 @@ response.dig("data", "repository", "prefixes", "totalCount"), ).to eq(3) expect(response.dig("data", "repository", "prefixes", "years")).to eq( - [{ "count" => 3, "id" => "2022" }], + [{ "count" => 3, "id" => "#{@current_year}" }], ) expect( response.dig("data", "repository", "prefixes", "nodes").length, From 792dd5697a69f36e0e232880f231f3ca50e21104 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Wed, 11 Jan 2023 11:38:48 -0500 Subject: [PATCH 6/7] Update the rest of specs to auto use current year --- spec/concerns/countable_spec.rb | 16 ++++++++-------- spec/graphql/types/member_type_spec.rb | 8 +++++--- spec/graphql/types/repository_type_spec.rb | 4 ++-- spec/requests/provider_prefixes_spec.rb | 3 ++- spec/requests/providers_spec.rb | 5 +++-- spec/requests/repositories_spec.rb | 3 ++- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/spec/concerns/countable_spec.rb b/spec/concerns/countable_spec.rb index d59ec7759..141042f60 100644 --- a/spec/concerns/countable_spec.rb +++ b/spec/concerns/countable_spec.rb @@ -5,14 +5,14 @@ describe "Providers", type: :controller, elasticsearch: true do subject { ProvidersController.new } before(:all) do - current_year = Date.today.year - @CUMULTATIVE_BY_YEAR = (2015..current_year).to_a.map do |year| - { "count" => 3, "id" => "#{year}", "title" => "#{year}" } - end - @CUMULTATIVE_BY_YEAR_WITH_DELETES = (2015..2017).to_a.map do |year| - { "count" => 2, "id" => "#{year}", "title" => "#{year}" } - end | (2018..current_year).to_a.map do |year| - { "count" => 1, "id" => "#{year}", "title" => "#{year}" } + current_year = Date.today.year.to_s + @CUMULTATIVE_BY_YEAR = ("2015"..current_year).to_a.map do |year| + { "count" => 3, "id" => year, "title" => year } + end + @CUMULTATIVE_BY_YEAR_WITH_DELETES = ("2015".."2017").to_a.map do |year| + { "count" => 2, "id" => year, "title" => year } + end | ("2018"..current_year).to_a.map do |year| + { "count" => 1, "id" => year, "title" => year } end end diff --git a/spec/graphql/types/member_type_spec.rb b/spec/graphql/types/member_type_spec.rb index a706c8e7e..36c9b8a6f 100644 --- a/spec/graphql/types/member_type_spec.rb +++ b/spec/graphql/types/member_type_spec.rb @@ -102,6 +102,7 @@ it "returns all members" do response = LupoSchema.execute(query).as_json + current_year = Date.today.year.to_s expect(response.dig("data", "members", "totalCount")).to eq(6) expect( @@ -116,7 +117,7 @@ ).to be true expect(response.dig("data", "members", "years")).to eq( - [{ "count" => 6, "id" => "2023", "title" => "2023" }], + [{ "count" => 6, "id" => current_year, "title" => current_year }], ) expect(response.dig("data", "members", "regions")).to eq( [ @@ -210,6 +211,7 @@ it "returns member" do response = LupoSchema.execute(query).as_json + current_year = Date.today.year.to_s expect(response.dig("data", "member", "id")).to eq(provider.uid) expect(response.dig("data", "member", "memberRole")).to eq( @@ -224,7 +226,7 @@ response.dig("data", "member", "repositories", "totalCount"), ).to eq(1) expect(response.dig("data", "member", "repositories", "years")).to eq( - [{ "count" => 1, "id" => "2023" }], + [{ "count" => 1, "id" => current_year }], ) expect(response.dig("data", "member", "repositories", "software")).to eq( [{ "count" => 1, "id" => "dataverse" }], @@ -239,7 +241,7 @@ expect(response.dig("data", "member", "prefixes", "totalCount")).to eq(3) expect(response.dig("data", "member", "prefixes", "years")).to eq( - [{ "count" => 3, "id" => "2023" }], + [{ "count" => 3, "id" => current_year }], ) expect(response.dig("data", "member", "prefixes", "nodes").length).to eq( 3, diff --git a/spec/graphql/types/repository_type_spec.rb b/spec/graphql/types/repository_type_spec.rb index 7f09faa59..e293aef55 100644 --- a/spec/graphql/types/repository_type_spec.rb +++ b/spec/graphql/types/repository_type_spec.rb @@ -5,7 +5,7 @@ describe RepositoryType do before(:all) do - @current_year = Date.today.year + @current_year = Date.today.year.to_s end describe "fields" do subject { described_class } @@ -680,7 +680,7 @@ response.dig("data", "repository", "prefixes", "totalCount"), ).to eq(3) expect(response.dig("data", "repository", "prefixes", "years")).to eq( - [{ "count" => 3, "id" => "#{@current_year}" }], + [{ "count" => 3, "id" => @current_year }], ) expect( response.dig("data", "repository", "prefixes", "nodes").length, diff --git a/spec/requests/provider_prefixes_spec.rb b/spec/requests/provider_prefixes_spec.rb index 7c17530a1..18c81e3d8 100644 --- a/spec/requests/provider_prefixes_spec.rb +++ b/spec/requests/provider_prefixes_spec.rb @@ -38,10 +38,11 @@ get "/provider-prefixes?consortium-id=#{consortium.uid}", nil, headers + current_year = Date.today.year.to_s expect(last_response.status).to eq(200) expect(json["data"].size).to eq(3) expect(json.dig("meta", "years")).to eq( - [{ "count" => 3, "id" => "2023", "title" => "2023" }], + [{ "count" => 3, "id" => current_year, "title" => current_year }], ) expect(json.dig("meta", "states")).to eq( [ diff --git a/spec/requests/providers_spec.rb b/spec/requests/providers_spec.rb index 1ec8366f8..2232b5f43 100644 --- a/spec/requests/providers_spec.rb +++ b/spec/requests/providers_spec.rb @@ -247,13 +247,14 @@ get "/providers/#{provider.symbol.downcase}/stats", nil, headers + current_year = Date.today.year.to_s expect(last_response.status).to eq(200) expect(json["clients"]).to eq( - [{ "count" => 1, "id" => "2023", "title" => "2023" }], + [{ "count" => 1, "id" => current_year, "title" => current_year }], ) # expect(json["resourceTypes"]).to eq([{"count"=>3, "id"=>"dataset", "title"=>"Dataset"}]) expect(json["dois"]).to eq( - [{ "count" => 3, "id" => "2023", "title" => "2023" }], + [{ "count" => 3, "id" => current_year, "title" => current_year }], ) end end diff --git a/spec/requests/repositories_spec.rb b/spec/requests/repositories_spec.rb index c9f9a9717..1d3b53cb1 100644 --- a/spec/requests/repositories_spec.rb +++ b/spec/requests/repositories_spec.rb @@ -192,12 +192,13 @@ it "returns repository" do get "/repositories/#{client.uid}/stats" + current_year = Date.today.year.to_s expect(last_response.status).to eq(200) expect(json["resourceTypes"]).to eq( [{ "count" => 3, "id" => "dataset", "title" => "Dataset" }], ) expect(json["dois"]).to eq( - [{ "count" => 3, "id" => "2023", "title" => "2023" }], + [{ "count" => 3, "id" => current_year, "title" => current_year }], ) end end From 0c9558fbcdf9a108de4fecbde61cb3d130244947 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Wed, 11 Jan 2023 14:20:03 -0500 Subject: [PATCH 7/7] Fix typo --- spec/concerns/countable_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/concerns/countable_spec.rb b/spec/concerns/countable_spec.rb index 141042f60..49dae4d18 100644 --- a/spec/concerns/countable_spec.rb +++ b/spec/concerns/countable_spec.rb @@ -6,10 +6,10 @@ subject { ProvidersController.new } before(:all) do current_year = Date.today.year.to_s - @CUMULTATIVE_BY_YEAR = ("2015"..current_year).to_a.map do |year| + @CUMULATIVE_BY_YEAR = ("2015"..current_year).to_a.map do |year| { "count" => 3, "id" => year, "title" => year } end - @CUMULTATIVE_BY_YEAR_WITH_DELETES = ("2015".."2017").to_a.map do |year| + @CUMULATIVE_BY_YEAR_WITH_DELETES = ("2015".."2017").to_a.map do |year| { "count" => 2, "id" => year, "title" => year } end | ("2018"..current_year).to_a.map do |year| { "count" => 1, "id" => year, "title" => year } @@ -26,7 +26,7 @@ Provider.import sleep 2 expect(subject.provider_count).to match_array( - @CUMULTATIVE_BY_YEAR + @CUMULATIVE_BY_YEAR ) end @@ -36,7 +36,7 @@ Provider.import sleep 2 expect(subject.provider_count).to match_array( - @CUMULTATIVE_BY_YEAR_WITH_DELETES + @CUMULATIVE_BY_YEAR_WITH_DELETES ) end end @@ -51,7 +51,7 @@ Client.import sleep 2 expect(subject.client_count).to match_array( - @CUMULTATIVE_BY_YEAR + @CUMULATIVE_BY_YEAR ) end @@ -61,7 +61,7 @@ Client.import sleep 2 expect(subject.client_count).to match_array( - @CUMULTATIVE_BY_YEAR_WITH_DELETES + @CUMULATIVE_BY_YEAR_WITH_DELETES ) end end