@@ -6,131 +6,122 @@ use dal_test::{
66use pretty_assertions_sorted:: assert_eq;
77use si_db:: {
88 DEFAULT_PAGE_SIZE ,
9+ MAX_REPORTS_PER_GROUP ,
910 PolicyReport ,
10- PolicyReportResult ,
1111} ;
1212
1313#[ test]
1414async fn end_to_end ( ctx : & mut DalContext ) -> Result < ( ) > {
15- // Create three reports to use throughout the test.
16- let divinity_report_name = "divinity original sin " . to_string ( ) ;
17- let divinity_report_policy = "swen vincke" . to_string ( ) ;
18- let divinity_report_report = "2014" . to_string ( ) ;
15+ let policy_name = " test-policy" . to_string ( ) ;
16+ let other_policy_name = "other-policy " . to_string ( ) ;
17+
18+ // Create multiple reports with the same name for pagination testing
1919 PolicyReport :: new_pass (
2020 ctx,
21- divinity_report_name . to_owned ( ) ,
22- divinity_report_policy . to_owned ( ) ,
23- divinity_report_report . to_owned ( ) ,
21+ policy_name . clone ( ) ,
22+ "policy document 1" . to_string ( ) ,
23+ "report 1" . to_string ( ) ,
2424 )
2525 . await ?;
2626
27- let pillars_report_name = "pillars of eternity" . to_string ( ) ;
28- let pillars_report_policy = "josh sawyer" . to_string ( ) ;
29- let pillars_report_report = "2015" . to_string ( ) ;
3027 PolicyReport :: new_pass (
3128 ctx,
32- pillars_report_name . to_owned ( ) ,
33- pillars_report_policy . to_owned ( ) ,
34- pillars_report_report . to_owned ( ) ,
29+ policy_name . clone ( ) ,
30+ "policy document 2" . to_string ( ) ,
31+ "report 2" . to_string ( ) ,
3532 )
3633 . await ?;
3734
38- let fail_report_name = "fail" . to_string ( ) ;
39- let fail_report_policy = "fail" . to_string ( ) ;
40- let fail_report_report = "fail" . to_string ( ) ;
4135 PolicyReport :: new_fail (
4236 ctx,
43- fail_report_name. to_owned ( ) ,
44- fail_report_policy. to_owned ( ) ,
45- fail_report_report. to_owned ( ) ,
37+ policy_name. clone ( ) ,
38+ "policy document 3" . to_string ( ) ,
39+ "report 3" . to_string ( ) ,
40+ )
41+ . await ?;
42+
43+ // Create a report with a different name to ensure filtering works
44+ PolicyReport :: new_pass (
45+ ctx,
46+ other_policy_name. clone ( ) ,
47+ "other policy document" . to_string ( ) ,
48+ "other report" . to_string ( ) ,
4649 )
4750 . await ?;
4851
49- // Test that asking for fewer results than the total works .
52+ // Test that fetching by name only returns reports with that name .
5053 {
51- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 1 ) , Some ( 1 ) ) . await ?;
54+ let batch = PolicyReport :: fetch_batch ( ctx, None , None , policy_name . clone ( ) ) . await ?;
5255 assert_eq ! (
53- 1 , // expected
56+ 3 , // expected
5457 batch. reports. len( ) // actual
5558 ) ;
5659 assert_eq ! (
5760 3 , // expected
5861 batch. total_report_count // actual
5962 ) ;
6063 assert_eq ! (
61- 1 , // expected
62- batch. page_size // actual
64+ DEFAULT_PAGE_SIZE , // expected
65+ batch. page_size // actual
6366 ) ;
6467 assert_eq ! (
6568 1 , // expected
6669 batch. page_number // actual
6770 ) ;
6871 assert_eq ! (
69- 3 , // expected
72+ 1 , // expected
7073 batch. total_page_count // actual
7174 ) ;
75+
76+ // Verify all reports have the correct name
77+ for report in & batch. reports {
78+ assert_eq ! ( & policy_name, & report. name) ;
79+ }
7280 }
7381
74- // Test that the reports look as we expect.
82+ // Test that fetching a different policy name only returns that policy
7583 {
76- let batch = PolicyReport :: fetch_batch ( ctx, None , None ) . await ?;
84+ let batch = PolicyReport :: fetch_batch ( ctx, None , None , other_policy_name . clone ( ) ) . await ?;
7785 assert_eq ! (
78- 3 , // expected
86+ 1 , // expected
87+ batch. reports. len( ) // actual
88+ ) ;
89+ assert_eq ! (
90+ 1 , // expected
91+ batch. total_report_count // actual
92+ ) ;
93+ assert_eq ! ( & other_policy_name, & batch. reports[ 0 ] . name) ;
94+ }
95+
96+ // Test that asking for fewer results than the total works with pagination.
97+ {
98+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 1 ) , Some ( 1 ) , policy_name. clone ( ) ) . await ?;
99+ assert_eq ! (
100+ 1 , // expected
79101 batch. reports. len( ) // actual
80102 ) ;
81103 assert_eq ! (
82104 3 , // expected
83105 batch. total_report_count // actual
84106 ) ;
85107 assert_eq ! (
86- DEFAULT_PAGE_SIZE , // expected
87- batch. page_size // actual
108+ 1 , // expected
109+ batch. page_size // actual
88110 ) ;
89111 assert_eq ! (
90112 1 , // expected
91113 batch. page_number // actual
92114 ) ;
93115 assert_eq ! (
94- 1 , // expected
116+ 3 , // expected
95117 batch. total_page_count // actual
96118 ) ;
97-
98- let expected = vec ! [
99- (
100- divinity_report_name,
101- divinity_report_policy,
102- divinity_report_report,
103- PolicyReportResult :: Pass ,
104- ) ,
105- (
106- fail_report_name. to_owned( ) ,
107- fail_report_policy. to_owned( ) ,
108- fail_report_report. to_owned( ) ,
109- PolicyReportResult :: Fail ,
110- ) ,
111- (
112- pillars_report_name,
113- pillars_report_policy,
114- pillars_report_report,
115- PolicyReportResult :: Pass ,
116- ) ,
117- ] ;
118-
119- // Sort the reports by name for the assertion.
120- let mut reports = batch. reports ;
121- let mut actual = Vec :: with_capacity ( reports. len ( ) ) ;
122- reports. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
123- for report in reports {
124- actual. push ( ( report. name , report. policy , report. report , report. result ) ) ;
125- }
126-
127- assert_eq ! ( expected, actual) ;
128119 }
129120
130121 // Test minimum page size and page number.
131122 {
132123 // Both are zero.
133- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 0 ) , Some ( 0 ) ) . await ?;
124+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 0 ) , Some ( 0 ) , policy_name . clone ( ) ) . await ?;
134125 assert_eq ! (
135126 1 , // expected
136127 batch. reports. len( ) // actual
@@ -153,7 +144,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
153144 ) ;
154145
155146 // Page size is zero.
156- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 0 ) , None ) . await ?;
147+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 0 ) , None , policy_name . clone ( ) ) . await ?;
157148 assert_eq ! (
158149 1 , // expected
159150 batch. reports. len( ) // actual
@@ -176,7 +167,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
176167 ) ;
177168
178169 // Page number is zero.
179- let batch = PolicyReport :: fetch_batch ( ctx, None , Some ( 0 ) ) . await ?;
170+ let batch = PolicyReport :: fetch_batch ( ctx, None , Some ( 0 ) , policy_name . clone ( ) ) . await ?;
180171 assert_eq ! (
181172 3 , // expected
182173 batch. reports. len( ) // actual
@@ -203,21 +194,21 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
203194 {
204195 PolicyReport :: new_fail (
205196 ctx,
206- "fourth" . to_string ( ) ,
197+ policy_name . clone ( ) ,
207198 "fourth" . to_string ( ) ,
208199 "fourth" . to_string ( ) ,
209200 )
210201 . await ?;
211202 PolicyReport :: new_fail (
212203 ctx,
213- "fifth" . to_string ( ) ,
204+ policy_name . clone ( ) ,
214205 "fifth" . to_string ( ) ,
215206 "fifth" . to_string ( ) ,
216207 )
217208 . await ?;
218209
219210 // Grab the first page.
220- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 1 ) ) . await ?;
211+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 1 ) , policy_name . clone ( ) ) . await ?;
221212 assert_eq ! (
222213 2 , // expected
223214 batch. reports. len( ) // actual
@@ -240,7 +231,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
240231 ) ;
241232
242233 // Grab the second page.
243- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 2 ) ) . await ?;
234+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 2 ) , policy_name . clone ( ) ) . await ?;
244235 assert_eq ! (
245236 2 , // expected
246237 batch. reports. len( ) // actual
@@ -263,7 +254,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
263254 ) ;
264255
265256 // Grab the third page.
266- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 3 ) ) . await ?;
257+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 3 ) , policy_name . clone ( ) ) . await ?;
267258 assert_eq ! (
268259 1 , // expected
269260 batch. reports. len( ) // actual
@@ -286,8 +277,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
286277 ) ;
287278
288279 // What if you ask for a fourth page that does not exist?.
289- let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 4 ) ) . await ?;
290- dbg ! ( & batch) ;
280+ let batch = PolicyReport :: fetch_batch ( ctx, Some ( 2 ) , Some ( 4 ) , policy_name. clone ( ) ) . await ?;
291281 assert_eq ! (
292282 0 , // expected
293283 batch. reports. len( ) // actual
@@ -310,7 +300,7 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
310300 ) ;
311301
312302 // No pagination, just to be sure!
313- let batch = PolicyReport :: fetch_batch ( ctx, None , None ) . await ?;
303+ let batch = PolicyReport :: fetch_batch ( ctx, None , None , policy_name . clone ( ) ) . await ?;
314304 assert_eq ! (
315305 5 , // expected
316306 batch. reports. len( ) // actual
@@ -335,3 +325,100 @@ async fn end_to_end(ctx: &mut DalContext) -> Result<()> {
335325
336326 Ok ( ( ) )
337327}
328+
329+ #[ test]
330+ async fn grouped_by_name ( ctx : & mut DalContext ) -> Result < ( ) > {
331+ // Test Case 1: Create a policy with 1 report
332+ let policy_one_report = "policy-with-one-report" . to_string ( ) ;
333+ PolicyReport :: new_pass (
334+ ctx,
335+ policy_one_report. clone ( ) ,
336+ "policy document 1" . to_string ( ) ,
337+ "report 1" . to_string ( ) ,
338+ )
339+ . await ?;
340+
341+ // Test Case 2: Create a policy with 2 reports
342+ let policy_two_reports = "policy-with-two-reports" . to_string ( ) ;
343+ PolicyReport :: new_pass (
344+ ctx,
345+ policy_two_reports. clone ( ) ,
346+ "policy document 1" . to_string ( ) ,
347+ "report 1" . to_string ( ) ,
348+ )
349+ . await ?;
350+ PolicyReport :: new_fail (
351+ ctx,
352+ policy_two_reports. clone ( ) ,
353+ "policy document 2" . to_string ( ) ,
354+ "report 2" . to_string ( ) ,
355+ )
356+ . await ?;
357+
358+ // Test Case 3: Create a policy with more than MAX_REPORTS_PER_GROUP reports
359+ let policy_many_reports = "policy-with-many-reports" . to_string ( ) ;
360+ for i in 1 ..=15 {
361+ PolicyReport :: new_pass (
362+ ctx,
363+ policy_many_reports. clone ( ) ,
364+ format ! ( "policy document {i}" ) ,
365+ format ! ( "report {i}" ) ,
366+ )
367+ . await ?;
368+ }
369+
370+ // Fetch the grouped reports
371+ let groups = PolicyReport :: fetch_grouped_by_name ( ctx) . await ?;
372+
373+ // Verify we got 3 groups
374+ assert_eq ! (
375+ 3 , // expected
376+ groups. len( ) // actual
377+ ) ;
378+
379+ // Sort groups by name for consistent ordering
380+ let mut groups = groups;
381+ groups. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
382+
383+ // Verify group 1: policy with many reports (should be limited to MAX_REPORTS_PER_GROUP)
384+ assert_eq ! ( & policy_many_reports, & groups[ 0 ] . name) ;
385+ assert_eq ! (
386+ MAX_REPORTS_PER_GROUP as usize , // expected
387+ groups[ 0 ] . results. len( ) // actual
388+ ) ;
389+ // Verify all reports have the correct name
390+ for report in & groups[ 0 ] . results {
391+ assert_eq ! ( & policy_many_reports, & report. name) ;
392+ }
393+
394+ // Verify we got exactly MAX_REPORTS_PER_GROUP reports, not all 15
395+ // This confirms the limit is working correctly
396+ assert ! (
397+ groups[ 0 ] . results. len( ) < 15 ,
398+ "Should have limited to {MAX_REPORTS_PER_GROUP} reports, not returned all 15"
399+ ) ;
400+
401+ // Note(victor): We cannot reliably test ORDER BY created_at DESC here because all reports
402+ // created within the same test transaction have identical created_at timestamps.
403+ // The SQL query is correct, but the ordering is indeterminate when timestamps are equal.
404+
405+ // Verify group 2: policy with one report
406+ assert_eq ! ( & policy_one_report, & groups[ 1 ] . name) ;
407+ assert_eq ! (
408+ 1 , // expected
409+ groups[ 1 ] . results. len( ) // actual
410+ ) ;
411+ assert_eq ! ( & policy_one_report, & groups[ 1 ] . results[ 0 ] . name) ;
412+
413+ // Verify group 3: policy with two reports
414+ assert_eq ! ( & policy_two_reports, & groups[ 2 ] . name) ;
415+ assert_eq ! (
416+ 2 , // expected
417+ groups[ 2 ] . results. len( ) // actual
418+ ) ;
419+ for report in & groups[ 2 ] . results {
420+ assert_eq ! ( & policy_two_reports, & report. name) ;
421+ }
422+
423+ Ok ( ( ) )
424+ }
0 commit comments