Browse Source

Test SQLite storage

It is rather tricky to cleanup a test in Rust, luckily I found the
`Drop` pattern where a random struct is created at the beginning of the
test and `Drop` is implemented for this struct where all the cleanup
happens in this implementation. Works like a charm.
master
Andreas Linz 2 years ago
parent
commit
ff838cd276
No known key found for this signature in database GPG Key ID: 8DEF5099A0822620
3 changed files with 90 additions and 0 deletions
  1. +45
    -0
      Cargo.lock
  2. +3
    -0
      Cargo.toml
  3. +42
    -0
      src/main.rs

+ 45
- 0
Cargo.lock View File

@ -13,6 +13,14 @@ dependencies = [
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dirs"
version = "1.0.3"
@ -22,6 +30,20 @@ dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.43"
@ -67,6 +89,23 @@ name = "pkg-config"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_core"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_syscall"
version = "0.1.40"
@ -100,6 +139,7 @@ version = "0.1.0"
dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rusqlite 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -130,7 +170,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum dirs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f679c09c1cf5428702cc10f6846c56e4e23420d3a88bcc9335b17c630a7b710b"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum libsqlite3-sys 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d3711dfd91a1081d2458ad2d06ea30a8755256e74038be2ad927d94e1c955ca8"
"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939"
@ -138,6 +181,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
"checksum rusqlite 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c9d9118f1ce84d8d0b67f9779936432fb42bb620cef2122409d786892cce9a3c"
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"

+ 3
- 0
Cargo.toml View File

@ -7,3 +7,6 @@ authors = ["Andreas Linz "]
chrono = { version="0.4" }
rusqlite = { version="0.14", features=["chrono"] }
dirs = "1"
[dev-dependencies]
rand = "0.5"

+ 42
- 0
src/main.rs View File

@ -280,8 +280,12 @@ fn work_dur(recs: &[Record]) -> Option {
#[cfg(test)]
mod tests {
extern crate rand;
use self::rand::Rng;
use super::*;
use std::collections::HashMap;
use std::env;
fn new_record(timestamp: &str, state: State) -> Record {
Record {
@ -292,6 +296,44 @@ mod tests {
}
}
struct DbCleaner<P: AsRef<std::path::Path>> {
db_path: P,
}
impl<P: AsRef<std::path::Path>> Drop for DbCleaner<P> {
fn drop(&mut self) {
::std::fs::remove_file(&self.db_path).unwrap()
}
}
#[test]
fn test_storage() {
let mut db_path = env::temp_dir();
let db_file_name: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(16)
.collect();
db_path.push(format!("tt-test-{}.sqlite", db_file_name));
let _cleaner = DbCleaner { db_path: &db_path };
let conn = rusqlite::Connection::open(&db_path).unwrap();
prepare_database(&conn).unwrap();
let start = Record {
timestamp: DateTime::parse_from_rfc3339("2018-09-12T09:25:33+02:00")
.unwrap()
.with_timezone(&Local),
state: State::Work,
};
start.insert(&conn).unwrap();
let end = Record {
timestamp: start.timestamp + Duration::hours(8),
state: State::Work,
};
end.insert(&conn).unwrap();
let recs = recs_at_date(&conn, start.timestamp.date().naive_local()).unwrap();
assert_eq!(work_dur(&recs).unwrap(), Duration::hours(8));
}
#[test]
fn test_work_dur() {
struct TestCase {

Loading…
Cancel
Save