Skip to content

Commit

Permalink
refactored test
Browse files Browse the repository at this point in the history
  • Loading branch information
scMarkus committed Aug 23, 2023
1 parent df45438 commit 3512a7a
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/codecs/src/encoding/format/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,50 +429,45 @@ mod tests {
let mut always_bytes = BytesMut::new();
let mut non_numeric_bytes = BytesMut::new();

let mut default_serializer = CsvSerializerConfig::new(CsvSerializerOptions {
CsvSerializerConfig::new(CsvSerializerOptions {
fields: fields.clone(),
..Default::default()
})
.build()
.unwrap()
.encode(event.clone(), &mut default_bytes)
.unwrap();

let mut never_serializer = CsvSerializerConfig::new(CsvSerializerOptions {
CsvSerializerConfig::new(CsvSerializerOptions {
fields: fields.clone(),
quote_style: QuoteStyle::Never,
..Default::default()
})
.build()
.unwrap()
.encode(event.clone(), &mut never_bytes)
.unwrap();

let mut always_serializer = CsvSerializerConfig::new(CsvSerializerOptions {
CsvSerializerConfig::new(CsvSerializerOptions {
fields: fields.clone(),
quote_style: QuoteStyle::Always,
..Default::default()
})
.build()
.unwrap()
.encode(event.clone(), &mut always_bytes)
.unwrap();

let mut non_numeric_serializer = CsvSerializerConfig::new(CsvSerializerOptions {
CsvSerializerConfig::new(CsvSerializerOptions {
fields: fields.clone(),
quote_style: QuoteStyle::NonNumeric,
..Default::default()
})
.build()
.unwrap()
.encode(event.clone(), &mut non_numeric_bytes)
.unwrap();

default_serializer
.encode(event.clone(), &mut default_bytes)
.unwrap();
never_serializer
.encode(event.clone(), &mut never_bytes)
.unwrap();
always_serializer
.encode(event.clone(), &mut always_bytes)
.unwrap();
non_numeric_serializer
.encode(event.clone(), &mut non_numeric_bytes)
.unwrap();

assert_eq!(
default_bytes.freeze(),
b"hello world,1,\"foo\"\"bar\",\"baz,bas\"".as_slice()
Expand Down Expand Up @@ -560,4 +555,22 @@ mod tests {

assert_eq!(bytes.freeze(), b"foo bar".as_slice());
}

#[test]
fn multiple_events() {
let (fields, event1) = make_event_with_fields(vec![("field1", "foo\"")]);
let (_, event2) = make_event_with_fields(vec![("field1", "\"bar")]);
let opts = CsvSerializerOptions {
fields,
..Default::default()
};
let config = CsvSerializerConfig::new(opts);
let mut serializer = config.build().unwrap();
let mut bytes = BytesMut::new();

serializer.encode(event1, &mut bytes).unwrap();
serializer.encode(event2, &mut bytes).unwrap();

assert_eq!(bytes.freeze(), b"\"foo\"\"\"\"\"bar\"".as_slice());
}
}

0 comments on commit 3512a7a

Please sign in to comment.