diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 41ed15742..ea43578de 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -125,9 +125,9 @@ class FormHandler forms.count { |form| now.between?(form.start_date, form.end_date) } > 1 end - def use_fake_forms! + def use_fake_forms!(fake_forms = nil) @directories = ["spec/fixtures/forms"] - @forms = get_all_forms + @forms = fake_forms || get_all_forms end def use_real_forms! diff --git a/spec/factories/form.rb b/spec/factories/form.rb new file mode 100644 index 000000000..9694cfaac --- /dev/null +++ b/spec/factories/form.rb @@ -0,0 +1,25 @@ +class FormFixture < Form + attr_accessor :sections, :subsections, :pages, :questions +end + +class FormFactory + def initialize(year:, type:) + @year = year + @type = type + end + + def with_sections(sections) + @sections = sections + self + end + + def build + form = FormFixture.new(nil, @year, [], @type) + @sections.each { |section| section.form = form } + form.sections = @sections + form.subsections = form.sections.flat_map(&:subsections) + form.pages = form.subsections.flat_map(&:pages) + form.questions = form.pages.flat_map(&:questions) + form + end +end diff --git a/spec/factories/page.rb b/spec/factories/page.rb new file mode 100644 index 000000000..2204db3ac --- /dev/null +++ b/spec/factories/page.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :page, class: "Form::Page" do + initialize_with { new(id, nil, nil) } + end +end diff --git a/spec/factories/question.rb b/spec/factories/question.rb new file mode 100644 index 000000000..e34bc5e75 --- /dev/null +++ b/spec/factories/question.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :question, class: "Form::Question" do + initialize_with { new(id, nil, nil) } + type { "text" } + end +end diff --git a/spec/factories/section.rb b/spec/factories/section.rb new file mode 100644 index 000000000..92f691455 --- /dev/null +++ b/spec/factories/section.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :section, class: "Form::Section" do + initialize_with { new(id, nil, nil) } + end +end diff --git a/spec/factories/subsection.rb b/spec/factories/subsection.rb new file mode 100644 index 000000000..d1949a369 --- /dev/null +++ b/spec/factories/subsection.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :subsection, class: "Form::Subsection" do + initialize_with { new(id, nil, nil) } + end +end diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index d32cd4066..7979b838d 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -254,18 +254,50 @@ RSpec.describe FormHandler do end it "inserts questions from previous years that do not appear in more recent years in the correct position" do - current_sales_question_ids = described_class.instance.forms["current_sales"].questions.map(&:id).uniq - previous_sales_question_ids = described_class.instance.forms["previous_sales"].questions.map(&:id).uniq - obsolete_question_ids = previous_sales_question_ids - current_sales_question_ids - expect(obsolete_question_ids.count).to be_positive - obsolete_question_ids.each do |obsolete_id| - index = previous_sales_question_ids.index(obsolete_id) - previous_question_id = previous_sales_question_ids[index - 1] - expect(result).to(include { |q| q.id == id }) - index_of_previous_question_in_result = result.index { |q| q.id == previous_question_id } - index_of_obsolete_question_in_result = result.index { |q| q.id == obsolete_id } - expect(index_of_obsolete_question_in_result).to be index_of_previous_question_in_result + 1 - end + original_section = build( + :section, + id: "original_section", + subsections: [ + build( + :subsection, + id: "original_subsection", + pages: [ + build(:page, id: "1", questions: [build(:question, id: "1")]), + build(:page, id: "1a_deprecated", questions: [build(:question, id: "1a_deprecated")]), + build(:page, id: "2", questions: [build(:question, id: "2")]), + build(:page, id: "3", questions: [build(:question, id: "3")]), + ] + ) + ] + ) + new_section = build( + :section, + id: "new_section", + subsections: [ + build( + :subsection, + id: "new_subsection", + pages: [ + build(:page, id: "1", questions: [build(:question, id: "1")]), + build(:page, id: "2", questions: [build(:question, id: "2")]), + build(:page, id: "2a_new", questions: [build(:question, id: "2a_new")]), + build(:page, id: "3", questions: [build(:question, id: "3")]), + ] + ) + ] + ) + original_form = FormFactory.new(year: 1066, type: "sales") + .with_sections([original_section]) + .build + new_form = FormFactory.new(year: 1485, type: "sales") + .with_sections([new_section]) + .build + fake_forms = { + earlier_sales: original_form, + newer_sales: new_form, + } + described_class.instance.use_fake_forms!(fake_forms) + expect(result.map(&:id)).to eq %w[1 1a_deprecated 2 2a_new 3] end end # rubocop:enable RSpec/PredicateMatcher