Skip to content

Commit 49737d8

Browse files
Migrate physical plan tests to insta (Part-2) (#15364)
* Migrated tests to insta in aggregate/topk/heap.rs * Migrated tests to insta in joins/nested_loop_join.rs * Migrated tests to insta in joins/sort_merge_join.rs * Remove snapshot uploaded by mistake * Migrated tests to insta in repartition/mod.rs * Fix batches_to_sort_string problem and formatting
1 parent a2db3f0 commit 49737d8

File tree

4 files changed

+456
-504
lines changed

4 files changed

+456
-504
lines changed

datafusion/physical-plan/src/aggregates/topk/heap.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ pub fn new_heap(
485485

486486
#[cfg(test)]
487487
mod tests {
488+
use insta::assert_snapshot;
489+
488490
use super::*;
489491

490492
#[test]
@@ -494,10 +496,9 @@ mod tests {
494496
heap.append_or_replace(1, 1, &mut map);
495497

496498
let actual = heap.to_string();
497-
let expected = r#"
499+
assert_snapshot!(actual, @r#"
498500
val=1 idx=0, bucket=1
499-
"#;
500-
assert_eq!(actual.trim(), expected.trim());
501+
"#);
501502

502503
Ok(())
503504
}
@@ -514,11 +515,10 @@ val=1 idx=0, bucket=1
514515
assert_eq!(map, vec![(2, 0), (1, 1)]);
515516

516517
let actual = heap.to_string();
517-
let expected = r#"
518+
assert_snapshot!(actual, @r#"
518519
val=2 idx=0, bucket=2
519520
└── val=1 idx=1, bucket=1
520-
"#;
521-
assert_eq!(actual.trim(), expected.trim());
521+
"#);
522522

523523
Ok(())
524524
}
@@ -532,22 +532,20 @@ val=2 idx=0, bucket=2
532532
heap.append_or_replace(2, 2, &mut map);
533533
heap.append_or_replace(3, 3, &mut map);
534534
let actual = heap.to_string();
535-
let expected = r#"
535+
assert_snapshot!(actual, @r#"
536536
val=3 idx=0, bucket=3
537537
├── val=1 idx=1, bucket=1
538538
└── val=2 idx=2, bucket=2
539-
"#;
540-
assert_eq!(actual.trim(), expected.trim());
539+
"#);
541540

542541
let mut map = vec![];
543542
heap.append_or_replace(0, 0, &mut map);
544543
let actual = heap.to_string();
545-
let expected = r#"
544+
assert_snapshot!(actual, @r#"
546545
val=2 idx=0, bucket=2
547546
├── val=1 idx=1, bucket=1
548547
└── val=0 idx=2, bucket=0
549-
"#;
550-
assert_eq!(actual.trim(), expected.trim());
548+
"#);
551549
assert_eq!(map, vec![(2, 0), (0, 2)]);
552550

553551
Ok(())
@@ -563,24 +561,22 @@ val=2 idx=0, bucket=2
563561
heap.append_or_replace(3, 3, &mut map);
564562
heap.append_or_replace(4, 4, &mut map);
565563
let actual = heap.to_string();
566-
let expected = r#"
564+
assert_snapshot!(actual, @r#"
567565
val=4 idx=0, bucket=4
568566
├── val=3 idx=1, bucket=3
569567
│ └── val=1 idx=3, bucket=1
570568
└── val=2 idx=2, bucket=2
571-
"#;
572-
assert_eq!(actual.trim(), expected.trim());
569+
"#);
573570

574571
let mut map = vec![];
575572
heap.replace_if_better(1, 0, &mut map);
576573
let actual = heap.to_string();
577-
let expected = r#"
574+
assert_snapshot!(actual, @r#"
578575
val=4 idx=0, bucket=4
579576
├── val=1 idx=1, bucket=1
580577
│ └── val=0 idx=3, bucket=3
581578
└── val=2 idx=2, bucket=2
582-
"#;
583-
assert_eq!(actual.trim(), expected.trim());
579+
"#);
584580
assert_eq!(map, vec![(1, 1), (3, 3)]);
585581

586582
Ok(())
@@ -595,11 +591,10 @@ val=4 idx=0, bucket=4
595591
heap.append_or_replace(2, 2, &mut map);
596592

597593
let actual = heap.to_string();
598-
let expected = r#"
594+
assert_snapshot!(actual, @r#"
599595
val=2 idx=0, bucket=2
600596
└── val=1 idx=1, bucket=1
601-
"#;
602-
assert_eq!(actual.trim(), expected.trim());
597+
"#);
603598

604599
assert_eq!(heap.worst_val(), Some(&2));
605600
assert_eq!(heap.worst_map_idx(), 2);
@@ -616,11 +611,10 @@ val=2 idx=0, bucket=2
616611
heap.append_or_replace(2, 2, &mut map);
617612

618613
let actual = heap.to_string();
619-
let expected = r#"
614+
assert_snapshot!(actual, @r#"
620615
val=2 idx=0, bucket=2
621616
└── val=1 idx=1, bucket=1
622-
"#;
623-
assert_eq!(actual.trim(), expected.trim());
617+
"#);
624618

625619
let (vals, map_idxs) = heap.drain();
626620
assert_eq!(vals, vec![1, 2]);
@@ -639,20 +633,18 @@ val=2 idx=0, bucket=2
639633
heap.append_or_replace(2, 2, &mut map);
640634

641635
let actual = heap.to_string();
642-
let expected = r#"
636+
assert_snapshot!(actual, @r#"
643637
val=2 idx=0, bucket=2
644638
└── val=1 idx=1, bucket=1
645-
"#;
646-
assert_eq!(actual.trim(), expected.trim());
639+
"#);
647640

648641
let numbers = vec![(0, 1), (1, 2)];
649642
heap.renumber(numbers.as_slice());
650643
let actual = heap.to_string();
651-
let expected = r#"
644+
assert_snapshot!(actual, @r#"
652645
val=2 idx=0, bucket=1
653646
└── val=1 idx=1, bucket=2
654-
"#;
655-
assert_eq!(actual.trim(), expected.trim());
647+
"#);
656648

657649
Ok(())
658650
}

datafusion/physical-plan/src/joins/nested_loop_join.rs

Lines changed: 78 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,13 +1047,15 @@ pub(crate) mod tests {
10471047
use arrow::array::Int32Array;
10481048
use arrow::compute::SortOptions;
10491049
use arrow::datatypes::{DataType, Field};
1050-
use datafusion_common::{assert_batches_sorted_eq, assert_contains, ScalarValue};
1050+
use datafusion_common::test_util::batches_to_sort_string;
1051+
use datafusion_common::{assert_contains, ScalarValue};
10511052
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
10521053
use datafusion_expr::Operator;
10531054
use datafusion_physical_expr::expressions::{BinaryExpr, Literal};
10541055
use datafusion_physical_expr::{Partitioning, PhysicalExpr};
10551056
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
10561057

1058+
use insta::assert_snapshot;
10571059
use rstest::rstest;
10581060

10591061
fn build_table(
@@ -1216,15 +1218,13 @@ pub(crate) mod tests {
12161218
)
12171219
.await?;
12181220
assert_eq!(columns, vec!["a1", "b1", "c1", "a2", "b2", "c2"]);
1219-
let expected = [
1220-
"+----+----+----+----+----+----+",
1221-
"| a1 | b1 | c1 | a2 | b2 | c2 |",
1222-
"+----+----+----+----+----+----+",
1223-
"| 5 | 5 | 50 | 2 | 2 | 80 |",
1224-
"+----+----+----+----+----+----+",
1225-
];
1226-
1227-
assert_batches_sorted_eq!(expected, &batches);
1221+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1222+
+----+----+----+----+----+----+
1223+
| a1 | b1 | c1 | a2 | b2 | c2 |
1224+
+----+----+----+----+----+----+
1225+
| 5 | 5 | 50 | 2 | 2 | 80 |
1226+
+----+----+----+----+----+----+
1227+
"#);
12281228

12291229
Ok(())
12301230
}
@@ -1245,17 +1245,15 @@ pub(crate) mod tests {
12451245
)
12461246
.await?;
12471247
assert_eq!(columns, vec!["a1", "b1", "c1", "a2", "b2", "c2"]);
1248-
let expected = [
1249-
"+----+----+-----+----+----+----+",
1250-
"| a1 | b1 | c1 | a2 | b2 | c2 |",
1251-
"+----+----+-----+----+----+----+",
1252-
"| 11 | 8 | 110 | | | |",
1253-
"| 5 | 5 | 50 | 2 | 2 | 80 |",
1254-
"| 9 | 8 | 90 | | | |",
1255-
"+----+----+-----+----+----+----+",
1256-
];
1257-
1258-
assert_batches_sorted_eq!(expected, &batches);
1248+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1249+
+----+----+-----+----+----+----+
1250+
| a1 | b1 | c1 | a2 | b2 | c2 |
1251+
+----+----+-----+----+----+----+
1252+
| 11 | 8 | 110 | | | |
1253+
| 5 | 5 | 50 | 2 | 2 | 80 |
1254+
| 9 | 8 | 90 | | | |
1255+
+----+----+-----+----+----+----+
1256+
"#);
12591257

12601258
Ok(())
12611259
}
@@ -1276,17 +1274,15 @@ pub(crate) mod tests {
12761274
)
12771275
.await?;
12781276
assert_eq!(columns, vec!["a1", "b1", "c1", "a2", "b2", "c2"]);
1279-
let expected = [
1280-
"+----+----+----+----+----+-----+",
1281-
"| a1 | b1 | c1 | a2 | b2 | c2 |",
1282-
"+----+----+----+----+----+-----+",
1283-
"| | | | 10 | 10 | 100 |",
1284-
"| | | | 12 | 10 | 40 |",
1285-
"| 5 | 5 | 50 | 2 | 2 | 80 |",
1286-
"+----+----+----+----+----+-----+",
1287-
];
1288-
1289-
assert_batches_sorted_eq!(expected, &batches);
1277+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1278+
+----+----+----+----+----+-----+
1279+
| a1 | b1 | c1 | a2 | b2 | c2 |
1280+
+----+----+----+----+----+-----+
1281+
| | | | 10 | 10 | 100 |
1282+
| | | | 12 | 10 | 40 |
1283+
| 5 | 5 | 50 | 2 | 2 | 80 |
1284+
+----+----+----+----+----+-----+
1285+
"#);
12901286

12911287
Ok(())
12921288
}
@@ -1307,19 +1303,17 @@ pub(crate) mod tests {
13071303
)
13081304
.await?;
13091305
assert_eq!(columns, vec!["a1", "b1", "c1", "a2", "b2", "c2"]);
1310-
let expected = [
1311-
"+----+----+-----+----+----+-----+",
1312-
"| a1 | b1 | c1 | a2 | b2 | c2 |",
1313-
"+----+----+-----+----+----+-----+",
1314-
"| | | | 10 | 10 | 100 |",
1315-
"| | | | 12 | 10 | 40 |",
1316-
"| 11 | 8 | 110 | | | |",
1317-
"| 5 | 5 | 50 | 2 | 2 | 80 |",
1318-
"| 9 | 8 | 90 | | | |",
1319-
"+----+----+-----+----+----+-----+",
1320-
];
1321-
1322-
assert_batches_sorted_eq!(expected, &batches);
1306+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1307+
+----+----+-----+----+----+-----+
1308+
| a1 | b1 | c1 | a2 | b2 | c2 |
1309+
+----+----+-----+----+----+-----+
1310+
| | | | 10 | 10 | 100 |
1311+
| | | | 12 | 10 | 40 |
1312+
| 11 | 8 | 110 | | | |
1313+
| 5 | 5 | 50 | 2 | 2 | 80 |
1314+
| 9 | 8 | 90 | | | |
1315+
+----+----+-----+----+----+-----+
1316+
"#);
13231317

13241318
Ok(())
13251319
}
@@ -1340,15 +1334,13 @@ pub(crate) mod tests {
13401334
)
13411335
.await?;
13421336
assert_eq!(columns, vec!["a1", "b1", "c1"]);
1343-
let expected = [
1344-
"+----+----+----+",
1345-
"| a1 | b1 | c1 |",
1346-
"+----+----+----+",
1347-
"| 5 | 5 | 50 |",
1348-
"+----+----+----+",
1349-
];
1350-
1351-
assert_batches_sorted_eq!(expected, &batches);
1337+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1338+
+----+----+----+
1339+
| a1 | b1 | c1 |
1340+
+----+----+----+
1341+
| 5 | 5 | 50 |
1342+
+----+----+----+
1343+
"#);
13521344

13531345
Ok(())
13541346
}
@@ -1369,16 +1361,14 @@ pub(crate) mod tests {
13691361
)
13701362
.await?;
13711363
assert_eq!(columns, vec!["a1", "b1", "c1"]);
1372-
let expected = [
1373-
"+----+----+-----+",
1374-
"| a1 | b1 | c1 |",
1375-
"+----+----+-----+",
1376-
"| 11 | 8 | 110 |",
1377-
"| 9 | 8 | 90 |",
1378-
"+----+----+-----+",
1379-
];
1380-
1381-
assert_batches_sorted_eq!(expected, &batches);
1364+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1365+
+----+----+-----+
1366+
| a1 | b1 | c1 |
1367+
+----+----+-----+
1368+
| 11 | 8 | 110 |
1369+
| 9 | 8 | 90 |
1370+
+----+----+-----+
1371+
"#);
13821372

13831373
Ok(())
13841374
}
@@ -1399,15 +1389,13 @@ pub(crate) mod tests {
13991389
)
14001390
.await?;
14011391
assert_eq!(columns, vec!["a2", "b2", "c2"]);
1402-
let expected = [
1403-
"+----+----+----+",
1404-
"| a2 | b2 | c2 |",
1405-
"+----+----+----+",
1406-
"| 2 | 2 | 80 |",
1407-
"+----+----+----+",
1408-
];
1409-
1410-
assert_batches_sorted_eq!(expected, &batches);
1392+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1393+
+----+----+----+
1394+
| a2 | b2 | c2 |
1395+
+----+----+----+
1396+
| 2 | 2 | 80 |
1397+
+----+----+----+
1398+
"#);
14111399

14121400
Ok(())
14131401
}
@@ -1428,16 +1416,14 @@ pub(crate) mod tests {
14281416
)
14291417
.await?;
14301418
assert_eq!(columns, vec!["a2", "b2", "c2"]);
1431-
let expected = [
1432-
"+----+----+-----+",
1433-
"| a2 | b2 | c2 |",
1434-
"+----+----+-----+",
1435-
"| 10 | 10 | 100 |",
1436-
"| 12 | 10 | 40 |",
1437-
"+----+----+-----+",
1438-
];
1439-
1440-
assert_batches_sorted_eq!(expected, &batches);
1419+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1420+
+----+----+-----+
1421+
| a2 | b2 | c2 |
1422+
+----+----+-----+
1423+
| 10 | 10 | 100 |
1424+
| 12 | 10 | 40 |
1425+
+----+----+-----+
1426+
"#);
14411427

14421428
Ok(())
14431429
}
@@ -1458,17 +1444,15 @@ pub(crate) mod tests {
14581444
)
14591445
.await?;
14601446
assert_eq!(columns, vec!["a1", "b1", "c1", "mark"]);
1461-
let expected = [
1462-
"+----+----+-----+-------+",
1463-
"| a1 | b1 | c1 | mark |",
1464-
"+----+----+-----+-------+",
1465-
"| 11 | 8 | 110 | false |",
1466-
"| 5 | 5 | 50 | true |",
1467-
"| 9 | 8 | 90 | false |",
1468-
"+----+----+-----+-------+",
1469-
];
1470-
1471-
assert_batches_sorted_eq!(expected, &batches);
1447+
assert_snapshot!(batches_to_sort_string(&batches), @r#"
1448+
+----+----+-----+-------+
1449+
| a1 | b1 | c1 | mark |
1450+
+----+----+-----+-------+
1451+
| 11 | 8 | 110 | false |
1452+
| 5 | 5 | 50 | true |
1453+
| 9 | 8 | 90 | false |
1454+
+----+----+-----+-------+
1455+
"#);
14721456

14731457
Ok(())
14741458
}

0 commit comments

Comments
 (0)