diff options
author | Teddy Wing | 2022-03-13 01:37:03 +0100 |
---|---|---|
committer | Teddy Wing | 2022-03-13 01:37:03 +0100 |
commit | 4059bdad943ebc7da38b0ba7965a9134e46ba4c4 (patch) | |
tree | 63918df500fbe4781c065fbd08fa5f3eedb66ba8 /src | |
parent | e88bcdebd09d3644d8df1dd04386f121d83ef5da (diff) | |
download | yaqlite-4059bdad943ebc7da38b0ba7965a9134e46ba4c4.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 76 |
1 files changed, 41 insertions, 35 deletions
@@ -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,6 +72,8 @@ r#"- description: >- } ).unwrap(); + let got: Vec<TestRecord> = rows.map(|r| r.unwrap()).collect(); + assert_eq!(expected, got); } @@ -104,6 +81,35 @@ r#"- description: >- } #[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() { } } |