Skip to content

Commit f732ddf

Browse files
authored
Merge pull request #2079 from alphagov/ldeb-fix-model-specs
Fix specs for forms models
2 parents c2d641e + a411bcc commit f732ddf

17 files changed

Lines changed: 609 additions & 368 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
FactoryBot.define do
2+
factory :condition_resource, class: "Api::V1::ConditionResource" do
3+
routing_page_id { nil }
4+
check_page_id { nil }
5+
answer_value { nil }
6+
goto_page_id { nil }
7+
skip_to_end { false }
8+
validation_errors { [] }
9+
has_routing_errors { false }
10+
exit_page_heading { nil }
11+
exit_page_markdown { nil }
12+
13+
trait :with_answer_value_missing do
14+
answer_value { nil }
15+
has_routing_errors { true }
16+
validation_errors { [OpenStruct.new(name: "answer_value_doesnt_exist")] }
17+
end
18+
19+
trait :with_goto_page_missing do
20+
goto_page_id { nil }
21+
has_routing_errors { true }
22+
validation_errors { [OpenStruct.new(name: "goto_page_doesnt_exist")] }
23+
end
24+
25+
trait :with_goto_page_before_check_page do
26+
has_routing_errors { true }
27+
validation_errors { [OpenStruct.new(name: "cannot_have_goto_page_before_routing_page")] }
28+
end
29+
30+
trait :with_goto_page_immediately_after_check_page do
31+
has_routing_errors { true }
32+
validation_errors { [OpenStruct.new(name: "cannot_route_to_next_page")] }
33+
end
34+
35+
trait :with_answer_value_and_goto_page_missing do
36+
goto_page_id { nil }
37+
has_routing_errors { true }
38+
validation_errors { [OpenStruct.new(name: "answer_value_doesnt_exist"), OpenStruct.new(name: "goto_page_doesnt_exist")] }
39+
end
40+
41+
trait :with_exit_page do
42+
goto_page_id { nil }
43+
exit_page_heading { "Exit page heading" }
44+
exit_page_markdown { "Exit page markdown" }
45+
end
46+
end
47+
end
Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,3 @@
11
FactoryBot.define do
2-
factory :condition, class: "Api::V1::ConditionResource" do
3-
routing_page_id { nil }
4-
check_page_id { nil }
5-
answer_value { nil }
6-
goto_page_id { nil }
7-
skip_to_end { false }
8-
validation_errors { [] }
9-
has_routing_errors { false }
10-
exit_page_heading { nil }
11-
exit_page_markdown { nil }
12-
13-
trait :with_answer_value_missing do
14-
answer_value { nil }
15-
has_routing_errors { true }
16-
validation_errors { [OpenStruct.new(name: "answer_value_doesnt_exist")] }
17-
end
18-
19-
trait :with_goto_page_missing do
20-
goto_page_id { nil }
21-
has_routing_errors { true }
22-
validation_errors { [OpenStruct.new(name: "goto_page_doesnt_exist")] }
23-
end
24-
25-
trait :with_goto_page_before_check_page do
26-
has_routing_errors { true }
27-
validation_errors { [OpenStruct.new(name: "cannot_have_goto_page_before_routing_page")] }
28-
end
29-
30-
trait :with_goto_page_immediately_after_check_page do
31-
has_routing_errors { true }
32-
validation_errors { [OpenStruct.new(name: "cannot_route_to_next_page")] }
33-
end
34-
35-
trait :with_answer_value_and_goto_page_missing do
36-
goto_page_id { nil }
37-
has_routing_errors { true }
38-
validation_errors { [OpenStruct.new(name: "answer_value_doesnt_exist"), OpenStruct.new(name: "goto_page_doesnt_exist")] }
39-
end
40-
41-
trait :with_exit_page do
42-
goto_page_id { nil }
43-
exit_page_heading { "Exit page heading" }
44-
exit_page_markdown { "Exit page markdown" }
45-
end
46-
end
2+
factory :condition, parent: :condition_resource
473
end

spec/factories/models/form_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
end
3030

3131
pages do
32-
Array.new(pages_count) { association(:page, factory: :page_record) }
32+
Array.new(pages_count) { association(:page_record) }
3333
end
3434

3535
question_section_completed { true }
3636
end
3737

3838
trait :with_text_page do
3939
pages do
40-
Array.new(1) { association(:page, factory: :page_record, answer_type: "text", answer_settings: { input_type: %w[single_line long_text].sample }) }
40+
Array.new(1) { association(:page_record, answer_type: "text", answer_settings: { input_type: %w[single_line long_text].sample }) }
4141
end
4242

4343
question_section_completed { true }
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
FactoryBot.define do
2+
factory :form_resource, class: "Api::V1::FormResource" do
3+
sequence(:name) { |n| "Form #{n}" }
4+
sequence(:form_slug) { |n| "form-#{n}" }
5+
has_draft_version { true }
6+
submission_email { Faker::Internet.email(domain: "example.gov.uk") }
7+
submission_type { "email" }
8+
privacy_policy_url { Faker::Internet.url(host: "gov.uk") }
9+
language { "en" }
10+
live_at { nil }
11+
support_email { nil }
12+
support_phone { nil }
13+
support_url { nil }
14+
support_url_text { nil }
15+
what_happens_next_markdown { nil }
16+
declaration_text { nil }
17+
question_section_completed { false }
18+
declaration_section_completed { false }
19+
share_preview_completed { false }
20+
has_routing_errors { false }
21+
creator_id { nil }
22+
ready_for_live { false }
23+
incomplete_tasks { %i[missing_pages missing_privacy_policy_url missing_contact_details missing_what_happens_next share_preview_not_completed] }
24+
state { :draft }
25+
26+
transient do
27+
statuses { { declaration_status: "not_started", make_live_status: "cannot_start", name_status: "completed", pages_status: "not_started", privacy_policy_status: "not_started", support_contact_details_status: "not_started", what_happens_next_status: "not_started" } }
28+
end
29+
task_statuses { OpenStruct.new(attributes: statuses) }
30+
31+
trait :new_form do
32+
submission_email { "" }
33+
privacy_policy_url { "" }
34+
state { :draft }
35+
end
36+
37+
trait :with_id do
38+
sequence(:id) { |n| n }
39+
end
40+
41+
trait :ready_for_live do
42+
with_pages
43+
support_email { Faker::Internet.email(domain: "example.gov.uk") }
44+
what_happens_next_markdown { "We usually respond to applications within 10 working days." }
45+
question_section_completed { true }
46+
declaration_section_completed { true }
47+
share_preview_completed { true }
48+
ready_for_live { true }
49+
incomplete_tasks { [] }
50+
transient do
51+
statuses { { declaration_status: "completed", make_live_status: "not_started", name_status: "completed", pages_status: "completed", privacy_policy_status: "completed", support_contact_details_status: "completed", what_happens_next_status: "completed" } }
52+
end
53+
task_statuses { OpenStruct.new(attributes: statuses) }
54+
end
55+
56+
trait :live do
57+
ready_for_live
58+
live_at { Time.zone.now }
59+
has_draft_version { false }
60+
state { :live }
61+
end
62+
63+
trait :live_with_draft do
64+
live
65+
state { :live_with_draft }
66+
has_draft_version { true }
67+
end
68+
69+
trait :archived do
70+
live
71+
state { :archived }
72+
end
73+
74+
trait :archived_with_draft do
75+
live
76+
state { :archived_with_draft }
77+
end
78+
79+
trait :with_active_resource do
80+
task_statuses { statuses }
81+
end
82+
83+
trait :with_pages do
84+
transient do
85+
pages_count { 5 }
86+
end
87+
88+
pages do
89+
Array.new(pages_count) { association(:page_resource) }
90+
end
91+
92+
question_section_completed { true }
93+
end
94+
95+
trait :with_support do
96+
support_email { Faker::Internet.email(domain: "example.gov.uk") }
97+
support_phone { Faker::Lorem.paragraph(sentence_count: 2, supplemental: true, random_sentences_to_add: 4) }
98+
support_url { Faker::Internet.url(host: "gov.uk") }
99+
support_url_text { Faker::Lorem.sentence(word_count: 1, random_words_to_add: 4) }
100+
end
101+
102+
trait :ready_for_routing do
103+
transient do
104+
pages_count { 5 }
105+
end
106+
107+
pages do
108+
Array.new(pages_count) { association(:page_resource, :with_selection_settings) }
109+
end
110+
111+
after(:build) do |form|
112+
if form.pages.present?
113+
# assign position and next_page to each page
114+
form.pages.each.with_index(1).each_cons(2) do |page_with_index, next_page_with_index|
115+
page, page_index = page_with_index
116+
next_page, _next_page_index = next_page_with_index
117+
118+
page.position = page_index
119+
page.next_page = next_page&.id
120+
end
121+
end
122+
end
123+
end
124+
125+
trait :ready_for_api_routing do
126+
transient do
127+
pages_count { 5 }
128+
end
129+
130+
pages do
131+
Array.new(pages_count) { association(:page_resource, :with_api_selection_settings) }
132+
end
133+
134+
after(:build) do |form|
135+
if form.pages.present?
136+
# assign position and next_page to each page
137+
form.pages.each.with_index(1).each_cons(2) do |page_with_index, next_page_with_index|
138+
page, page_index = page_with_index
139+
next_page, _next_page_index = next_page_with_index
140+
141+
page.position = page_index
142+
page.next_page = next_page&.id
143+
end
144+
end
145+
end
146+
end
147+
148+
trait :missing_pages do
149+
incomplete_tasks { %i[missing_pages] }
150+
end
151+
end
152+
end

0 commit comments

Comments
 (0)