From 4059bdad943ebc7da38b0ba7965a9134e46ba4c4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 13 Mar 2022 01:37:03 +0100 Subject: inserts_yaml_in_database(): Generalise test code Extract the test code to a new function so that it can be reused to test other YAML inputs and database records. --- src/lib.rs | 76 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b858602..f0610fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,15 @@ pub fn insert( mod tests { use super::*; - #[test] - fn inserts_yaml_in_database() { + #[derive(Debug, PartialEq)] + struct TestRecord { + id: i8, + count: i16, + weight: f32, + description: String, + } + + fn test_yaml_insert(yaml_str: &str, expected: &[TestRecord]) { let mut conn = rusqlite::Connection::open_in_memory().unwrap(); conn.execute( @@ -39,38 +46,6 @@ mod tests { [] ).unwrap(); - #[derive(Debug, PartialEq)] - struct TestRecord { - id: i8, - count: i16, - weight: f32, - description: String, - } - - let description = r#"This is a test. - - Another paragraph - with a flowed line."#; - - let expected = TestRecord { - id: 1, - count: 99, - weight: 3.14, - description: r#"This is a test. -Another paragraph with a flowed line."#.to_owned(), - }; - - let yaml_str = format!( -r#"- description: >- - {} - count: {} - weight: {} -"#, - description, - expected.count, - expected.weight, - ); - let mut data = yaml_rust::YamlLoader::load_from_str(&yaml_str).unwrap(); insert(&mut conn, "test", &mut data); @@ -83,7 +58,7 @@ r#"- description: >- LIMIT 1; "#).unwrap(); - let got = stmt.query_row( + let rows = stmt.query_map( [], |row| { Ok( @@ -97,12 +72,43 @@ r#"- description: >- } ).unwrap(); + let got: Vec = rows.map(|r| r.unwrap()).collect(); + assert_eq!(expected, got); } conn.close().unwrap(); } + #[test] + fn inserts_yaml_in_database() { + let expected = TestRecord { + id: 1, + count: 99, + weight: 3.14, + description: r#"This is a test. +Another paragraph with a flowed line."#.to_owned(), + }; + + let description = r#"This is a test. + + Another paragraph + with a flowed line."#; + + let yaml_str = format!( +r#"- description: >- + {} + count: {} + weight: {} +"#, + description, + expected.count, + expected.weight, + ); + + test_yaml_insert(&yaml_str, &vec![expected]); + } + #[test] fn ignores_yaml_fields_that_are_not_column_names() { } -- cgit v1.2.3