summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-18 13:21:23 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-18 13:21:23 -0400
commit5c1e5261cefaa65596ff659b8b66d854db58a7e6 (patch)
tree55c8a39fe87747aad5d35b5710c8c0c9cfb9c1f9 /src/main.rs
parent83f7e53cf4b84bc26101e57b27b4dac0a32a19cc (diff)
Test table creation.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 3e66f9a..b2b65cd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,9 +22,25 @@ struct TagBase {
impl TagBase {
/// Instantiate a new `TagBase` whose backing database is at `path`.
- fn new(path: &str) -> Result<Self> {
- let conn = Connection::open(path)?;
- conn.execute(
+ pub fn new(path: &str) -> Result<Self> {
+ let tb = TagBase {
+ conn: Connection::open(path)?,
+ };
+ tb.initialize_tables()?;
+ Ok(tb)
+ }
+
+ fn make_temporary() -> Result<Self> {
+ let tb = TagBase {
+ conn: Connection::open_in_memory()?,
+ };
+ tb.initialize_tables()?;
+ Ok(tb)
+ }
+
+ /// Create the table structure for the tag database.
+ fn initialize_tables(&self) -> Result<()> {
+ self.conn.execute(
"CREATE TABLE IF NOT EXISTS images (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
blake2 TEXT NOT NULL,
@@ -33,21 +49,21 @@ impl TagBase {
)",
params![],
)?;
- conn.execute(
+ self.conn.execute(
"CREATE TABLE IF NOT EXISTS tags (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)",
params![],
)?;
- conn.execute(
+ self.conn.execute(
"CREATE TABLE IF NOT EXISTS mapping (
image INTEGER NOT NULL,
tag INTEGER NOT NULL
)",
params![],
)?;
- Ok(TagBase { conn })
+ Ok(())
}
/// Insert the image at `path` into the data.
@@ -118,6 +134,27 @@ impl TagBase {
}
}
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn tables_exist() {
+ let tb = TagBase::make_temporary().unwrap();
+ for table in ["images", "tags", "mapping"].iter() {
+ let exists = tb
+ .conn
+ .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?;")
+ .unwrap()
+ .exists(params![table])
+ .unwrap();
+ if !exists {
+ panic!("Table {} did not exist", table);
+ }
+ }
+ }
+}
+
fn main() {
let tb = TagBase::new("/tmp/test.db").unwrap();
tb.import_image(Path::new("/home/jakob/Sync/06fc9ae71549eb4d.jpeg"))