Skip to content

Commit 4262548

Browse files
committed
(feature) SupplierImport accepts CSV data and imports it
Calling `SupplierImport.new(csv).run!` iterates over the supplied CSV data and calls `SupplierImportRow` on each row.
1 parent 3344cfd commit 4262548

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/supplier_import.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
require 'csv'
2+
require 'supplier_import_row'
3+
14
class SupplierImport
5+
def initialize(csv_data)
6+
@rows = CSV.parse(csv_data, headers: true, header_converters: :symbol)
7+
end
8+
9+
def run!
10+
@rows.each do |row|
11+
SupplierImportRow.new(row.to_h).import!
12+
end
13+
end
214
end

spec/lib/supplier_import_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
require 'rails_helper'
2-
require 'supplier_import'
2+
require './lib/supplier_import'
3+
require './lib/supplier_import_row'
34

45
RSpec.describe SupplierImport do
56
describe '#initialize' do
67
it 'accepts CSV data as a string input' do
8+
csv = "frameworkreference,suppliername\nRM1234,Cheese & Crackers LLP\nRM5678,Vaughan Legal\n"
9+
10+
expect { SupplierImport.new(csv) }.to_not raise_error
11+
end
12+
end
13+
14+
describe '#run!' do
15+
it 'creates supplier records for the suppliers listed in the CSV data' do
16+
rm1234 = FactoryBot.create(:framework, short_name: 'RM1234')
17+
rm5678 = FactoryBot.create(:framework, short_name: 'RM5678')
18+
19+
csv = "frameworkreference,suppliername\nRM1234,Cheese & Crackers LLP\nRM5678,Vaughan Legal\n"
20+
21+
SupplierImport.new(csv).run!
22+
23+
created_suppliers = Supplier.order(:name).all
24+
25+
expect(created_suppliers[0].name).to eql 'Cheese & Crackers LLP'
26+
expect(created_suppliers[0].frameworks).to contain_exactly(rm1234)
27+
28+
expect(created_suppliers[1].name).to eql 'Vaughan Legal'
29+
expect(created_suppliers[1].frameworks).to contain_exactly(rm5678)
730
end
831
end
932
end

0 commit comments

Comments
 (0)