From 50e689ac57ef0fae9cb5a6ae475081395b3bda2d Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Thu, 23 Mar 2023 09:13:08 -0400 Subject: [PATCH] Deprecated datePublished nil date handling Why are these changes being introduced: * our deprecated datePublished logic errored if there were no dates at all returned Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-215 How does this address that need: * Adds safe navigators --- app/graphql/types/record_type.rb | 2 +- .../controllers/graphql_controller_v2_test.rb | 24 +++++- ..._does_not_error_when_no_dates_returned.yml | 84 +++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 test/vcr_cassettes/graphql_v2_datePublished_does_not_error_when_no_dates_returned.yml diff --git a/app/graphql/types/record_type.rb b/app/graphql/types/record_type.rb index 5dc42ad..cc2892c 100644 --- a/app/graphql/types/record_type.rb +++ b/app/graphql/types/record_type.rb @@ -211,7 +211,7 @@ def related_place end def publication_date - @object['dates'].map { |date| date['value'] if date['kind'] == 'Publication date' }.compact&.first + @object['dates']&.map { |date| date['value'] if date['kind'] == 'Publication date' }&.compact&.first end def file_formats diff --git a/test/controllers/graphql_controller_v2_test.rb b/test/controllers/graphql_controller_v2_test.rb index 5cfb9b5..4b34e51 100644 --- a/test/controllers/graphql_controller_v2_test.rb +++ b/test/controllers/graphql_controller_v2_test.rb @@ -225,7 +225,7 @@ def setup assert_equal('mit alma', json['data']['search']['aggregations']['source'] .first['key']) - assert_equal(1429210, + assert_equal(1_429_210, json['data']['search']['aggregations']['source'] .first['docCount']) end @@ -250,12 +250,29 @@ def setup assert_equal('mit alma', json['data']['search']['aggregations']['source'] .first['key']) - assert_equal(1429210, + assert_equal(1_429_210, json['data']['search']['aggregations']['source'] .first['docCount']) end end + test 'graphqlv2 deprecated datePublished does not error when no dates returned' do + VCR.use_cassette('graphql v2 datePublished does not error when no dates returned') do + post '/graphql', params: { query: '{ + recordId(id: "dspace:1721.1-2789") { + publicationDate + source + sourceLink + title + } + }' } + assert_equal(200, response.status) + json = JSON.parse(response.body) + assert_equal('DSpace@MIT', json['data']['recordId']['source']) + assert_nil(json['data']['recordId']['dates']) + end + end + test 'graphqlv2 search with multiple subjects applied' do skip 'opensearch model is not updated to allow this yet' VCR.use_cassette('graphql v2 search multiple subjects') do @@ -331,7 +348,7 @@ def setup end end - test 'graphqlv2 retrieve with not found recordid' do + test 'graphqlv2 retrieve with not found recordid' do VCR.use_cassette('graphql v2 retrieve not found') do post '/graphql', params: { query: '{ recordId(id: "totallylegitrecordid") { @@ -346,7 +363,6 @@ def setup end end - test 'graphqlv2 holding location is not required' do VCR.use_cassette('graphql v2 location') do post '/graphql', params: { query: '{ diff --git a/test/vcr_cassettes/graphql_v2_datePublished_does_not_error_when_no_dates_returned.yml b/test/vcr_cassettes/graphql_v2_datePublished_does_not_error_when_no_dates_returned.yml new file mode 100644 index 0000000..514087d --- /dev/null +++ b/test/vcr_cassettes/graphql_v2_datePublished_does_not_error_when_no_dates_returned.yml @@ -0,0 +1,84 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:9200/ + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - 'opensearch-ruby/2.1.0 (RUBY_VERSION: 3.1.2; darwin x86_64; Faraday v2.7.4)' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Content-Length: + - '342' + body: + encoding: ASCII-8BIT + string: | + { + "name" : "opensearch-node1", + "cluster_name" : "opensearch-cluster", + "cluster_uuid" : "iKrV9vbkTVyyScXNbMKHTA", + "version" : { + "distribution" : "opensearch", + "number" : "2.5.0", + "build_type" : "tar", + "build_hash" : "b8a8b6c4d7fc7a7e32eb2cb68ecad8057a4636ad", + "build_date" : "2023-01-18T23:48:48.981786100Z", + "build_snapshot" : false, + "lucene_version" : "9.4.2", + "minimum_wire_compatibility_version" : "7.10.0", + "minimum_index_compatibility_version" : "7.0.0" + }, + "tagline" : "The OpenSearch Project: https://opensearch.org/" + } + recorded_at: Wed, 22 Mar 2023 15:15:50 GMT +- request: + method: post + uri: http://localhost:9200/timdex-prod/_search + body: + encoding: UTF-8 + string: '{"query":{"ids":{"values":["dspace:1721.1-2789"]}}}' + headers: + User-Agent: + - 'opensearch-ruby/2.1.0 (RUBY_VERSION: 3.1.2; darwin x86_64; Faraday v2.7.4)' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Content-Length: + - '586' + body: + encoding: ASCII-8BIT + string: '{"took":85,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"dspace-2023-03-22t10-57-30","_id":"dspace:1721.1-2789","_score":1.0,"_source":{"source":"DSpace@MIT","source_link":"https://dspace.mit.edu/handle/1721.1/2789","timdex_record_id":"dspace:1721.1-2789","title":"Distributed + decision and communication problems in tactical USAF command and control : + annual technical report for period ...","citation":"Distributed decision and + communication problems in tactical USAF command and control : annual technical + report for period .... Laboratory for Information and Decision Systems, Massachusetts + Institute of Technology. https://dspace.mit.edu/handle/1721.1/2789","content_type":["Not + specified"],"file_formats":["application/pdf"],"format":"electronic resource","identifiers":[{"value":"http://hdl.handle.net/1721.1/2789","kind":"uri"}],"languages":["eng"],"links":[{"url":"http://hdl.handle.net/1721.1/2789","kind":"Digital + object URL","text":"Digital object URL"}],"numbering":"LIDS-IR ; 1226","publication_information":["Laboratory + for Information and Decision Systems, Massachusetts Institute of Technology"]}}]}}' + recorded_at: Wed, 22 Mar 2023 15:15:50 GMT +recorded_with: VCR 6.1.0