From 972cbb129747feb15c3c6c5ab5eabea2d7c045e1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 15:18:15 +0200 Subject: Workbench: Add `output` association The `output` attribute should be a `ReferentialSuite`. Refs #3520 --- spec/models/workbench_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/models/workbench_spec.rb') diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 84149ddb0..617f34bb1 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -11,6 +11,7 @@ RSpec.describe Workbench, :type => :model do it { should belong_to(:organisation) } it { should belong_to(:line_referential) } it { should belong_to(:stop_area_referential) } + it { should belong_to(:output).class_name('ReferentialSuite') } it { should have_many(:lines).through(:line_referential) } it { should have_many(:networks).through(:line_referential) } -- cgit v1.2.3 From 72806a4961e4240cff955c66fff1840e40301b89 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 16:17:04 +0200 Subject: Workbench: Validate presence of `output` Ensure all workbenches always have an associated `ReferentialSuite` by validating presence of the `output` attribute. In order to pass the 'should have a valid factory' test, we need to define a `ReferentialSuite` factory and set up a default association on `output` in the `Workbench` factory. We also need to add set `output` when creating a `Workbench` in `spec/support/referential.rb`, otherwise we get a validation error that breaks the tests. Clean up the formatting of that call. In order to pass a `ReferentialSuite`, I just create one above, but I'm not sure if I should be using a `find_or_create` mechanism like the other associated objects do above. Refs #3520 --- spec/models/workbench_spec.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'spec/models/workbench_spec.rb') diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 617f34bb1..f83342f75 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -20,6 +20,8 @@ RSpec.describe Workbench, :type => :model do it { should have_many(:stop_areas).through(:stop_area_referential) } + it { should validate_presence_of(:output) } + context '.lines' do let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } let!(:organisation) { create :organisation, sso_attributes: { functional_scope: ids.to_json } } -- cgit v1.2.3 From cfd4b8a553af99fd4571172183fa11e59f7e6e35 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 17:30:23 +0200 Subject: Workbench: Ensure `output` attr is initialised when creating workbench Since all `Workbench`es must have an associated `ReferentialSuite` via `output`, if none is provided when creating a new `Workbench`, create one automatically. This is done in a `before_validation` callback (eck, AR callbacks). In order for our prior 'should validate_presence_of' test to continue to work, we have to work around the new callback, otherwise Shoulda can't set `output` to `nil` to run its expectation and the test fails. Refs #3520 --- spec/models/workbench_spec.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'spec/models/workbench_spec.rb') diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index f83342f75..653b896dc 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -20,7 +20,14 @@ RSpec.describe Workbench, :type => :model do it { should have_many(:stop_areas).through(:stop_area_referential) } - it { should validate_presence_of(:output) } + it do + # This callback interferes with the validation test + Workbench.skip_callback(:validation, :before, :initialize_output) + + should validate_presence_of(:output) + + Workbench.set_callback(:validation, :before, :initialize_output) + end context '.lines' do let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } @@ -36,4 +43,18 @@ RSpec.describe Workbench, :type => :model do expect(lines.map(&:objectid)).to include(*ids) end end + + describe ".create" do + it "must automatically create a ReferentialSuite when being created" do + workbench = Workbench.create + expect(workbench.output).to be_an_instance_of(ReferentialSuite) + end + + it "must not overwrite a given ReferentialSuite" do + referential_suite = ReferentialSuite.create + workbench = Workbench.create(output: referential_suite) + + expect(workbench.output).to eq(referential_suite) + end + end end -- cgit v1.2.3 From 3ea043d39fc02b885294304157745c05b51dc19c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 27 Sep 2017 12:38:05 +0200 Subject: Workbench spec: Use factories Leverage our existing factories instead of creating the objects on the models directly. Refs #3520 --- spec/models/workbench_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/models/workbench_spec.rb') diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 653b896dc..037537b60 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -51,8 +51,8 @@ RSpec.describe Workbench, :type => :model do end it "must not overwrite a given ReferentialSuite" do - referential_suite = ReferentialSuite.create - workbench = Workbench.create(output: referential_suite) + referential_suite = create(:referential_suite) + workbench = create(:workbench, output: referential_suite) expect(workbench.output).to eq(referential_suite) end -- cgit v1.2.3