MCPcopy Create free account
hub / github.com/apache/arrow-rs / test_write_csv_custom_options

Function test_write_csv_custom_options

arrow-csv/src/writer.rs:813–893  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

811
812 #[test]
813 fn test_write_csv_custom_options() {
814 let schema = Schema::new(vec![
815 Field::new("c1", DataType::Utf8, false),
816 Field::new("c2", DataType::Float64, true),
817 Field::new("c3", DataType::UInt32, false),
818 Field::new("c4", DataType::Boolean, true),
819 Field::new("c6", DataType::Time32(TimeUnit::Second), false),
820 ]);
821
822 let c1 = StringArray::from(vec![
823 "Lorem ipsum \ndolor sit amet",
824 "consectetur \"adipiscing\" elit",
825 "sed do eiusmod tempor",
826 ]);
827 let c2 =
828 PrimitiveArray::<Float64Type>::from(vec![Some(123.564532), None, Some(-556132.25)]);
829 let c3 = PrimitiveArray::<UInt32Type>::from(vec![3, 2, 1]);
830 let c4 = BooleanArray::from(vec![Some(true), Some(false), None]);
831 let c6 = Time32SecondArray::from(vec![1234, 24680, 85563]);
832
833 let batch = RecordBatch::try_new(
834 Arc::new(schema),
835 vec![
836 Arc::new(c1),
837 Arc::new(c2),
838 Arc::new(c3),
839 Arc::new(c4),
840 Arc::new(c6),
841 ],
842 )
843 .unwrap();
844
845 let mut file = tempfile::tempfile().unwrap();
846
847 let builder = WriterBuilder::new()
848 .with_header(false)
849 .with_delimiter(b'|')
850 .with_quote(b'\'')
851 .with_null("NULL".to_string())
852 .with_time_format("%r".to_string());
853 let mut writer = builder.build(&mut file);
854 let batches = vec![&batch];
855 for batch in batches {
856 writer.write(batch).unwrap();
857 }
858 drop(writer);
859
860 // check that file was written successfully
861 file.rewind().unwrap();
862 let mut buffer: Vec<u8> = vec![];
863 file.read_to_end(&mut buffer).unwrap();
864
865 assert_eq!(
866 "'Lorem ipsum \ndolor sit amet'|123.564532|3|true|12:20:34 AM\nconsectetur \"adipiscing\" elit|NULL|2|false|06:51:20 AM\nsed do eiusmod tempor|-556132.25|1|NULL|11:46:03 PM\n"
867 .to_string(),
868 String::from_utf8(buffer).unwrap()
869 );
870

Callers

nothing calls this directly

Calls 10

try_newFunction · 0.85
with_double_quoteMethod · 0.80
with_time_formatMethod · 0.45
with_nullMethod · 0.45
with_quoteMethod · 0.45
with_delimiterMethod · 0.45
with_headerMethod · 0.45
buildMethod · 0.45
writeMethod · 0.45
with_escapeMethod · 0.45

Tested by

no test coverage detected