Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDC-3344: Sales CSV download ecstat2 child label bug fix #2801

Merged
2 changes: 2 additions & 0 deletions app/models/derived_variables/sales_log_variables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def child_under_16_constraints!
(start_index..6).each do |idx|
if age_under_16?(idx)
self["ecstat#{idx}"] = 9
elsif self["ecstat#{idx}"] == 9
self["ecstat#{idx}"] = nil
end
end
Copy link
Contributor Author

@Dinssa Dinssa Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this so a user can change the age of a person that was first set as under 16 to another age above 16. At the moment should they want to do that they would get an error:
Answer cannot be over 16 as person’s 2 working situation is ‘child under 16‘.

As the working situation question for a child is not one they can access they'd get stuck. Now should they change the age e.g. to 17, the next question will be to choose the working situation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests for this to make sure it doesn't clear it when we don't want it to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added

end
Expand Down
6 changes: 6 additions & 0 deletions app/models/form/sales/questions/buyer2_working_situation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def initialize(id, hsh, page)
@question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max]
end

def displayed_answer_options(log, _user = nil)
answer_options.reject { |key, _| key == "9" }
end

def answer_options
if form.start_year_2025_or_later?
{
Expand All @@ -26,6 +30,7 @@ def answer_options
"6" => { "value" => "Not seeking work" },
"7" => { "value" => "Full-time student" },
"8" => { "value" => "Unable to work due to long term sick or disability" },
"9" => { "value" => "Child under 16" },
"0" => { "value" => "Other" },
"10" => { "value" => "Buyer prefers not to say" },
}.freeze
Comment on lines 30 to 36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are not displaying the key “9” in 2024 or beyond, I added the value but omitted the depends_on conditions found on the generic person working situation page's answer options.

Expand All @@ -41,6 +46,7 @@ def answer_options
"0" => { "value" => "Other" },
"10" => { "value" => "Buyer prefers not to say" },
"7" => { "value" => "Full-time student" },
"9" => { "value" => "Child under 16" },
}.freeze
end
end
Expand Down
28 changes: 26 additions & 2 deletions spec/models/form/sales/questions/buyer2_working_situation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,46 @@
"0" => { "value" => "Other" },
"10" => { "value" => "Buyer prefers not to say" },
"7" => { "value" => "Full-time student" },
"9" => { "value" => "Child under 16" },
})
end

it "has the correct displayed_answer_options" do
expect(question.displayed_answer_options(nil)).to eq({
"1" => { "value" => "Full-time - 30 hours or more" },
"2" => { "value" => "Part-time - Less than 30 hours" },
"3" => { "value" => "In government training into work" },
"4" => { "value" => "Jobseeker" },
"6" => { "value" => "Not seeking work" },
"8" => { "value" => "Unable to work due to long term sick or disability" },
"5" => { "value" => "Retired" },
"0" => { "value" => "Other" },
"10" => { "value" => "Buyer prefers not to say" },
"7" => { "value" => "Full-time student" },
})
end

context "with start year before 2025" do
let(:form) { instance_double(Form, start_date: Time.zone.local(2024, 4, 1), start_year_2025_or_later?: false) }

it "uses the old ordering for answer options" do
expect(question.answer_options.keys).to eq(%w[1 2 3 4 6 8 5 0 10 7])
expect(question.answer_options.keys).to eq(%w[1 2 3 4 6 8 5 0 10 7 9])
end

it "uses the old ordering for displayed answer options" do
expect(question.displayed_answer_options(nil).keys).to eq(%w[1 2 3 4 6 8 5 0 10 7])
end
end

context "with start year from 2025" do
let(:form) { instance_double(Form, start_date: Time.zone.local(2025, 4, 1), start_year_2025_or_later?: true) }

it "uses the new ordering for answer options" do
expect(question.answer_options.keys).to eq(%w[1 2 3 4 5 6 7 8 0 10])
expect(question.answer_options.keys).to eq(%w[1 2 3 4 5 6 7 8 9 0 10])
end

it "uses the new ordering for displayed answer options" do
expect(question.displayed_answer_options(nil).keys).to eq(%w[1 2 3 4 5 6 7 8 0 10])
end
end

Expand Down
49 changes: 49 additions & 0 deletions spec/models/sales_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -978,5 +978,54 @@ def previous_postcode_fields
end
end
end

describe "#child_under_16_constraints!" do
let(:sales_log) { create(:sales_log, saledate: Time.zone.local(2024, 4, 1)) }

it "sets ecstat2 to 9 when age2 is under 16" do
sales_log.age2 = 14
sales_log.set_derived_fields!
expect(sales_log.ecstat2).to eq(9)
end

it "does not clear ecstat2 if it is not 9" do
sales_log.age2 = 22
sales_log.ecstat2 = 3
sales_log.set_derived_fields!
expect(sales_log.ecstat2).to eq(3)
end

it "clears ecstat3 if it is 9 and age3 is not under 16" do
sales_log.age3 = 22
sales_log.ecstat3 = 9
sales_log.set_derived_fields!
expect(sales_log.ecstat3).to be_nil
end

it "does not clear ecstat3 if it is not 9" do
sales_log.age3 = 22
sales_log.ecstat3 = 8
sales_log.set_derived_fields!
expect(sales_log.ecstat3).to eq(8)
end

context "when a user changes their answer" do
before do
sales_log.age2 = 14
sales_log.ecstat2 = 9
end

it "clears the working situation only when no longer a child under 16 and retains the new value" do
sales_log.set_derived_fields!
expect(sales_log.ecstat2).to eq(9)
sales_log.age2 = 22
sales_log.set_derived_fields!
expect(sales_log.ecstat2).to be_nil
sales_log.ecstat2 = 4
sales_log.set_derived_fields!
expect(sales_log.ecstat2).to eq(4)
end
end
end
end
# rubocop:enable RSpec/MessageChain
Loading