From b1d20f4d324527ef6e182b7f8f37b0cd09c06587 Mon Sep 17 00:00:00 2001 From: jazairi <16103405+jazairi@users.noreply.github.com> Date: Mon, 15 Jun 2026 10:01:37 -0700 Subject: [PATCH] Add safe navigation to file_formats method Why these changes are being introduced: We are calling `uniq` on `file_formats`, which returns a nil error if that field does not exist. Relevant ticket(s): - [TIMX-628](https://mitlibraries.atlassian.net/browse/TIMX-628) How this addresses that need: This adds a safe navigation operator to the `file_formats` method. Side effects of this change: I added a regression test, which creates a new test file. It feels a bit odd not to test the other Record Type methods in there, but doing so seemed far out of scope of this ticket. --- app/graphql/types/record_type.rb | 2 +- test/graphql/types/record_type_test.rb | 33 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/graphql/types/record_type_test.rb diff --git a/app/graphql/types/record_type.rb b/app/graphql/types/record_type.rb index a4e88b8e..1d7a5e09 100644 --- a/app/graphql/types/record_type.rb +++ b/app/graphql/types/record_type.rb @@ -217,7 +217,7 @@ def publication_information end def file_formats - @object['file_formats'].uniq + @object['file_formats']&.uniq end def imprint diff --git a/test/graphql/types/record_type_test.rb b/test/graphql/types/record_type_test.rb new file mode 100644 index 00000000..0c5574be --- /dev/null +++ b/test/graphql/types/record_type_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class RecordTypeTest < ActiveSupport::TestCase + # Test that file_formats handles missing key gracefully + test 'file_formats returns nil when file_formats key is missing' do + record_data = { 'title' => 'Test Record' } + record_type = Types::RecordType.send(:new, record_data, {}) + + result = record_type.file_formats + assert_nil result, 'file_formats should return nil when key is missing' + end + + # Test that file_formats handles explicit nil gracefully + test 'file_formats returns nil when file_formats is nil' do + record_data = { 'title' => 'Test Record', 'file_formats' => nil } + record_type = Types::RecordType.send(:new, record_data, {}) + + result = record_type.file_formats + assert_nil result, 'file_formats should return nil when value is nil' + end + + # Test that file_formats works correctly when data is present + test 'file_formats returns unique formats when present' do + record_data = { + 'title' => 'Test Record', + 'file_formats' => %w[PDF PDF EPUB PDF] + } + record_type = Types::RecordType.send(:new, record_data, {}) + + result = record_type.file_formats + assert_equal %w[PDF EPUB], result, 'file_formats should return unique values' + end +end