Skip to content

Commit 3344cfd

Browse files
committed
(feature) Raise exception if given hash is missing required keys
We want to ensure that the data import runs successfully, with no unexpected side effects, so this checks that the input hash includes the fields we use during the import process.
1 parent 80cbbc4 commit 3344cfd

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/supplier_import_row.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class SupplierImportRow
2+
REQUIRED_KEYS = %i[frameworkreference suppliername].freeze
3+
24
def initialize(row)
35
@row = row
6+
7+
check_for_required_keys!
48
end
59

610
def import!
@@ -9,4 +13,12 @@ def import!
913

1014
supplier.agreements.find_or_create_by!(framework: framework)
1115
end
16+
17+
private
18+
19+
def check_for_required_keys!
20+
raise MissingKey unless (REQUIRED_KEYS - @row.keys).empty?
21+
end
22+
23+
class MissingKey < StandardError; end
1224
end

spec/lib/supplier_import_row_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
}
2121
end
2222

23+
describe '#initialize' do
24+
it 'throws an error if the hash is missing required fields' do
25+
data = {
26+
frameworkreference: 'RM3756'
27+
}
28+
29+
expect { SupplierImportRow.new(data) }.to raise_error('SupplierImportRow::MissingKey')
30+
end
31+
end
32+
2333
describe '#import!' do
2434
it 'creates a new supplier and adds it to the framework' do
2535
framework = FactoryBot.create(:framework, short_name: 'RM3756')

0 commit comments

Comments
 (0)