diff options
author | Teddy Wing | 2022-03-13 01:01:46 +0100 |
---|---|---|
committer | Teddy Wing | 2022-03-13 01:01:46 +0100 |
commit | e88bcdebd09d3644d8df1dd04386f121d83ef5da (patch) | |
tree | 20792571d2b4ee1a54d04bdde6f416d16ea38674 | |
parent | 8dd59ee6fee0f023a75d4657ad07f6f09b14fc9c (diff) | |
download | yaqlite-e88bcdebd09d3644d8df1dd04386f121d83ef5da.tar.bz2 |
yaqlite::insert(): Add test
Check that the function inserts a record into the database based on a
YAML input.
Adjust the `extract()` function to take the table name as an argument.
-rw-r--r-- | src/lib.rs | 92 | ||||
-rw-r--r-- | src/yaml.rs | 7 |
2 files changed, 96 insertions, 3 deletions
@@ -12,8 +12,98 @@ pub fn insert( for mut doc in data { let tx = dbconn.transaction().unwrap(); - crate::yaml::extract(&mut doc, &tx, &table_columns); + crate::yaml::extract(&mut doc, &tx, &table_name, &table_columns); tx.commit().unwrap(); } } + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn inserts_yaml_in_database() { + let mut conn = rusqlite::Connection::open_in_memory().unwrap(); + + conn.execute( + r#" + CREATE TABLE "test" ( + id INTEGER PRIMARY KEY, + count INTEGER, + weight REAL, + description TEXT + ); + "#, + [] + ).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); + + { + let mut stmt = conn.prepare(r#" + SELECT + id, count, weight, description + FROM "test" + LIMIT 1; + "#).unwrap(); + + let got = stmt.query_row( + [], + |row| { + Ok( + TestRecord { + id: row.get(0).unwrap(), + count: row.get(1).unwrap(), + weight: row.get(2).unwrap(), + description: row.get(3).unwrap(), + } + ) + } + ).unwrap(); + + assert_eq!(expected, got); + } + + conn.close().unwrap(); + } + + #[test] + fn ignores_yaml_fields_that_are_not_column_names() { + } +} diff --git a/src/yaml.rs b/src/yaml.rs index 532658a..2517d62 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -12,12 +12,13 @@ pub use sql::*; pub fn extract( doc: &mut yaml::Yaml, tx: &rusqlite::Transaction, + table_name: &str, table_columns: &HashMap<String, crate::sqlite::Zero>, ) { match doc { yaml::Yaml::Array(ref mut array) => { for yaml_value in array { - extract(yaml_value, tx, table_columns); + extract(yaml_value, tx, table_name, table_columns); } } yaml::Yaml::Hash(ref mut hash) => { @@ -35,11 +36,13 @@ pub fn extract( let mut stmt = tx.prepare( &format!( r#" - INSERT INTO "people" + INSERT INTO "{}" ({}) VALUES ({}); "#, + table_name, + // Wrap column names in quotes. hash.keys() .map(|k| format!(r#""{}""#, k.as_str().unwrap())) |