Skip to content

Commit 17f9953

Browse files
committed
Fix flaky test for mavis imports recent cli
1 parent 6dd1a60 commit 17f9953

1 file changed

Lines changed: 92 additions & 172 deletions

File tree

spec/features/cli_imports_recent_spec.rb

Lines changed: 92 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44

55
describe "mavis imports recent" do
66
scenario "shows both class and cohort imports by default" do
7-
given_there_are_class_and_cohort_imports
7+
given_there_imports
88
when_i_run_the_recent_patient_imports_command
99
then_i_should_see_both_import_types
1010
end
1111

1212
scenario "filters to show only class imports" do
13-
given_there_are_class_and_cohort_imports
13+
given_there_imports
1414
when_i_run_the_command_filtered_by_type("class")
1515
then_i_should_see_only_class_imports
1616
end
1717

1818
scenario "filters to show only cohort imports" do
19-
given_there_are_class_and_cohort_imports
19+
given_there_imports
2020
when_i_run_the_command_filtered_by_type("cohort")
2121
then_i_should_see_only_cohort_imports
2222
end
2323

24+
scenario "filters to show only immunisation imports" do
25+
given_there_imports
26+
when_i_run_the_command_filtered_by_type("immunisation")
27+
then_i_should_see_only_immunisation_imports
28+
end
29+
2430
scenario "limits the number of imports shown across both types" do
2531
given_there_are_multiple_class_and_cohort_imports
2632
when_i_run_the_command_with_a_number_limit
27-
then_i_should_see_limited_patient_imports
33+
then_i_should_see_a_limited_number_of_imports
2834
end
2935

3036
scenario "filters by organisation across both import types" do
@@ -45,124 +51,68 @@
4551
then_i_should_see_imports_sorted_by_creation_date
4652
end
4753

48-
def given_there_are_class_and_cohort_imports
49-
@team = create(:team, workgroup: "test_team")
50-
@location = create(:school, team: @team)
51-
52-
@class_import =
53-
create(
54-
:class_import,
55-
:processed,
56-
team: @team,
57-
location: @location,
58-
rows_count: 15
59-
)
60-
@cohort_import =
61-
create(:cohort_import, :processed, team: @team, rows_count: 25)
54+
def given_there_imports
55+
team = create(:team, workgroup: "test_team")
56+
location = create(:school, team:)
57+
58+
@class_import = create(:class_import, :processed, team:, location:)
59+
@cohort_import = create(:cohort_import, :processed, team:)
60+
@immunisation_import = create(:immunisation_import, :processed, team:)
6261
end
6362

6463
def given_there_are_multiple_class_and_cohort_imports
65-
@team = create(:team, workgroup: "multi_team")
66-
@location = create(:school, team: @team)
64+
team = create(:team, workgroup: "multi_team")
65+
location = create(:school, team:)
6766

6867
# Create 3 of each type with time travel to ensure proper ordering
6968
freeze_time do
70-
@class_import1 =
71-
create(
72-
:class_import,
73-
:processed,
74-
team: @team,
75-
location: @location,
76-
rows_count: 10
77-
)
69+
create(:class_import, :processed, team:, location:)
7870
travel 1.minute
79-
@class_import2 =
80-
create(
81-
:class_import,
82-
:processed,
83-
team: @team,
84-
location: @location,
85-
rows_count: 20
86-
)
71+
create(:class_import, :processed, team:, location:)
8772
travel 1.minute
88-
@class_import3 =
89-
create(
90-
:class_import,
91-
:processed,
92-
team: @team,
93-
location: @location,
94-
rows_count: 30
95-
)
73+
create(:class_import, :processed, team:, location:)
9674
travel 1.minute
97-
@cohort_import1 =
98-
create(:cohort_import, :processed, team: @team, rows_count: 40)
75+
@oldest_import = create(:cohort_import, :processed, team:)
9976
travel 1.minute
100-
@cohort_import2 =
101-
create(:cohort_import, :processed, team: @team, rows_count: 50)
77+
@middle_aged_import = create(:cohort_import, :processed, team:)
10278
travel 1.minute
103-
@cohort_import3 =
104-
create(:cohort_import, :processed, team: @team, rows_count: 60)
79+
@youngest_import = create(:cohort_import, :processed, team:)
10580
end
10681
end
10782

10883
def given_there_are_imports_in_different_organisations
109-
@organisation1 = create(:organisation, ods_code: "ORG1")
110-
@organisation2 = create(:organisation, ods_code: "ORG2")
111-
@team1 = create(:team, organisation: @organisation1, workgroup: "team1")
112-
@team2 = create(:team, organisation: @organisation2, workgroup: "team2")
113-
@location1 = create(:school, team: @team1)
114-
115-
@class_import_org1 =
116-
create(
117-
:class_import,
118-
:processed,
119-
team: @team1,
120-
location: @location1,
121-
rows_count: 15
122-
)
123-
@cohort_import_org1 =
124-
create(:cohort_import, :processed, team: @team1, rows_count: 25)
125-
@cohort_import_org2 =
126-
create(:cohort_import, :processed, team: @team2, rows_count: 35)
84+
organisation1 = create(:organisation, ods_code: "ORG1")
85+
organisation2 = create(:organisation, ods_code: "ORG2")
86+
team1 = create(:team, organisation: organisation1, workgroup: "team1")
87+
team2 = create(:team, organisation: organisation2, workgroup: "team2")
88+
location = create(:school, team: team1)
89+
90+
create(:class_import, :processed, team: team1, location:)
91+
create(:cohort_import, :processed, team: team1)
92+
create(:cohort_import, :processed, team: team2)
12793
end
12894

12995
def given_there_are_imports_in_different_workgroups
130-
@organisation = create(:organisation)
131-
@team1 = create(:team, organisation: @organisation, workgroup: "alpha")
132-
@team2 = create(:team, organisation: @organisation, workgroup: "beta")
133-
@location1 = create(:school, team: @team1)
134-
135-
@class_import_alpha =
136-
create(
137-
:class_import,
138-
:processed,
139-
team: @team1,
140-
location: @location1,
141-
rows_count: 15
142-
)
143-
@cohort_import_alpha =
144-
create(:cohort_import, :processed, team: @team1, rows_count: 25)
145-
@cohort_import_beta =
146-
create(:cohort_import, :processed, team: @team2, rows_count: 35)
96+
organisation = create(:organisation)
97+
team1 = create(:team, organisation:, workgroup: "wg1")
98+
team2 = create(:team, organisation:, workgroup: "wg2")
99+
location = create(:school, team: team1)
100+
101+
create(:class_import, :processed, team: team1, location:)
102+
create(:cohort_import, :processed, team: team1)
103+
create(:cohort_import, :processed, team: team2)
147104
end
148105

149106
def given_there_are_interleaved_class_and_cohort_imports
150-
@team = create(:team, workgroup: "interleaved")
151-
@location = create(:school, team: @team)
107+
team = create(:team, workgroup: "interleaved")
108+
location = create(:school, team:)
152109

153110
freeze_time do
154-
@oldest = create(:cohort_import, :processed, team: @team, rows_count: 10)
111+
@oldest = create(:cohort_import, :processed, team:)
155112
travel 1.hour
156-
@middle =
157-
create(
158-
:class_import,
159-
:processed,
160-
team: @team,
161-
location: @location,
162-
rows_count: 20
163-
)
113+
@middle = create(:class_import, :processed, team:, location:)
164114
travel 1.hour
165-
@newest = create(:cohort_import, :processed, team: @team, rows_count: 30)
115+
@newest = create(:cohort_import, :processed, team:)
166116
end
167117
end
168118

@@ -180,6 +130,8 @@ def when_i_run_the_command_filtered_by_type(type)
180130
"--class-imports"
181131
when "cohort"
182132
"--cohort-imports"
133+
when "immunisation"
134+
"--immunisation-imports"
183135
else
184136
raise ArgumentError, "Invalid type: #{type}"
185137
end
@@ -214,105 +166,73 @@ def when_i_run_the_command_filtered_by_workgroup
214166
@output =
215167
capture_output do
216168
Dry::CLI.new(MavisCLI).call(
217-
arguments: %w[imports recent --once --workgroups alpha]
169+
arguments: %w[imports recent --once --workgroups wg1]
218170
)
219171
end
220172
end
221173

222174
def then_i_should_see_both_import_types
223-
# Check for both types (may be truncated as "cla" and "coh")
224-
expect(@output).to match(/\bcla/)
225-
expect(@output).to match(/\bcoh/)
226-
expect(@output).to match(/\b15\b/)
227-
expect(@output).to match(/\b25\b/)
228-
expect(@output).to match(/tes/) # test_team may be truncated
175+
expect(@output).to include(/^. \s cla \S+ \s+ #{@class_import.id}/x)
176+
expect(@output).to include(/^. \s coh \S+ \s+ #{@cohort_import.id}/x)
177+
expect(@output).to include(/^. \s imm \S+ \s+ #{@immunisation_import.id}/x)
229178
end
230179

231180
def then_i_should_see_only_class_imports
232-
# Check for class import rows (may show as "cla")
233-
expect(@output).to match(/\bcla/)
234-
expect(@output).to match(/\b15\b/)
235-
# Count data lines that don't include "coh" (cohort imports)
236-
data_lines =
237-
@output.lines.reject do |line|
238-
line.include?("╭") || line.include?("╰") || line.include?("type")
239-
end
240-
cohort_lines = data_lines.select { |line| line.match?(/\bcoh/) }
241-
expect(cohort_lines.count).to eq(0)
181+
expect(@output).to include(/^. \s cla \S+ \s+ #{@class_import.id}/x)
182+
expect(@output).not_to include(/^. \s coh \S+ \s+ #{@cohort_import.id}/x)
183+
expect(@output).not_to include(
184+
/^. \s imm \S+ \s+ #{@immunisation_import.id}/x
185+
)
242186
end
243187

244188
def then_i_should_see_only_cohort_imports
245-
# Check for cohort import rows (may show as "coh")
246-
expect(@output).to match(/\bcoh/)
247-
expect(@output).to match(/\b25\b/)
248-
# Count data lines that don't include "cla" (class imports)
249-
data_lines =
250-
@output.lines.reject do |line|
251-
line.include?("╭") || line.include?("╰") || line.include?("type")
252-
end
253-
class_lines = data_lines.select { |line| line.match?(/\bcla/) }
254-
expect(class_lines.count).to eq(0)
189+
expect(@output).to include(/^. \s coh \S+ \s+ #{@cohort_import.id}/x)
190+
expect(@output).not_to include(/^. \s cla \S+ \s+ #{@class_import.id}/x)
191+
expect(@output).not_to include(
192+
/^. \s imm \S+ \s+ #{@immunisation_import.id}/x
193+
)
194+
end
195+
196+
def then_i_should_see_only_immunisation_imports
197+
expect(@output).not_to include(/^. \s coh \S+ \s+ #{@cohort_import.id}/x)
198+
expect(@output).not_to include(/^. \s cla \S+ \s+ #{@class_import.id}/x)
199+
expect(@output).to include(/^. \s imm \S+ \s+ #{@immunisation_import.id}/x)
255200
end
256201

257-
def then_i_should_see_limited_patient_imports
258-
# Should show the 3 most recent imports across both types
259-
lines = @output.lines
260-
# Count lines that contain actual data (exclude header/border lines)
261-
# May be truncated as "mul" or "multi"
262-
data_lines = lines.select { |line| line.match?(/\bmul/) }
263-
expect(data_lines.count).to eq(3)
264-
265-
# The 3 newest should be cohort_import3, cohort_import2, cohort_import1
266-
expect(@output).to match(/\b60\b/)
267-
expect(@output).to match(/\b50\b/)
268-
expect(@output).to match(/\b40\b/)
202+
def then_i_should_see_a_limited_number_of_imports
203+
content_lines = @output.lines[2..-2]
204+
205+
expect(content_lines.count).to eq(3)
206+
207+
expect(content_lines[0]).to include(
208+
/^. \s coh \S+ \s+ #{@youngest_import.id} \b/x
209+
)
210+
expect(content_lines[1]).to include(
211+
/^. \s coh \S+ \s+ #{@middle_aged_import.id} \b/x
212+
)
213+
expect(content_lines[2]).to include(
214+
/^. \s coh \S+ \s+ #{@oldest_import.id} \b/x
215+
)
269216
end
270217

271218
def then_i_should_see_imports_for_that_organisation_only
272219
expect(@output).to include("ORG1")
273-
expect(@output).to match(/tea/) # team1 may be truncated
274220
expect(@output).not_to include("ORG2")
275-
# Should see both class and cohort imports from ORG1
276-
expect(@output).to match(/\bcla/)
277-
expect(@output).to match(/\bcoh/)
278221
end
279222

280223
def then_i_should_see_imports_for_that_workgroup_only
281-
expect(@output).to match(/alp/) # alpha may be truncated
282-
expect(@output).not_to match(/bet/) # beta should not appear
283-
# Should see both class and cohort imports from alpha workgroup
284-
expect(@output).to match(/\bcla/)
285-
expect(@output).to match(/\bcoh/)
224+
content_lines = @output.lines[2..-2]
225+
226+
expect(content_lines).to include(/^. \s coh \S+ \b .* \b wg1/x)
227+
expect(content_lines).to include(/^. \s cla \S+ \b .* \b wg1/x)
228+
expect(content_lines).not_to include(/wg2/)
286229
end
287230

288231
def then_i_should_see_imports_sorted_by_creation_date
289-
# May be truncated as "int" or "inter"
290-
lines = @output.lines.select { |line| line.match?(/\bint/) }
291-
292-
# The order should be newest first (cohort 30), then middle (class 20), then oldest (cohort 10)
293-
# All three should appear in the output
294-
expect(lines.join("\n")).to match(/\b30\b/)
295-
expect(lines.join("\n")).to match(/\b20\b/)
296-
expect(lines.join("\n")).to match(/\b10\b/)
297-
298-
# Extract the rows values from data lines
299-
# The table format is: │ type id created… processed… rows ods ...
300-
rows_values = []
301-
lines.each do |line|
302-
# Skip header and border lines
303-
if line.include?("╭") || line.include?("╰") || line.include?("type") ||
304-
line.strip.start_with?("│ type")
305-
next
306-
end
307-
308-
# Extract all numbers from the line
309-
numbers = line.scan(/\b\d+\b/).map(&:to_i)
310-
# The rows value should be in the list - looking for 10, 20, or 30
311-
rows_val = numbers.find { |n| [10, 20, 30].include?(n) }
312-
rows_values << rows_val if rows_val
313-
end
232+
content_lines = @output.lines[2..-2]
314233

315-
# Since they're created newest first: 30, 20, 10
316-
expect(rows_values).to eq([30, 20, 10])
234+
expect(content_lines[0]).to include(/^. \s coh \S+ \s+ #{@newest.id} \b/x)
235+
expect(content_lines[1]).to include(/^. \s cla \S+ \s+ #{@middle.id} \b/x)
236+
expect(content_lines[2]).to include(/^. \s coh \S+ \s+ #{@oldest.id} \b/x)
317237
end
318238
end

0 commit comments

Comments
 (0)