Skip to content

Commit 80cbbc4

Browse files
committed
(feature) Ensure no duplicate suppliers or agreements are created
If a supplier or agreement already exists, retrieve it rather than creating a brand new one. Added RuboCop exclusion for Lint/AmbiguousBlockAssociation, as per recommmendation: rubocop/rubocop#4222
1 parent 1a53bde commit 80cbbc4

4 files changed

Lines changed: 50 additions & 18 deletions

File tree

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ Metrics/BlockLength:
6565
Max: 40
6666
Exclude:
6767
- 'spec/**/*'
68+
69+
Lint/AmbiguousBlockAssociation:
70+
Exclude:
71+
- 'spec/**/*'
72+

lib/supplier_import_row.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ def initialize(row)
44
end
55

66
def import!
7-
supplier = Supplier.create!(name: @row[:suppliername])
7+
supplier = Supplier.find_or_create_by!(name: @row[:suppliername])
88
framework = Framework.find_by!(short_name: @row[:frameworkreference])
99

10-
supplier.agreements.create!(framework: framework)
10+
supplier.agreements.find_or_create_by!(framework: framework)
1111
end
1212
end

spec/factories/agreement.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :agreement do
3+
supplier
4+
framework
5+
end
6+
end

spec/lib/supplier_import_row_spec.rb

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22
require './lib/supplier_import_row'
33

44
RSpec.describe SupplierImportRow do
5+
let(:data) do
6+
{
7+
frameworkreference: 'RM3756',
8+
supplierid: 1234,
9+
suppliername: 'Bob and Bucket LLP',
10+
lotid: 5678,
11+
lotnumber: 1,
12+
username: 'francis.brown',
13+
personname: 'Francis Brown',
14+
jobtitle: 'Big Cheese',
15+
contacttel: '02088118055',
16+
contactmobile: '07900111222',
17+
email: 'francis.brown@example.com',
18+
usercreationdate: '1/1/1970 18:00',
19+
lastlogindate: '1/7/2018 12:40'
20+
}
21+
end
22+
523
describe '#import!' do
624
it 'creates a new supplier and adds it to the framework' do
725
framework = FactoryBot.create(:framework, short_name: 'RM3756')
826

9-
data = {
10-
frameworkreference: 'RM3756',
11-
supplierid: 1234,
12-
suppliername: 'Bob and Bucket LLP',
13-
lotid: 5678,
14-
lotnumber: 1,
15-
username: 'francis.brown',
16-
personname: 'Francis Brown',
17-
jobtitle: 'Big Cheese',
18-
contacttel: '02088118055',
19-
contactmobile: '07900111222',
20-
email: 'francis.brown@example.com',
21-
usercreationdate: '1/1/1970 18:00',
22-
lastlogindate: '1/7/2018 12:40'
23-
}
24-
2527
row = SupplierImportRow.new(data)
2628

2729
expect { row.import! }.to change { Supplier.count }.by(1)
@@ -31,4 +33,23 @@
3133
expect(supplier.frameworks).to contain_exactly(framework)
3234
end
3335
end
36+
37+
it "doesn't create a supplier, if one already exists with that name" do
38+
FactoryBot.create(:framework, short_name: 'RM3756')
39+
FactoryBot.create(:supplier, name: 'Bob and Bucket LLP')
40+
41+
row = SupplierImportRow.new(data)
42+
43+
expect { row.import! }.not_to change { Supplier.count }
44+
end
45+
46+
it "doesn't create an agreement, if one already exists between the Supplier and Framework" do
47+
framework = FactoryBot.create(:framework, short_name: 'RM3756')
48+
supplier = FactoryBot.create(:supplier, name: 'Bob and Bucket LLP')
49+
FactoryBot.create(:agreement, framework: framework, supplier: supplier)
50+
51+
row = SupplierImportRow.new(data)
52+
53+
expect { row.import! }.not_to change { Agreement.count }
54+
end
3455
end

0 commit comments

Comments
 (0)