Skip to content

Commit fb23979

Browse files
authored
Merge pull request #969 from MITLibraries/use-628
Add safe navigation to file_formats method
2 parents 78ee617 + b1d20f4 commit fb23979

2 files changed

Lines changed: 34 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'test_helper'
2+
3+
class RecordTypeTest < ActiveSupport::TestCase
4+
# Test that file_formats handles missing key gracefully
5+
test 'file_formats returns nil when file_formats key is missing' do
6+
record_data = { 'title' => 'Test Record' }
7+
record_type = Types::RecordType.send(:new, record_data, {})
8+
9+
result = record_type.file_formats
10+
assert_nil result, 'file_formats should return nil when key is missing'
11+
end
12+
13+
# Test that file_formats handles explicit nil gracefully
14+
test 'file_formats returns nil when file_formats is nil' do
15+
record_data = { 'title' => 'Test Record', 'file_formats' => nil }
16+
record_type = Types::RecordType.send(:new, record_data, {})
17+
18+
result = record_type.file_formats
19+
assert_nil result, 'file_formats should return nil when value is nil'
20+
end
21+
22+
# Test that file_formats works correctly when data is present
23+
test 'file_formats returns unique formats when present' do
24+
record_data = {
25+
'title' => 'Test Record',
26+
'file_formats' => %w[PDF PDF EPUB PDF]
27+
}
28+
record_type = Types::RecordType.send(:new, record_data, {})
29+
30+
result = record_type.file_formats
31+
assert_equal %w[PDF EPUB], result, 'file_formats should return unique values'
32+
end
33+
end

0 commit comments

Comments
 (0)