Apache DataFusion SQL Query Engine
test(datafusion-cli): migrate tests to `insta` in `print_format.rs` (#16993)
Thearas31c3e5879759
diff --git a/datafusion-cli/src/print_format.rs b/datafusion-cli/src/print_format.rsindex 1d6a8396ae..56bdb15a31 100644--- a/datafusion-cli/src/print_format.rs+++ b/datafusion-cli/src/print_format.rs@@ -221,6 +221,7 @@ mod tests {use arrow::array::Int32Array;use arrow::datatypes::{DataType, Field, Schema};+ use insta::{allow_duplicates, assert_snapshot};#[test]fn print_empty() {@@ -232,249 +233,204 @@ mod tests {PrintFormat::Automatic,] {// no output for empty batches, even with header set- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(format).with_schema(three_column_schema()).with_batches(vec![])- .with_expected(&[""]).run();+ assert_eq!(output, "")}// output column headers for empty batches when format is Table- #[rustfmt::skip]- let expected = &[- "+---+---+---+",- "| a | b | c |",- "+---+---+---+",- "+---+---+---+",- ];- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_schema(three_column_schema()).with_batches(vec![])- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---+---+---++ | a | b | c |+ +---+---+---++ +---+---+---++ "#);}#[test]fn print_csv_no_header() {- #[rustfmt::skip]- let expected = &[- "1,4,7",- "2,5,8",- "3,6,9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Csv).with_batches(split_batch(three_column_batch())).with_header(WithHeader::No)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ 1,4,7+ 2,5,8+ 3,6,9+ "#);}#[test]fn print_csv_with_header() {- #[rustfmt::skip]- let expected = &[- "a,b,c",- "1,4,7",- "2,5,8",- "3,6,9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Csv).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Yes)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ a,b,c+ 1,4,7+ 2,5,8+ 3,6,9+ "#);}#[test]fn print_tsv_no_header() {- #[rustfmt::skip]- let expected = &[- "1\t4\t7",- "2\t5\t8",- "3\t6\t9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Tsv).with_batches(split_batch(three_column_batch())).with_header(WithHeader::No)- .with_expected(expected).run();+ assert_snapshot!(output, @"+ 1\t4\t7+ 2\t5\t8+ 3\t6\t9+ ")}#[test]fn print_tsv_with_header() {- #[rustfmt::skip]- let expected = &[- "a\tb\tc",- "1\t4\t7",- "2\t5\t8",- "3\t6\t9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Tsv).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Yes)- .with_expected(expected).run();+ assert_snapshot!(output, @"+ a\tb\tc+ 1\t4\t7+ 2\t5\t8+ 3\t6\t9+ ");}#[test]fn print_table() {- let expected = &[- "+---+---+---+",- "| a | b | c |",- "+---+---+---+",- "| 1 | 4 | 7 |",- "| 2 | 5 | 8 |",- "| 3 | 6 | 9 |",- "+---+---+---+",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Ignored)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---+---+---++ | a | b | c |+ +---+---+---++ | 1 | 4 | 7 |+ | 2 | 5 | 8 |+ | 3 | 6 | 9 |+ +---+---+---++ "#);}#[test]fn print_json() {- let expected =- &[r#"[{"a":1,"b":4,"c":7},{"a":2,"b":5,"c":8},{"a":3,"b":6,"c":9}]"#];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Json).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Ignored)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ [{"a":1,"b":4,"c":7},{"a":2,"b":5,"c":8},{"a":3,"b":6,"c":9}]+ "#);}#[test]fn print_ndjson() {- let expected = &[- r#"{"a":1,"b":4,"c":7}"#,- r#"{"a":2,"b":5,"c":8}"#,- r#"{"a":3,"b":6,"c":9}"#,- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::NdJson).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Ignored)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ {"a":1,"b":4,"c":7}+ {"a":2,"b":5,"c":8}+ {"a":3,"b":6,"c":9}+ "#);}#[test]fn print_automatic_no_header() {- #[rustfmt::skip]- let expected = &[- "1,4,7",- "2,5,8",- "3,6,9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Automatic).with_batches(split_batch(three_column_batch())).with_header(WithHeader::No)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ 1,4,7+ 2,5,8+ 3,6,9+ "#);}#[test]fn print_automatic_with_header() {- #[rustfmt::skip]- let expected = &[- "a,b,c",- "1,4,7",- "2,5,8",- "3,6,9",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Automatic).with_batches(split_batch(three_column_batch())).with_header(WithHeader::Yes)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ a,b,c+ 1,4,7+ 2,5,8+ 3,6,9+ "#);}#[test]fn print_maxrows_unlimited() {- #[rustfmt::skip]- let expected = &[- "+---+",- "| a |",- "+---+",- "| 1 |",- "| 2 |",- "| 3 |",- "+---+",- ];-// should print out entire output with no truncation if unlimited or// limit greater than number of batches or equal to the number of batchesfor max_rows in [MaxRows::Unlimited, MaxRows::Limited(5), MaxRows::Limited(3)] {- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_schema(one_column_schema()).with_batches(vec![one_column_batch()]).with_maxrows(max_rows)- .with_expected(expected).run();+ allow_duplicates! {+ assert_snapshot!(output, @r#"+ +---++ | a |+ +---++ | 1 |+ | 2 |+ | 3 |+ +---++ "#);+ }}}#[test]fn print_maxrows_limited_one_batch() {- #[rustfmt::skip]- let expected = &[- "+---+",- "| a |",- "+---+",- "| 1 |",- "| . |",- "| . |",- "| . |",- "+---+",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_batches(vec![one_column_batch()]).with_maxrows(MaxRows::Limited(1))- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---++ | a |+ +---++ | 1 |+ | . |+ | . |+ | . |+ +---++ "#);}#[test]fn print_maxrows_limited_multi_batched() {- #[rustfmt::skip]- let expected = &[- "+---+",- "| a |",- "+---+",- "| 1 |",- "| 2 |",- "| 3 |",- "| 1 |",- "| 2 |",- "| . |",- "| . |",- "| . |",- "+---+",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_batches(vec![one_column_batch(),@@ -482,8 +438,21 @@ mod tests {one_column_batch(),]).with_maxrows(MaxRows::Limited(5))- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---++ | a |+ +---++ | 1 |+ | 2 |+ | 3 |+ | 1 |+ | 2 |+ | . |+ | . |+ | . |+ +---++ "#);}#[test]@@ -491,22 +460,19 @@ mod tests {let batch = one_column_batch();let empty_batch = RecordBatch::new_empty(batch.schema());- #[rustfmt::skip]- let expected =&[- "+---+",- "| a |",- "+---+",- "| 1 |",- "| 2 |",- "| 3 |",- "+---+",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_batches(vec![empty_batch.clone(), batch, empty_batch])- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---++ | a |+ +---++ | 1 |+ | 2 |+ | 3 |+ +---++ "#);}#[test]@@ -514,32 +480,28 @@ mod tests {let empty_batch = RecordBatch::new_empty(one_column_batch().schema());// Print column headers for empty batch when format is Table- #[rustfmt::skip]- let expected =&[- "+---+",- "| a |",- "+---+",- "+---+",- ];-- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_schema(one_column_schema()).with_batches(vec![empty_batch]).with_header(WithHeader::Yes)- .with_expected(expected).run();+ assert_snapshot!(output, @r#"+ +---++ | a |+ +---++ +---++ "#);// No output for empty batch when schema contains no columnslet empty_batch = RecordBatch::new_empty(Arc::new(Schema::empty()));- let expected = &[""];- PrintBatchesTest::new()+ let output = PrintBatchesTest::new().with_format(PrintFormat::Table).with_schema(Arc::new(Schema::empty())).with_batches(vec![empty_batch]).with_header(WithHeader::Yes)- .with_expected(expected).run();+ assert_eq!(output, "")}#[derive(Debug)]@@ -549,7 +511,6 @@ mod tests {batches: Vec<RecordBatch>,maxrows: MaxRows,with_header: WithHeader,- expected: Vec<&'static str>,}/// How to test with_header@@ -569,7 +530,6 @@ mod tests {batches: vec![],maxrows: MaxRows::Unlimited,with_header: WithHeader::Ignored,- expected: vec![],}}@@ -603,25 +563,9 @@ mod tests {self}- /// set expected output- fn with_expected(mut self, expected: &[&'static str]) -> Self {- self.expected = expected.to_vec();- self- }-/// run the test- fn run(self) {- let actual = self.output();- let actual: Vec<_> = actual.trim_end().split('\n').collect();- let expected = self.expected;- assert_eq!(- actual, expected,- "\n\nactual:\n{actual:#?}\n\nexpected:\n{expected:#?}"- );- }-/// formats batches using parameters and returns the resulting output- fn output(&self) -> String {+ fn run(self) -> String {match self.with_header {WithHeader::Yes => self.output_with_header(true),WithHeader::No => self.output_with_header(false),