aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-03-13 01:37:03 +0100
committerTeddy Wing2022-03-13 01:37:03 +0100
commit4059bdad943ebc7da38b0ba7965a9134e46ba4c4 (patch)
tree63918df500fbe4781c065fbd08fa5f3eedb66ba8
parente88bcdebd09d3644d8df1dd04386f121d83ef5da (diff)
downloadyaqlite-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.
-rw-r--r--src/lib.rs76
1 files changed, 41 insertions, 35 deletions
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,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() {
}
}