Skip to content

Commit 537b2d7

Browse files
committed
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.
1 parent 78ee617 commit 537b2d7

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/graphql/types/record_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def publication_information
217217
end
218218

219219
def file_formats
220-
@object['file_formats'].uniq
220+
@object['file_formats']&.uniq
221221
end
222222

223223
def imprint
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'test_helper'
2+
3+
class RecordTypeTest < ActiveSupport::TestCase
4+
# Test that file_formats handles nil values gracefully
5+
test 'file_formats returns nil when file_formats is nil' do
6+
record_data = { 'title' => 'Test Record' }
7+
record_type = TimdexSchema.types['Record'].new(record_data, {})
8+
9+
result = record_type.file_formats
10+
assert_nil result, 'file_formats should return nil when not present in data'
11+
end
12+
13+
# Test that file_formats works correctly when data is present
14+
test 'file_formats returns unique formats when present' do
15+
record_data = {
16+
'title' => 'Test Record',
17+
'file_formats' => ['PDF', 'PDF', 'EPUB', 'PDF']
18+
}
19+
record_type = TimdexSchema.types['Record'].new(record_data, {})
20+
21+
result = record_type.file_formats
22+
assert_equal ['PDF', 'EPUB'], result, 'file_formats should return unique values'
23+
end
24+
end

0 commit comments

Comments
 (0)