factor storage out to a trait object

This commit is contained in:
Dustin J. Mitchell 2020-01-05 14:58:24 -05:00
parent e228c99b83
commit 611b1cd68f
11 changed files with 177 additions and 108 deletions

View file

@ -1,8 +1,12 @@
use chrono::Utc;
use proptest::prelude::*;
use taskwarrior_rust::{Operation, DB};
use taskwarrior_rust::{taskstorage, Operation, DB};
use uuid::Uuid;
fn newdb() -> DB {
DB::new(Box::new(taskstorage::InMemoryStorage::new()))
}
fn uuid_strategy() -> impl Strategy<Value = Uuid> {
prop_oneof![
Just(Uuid::parse_str("83a2f9ef-f455-4195-b92e-a54c161eebfc").unwrap()),
@ -37,27 +41,30 @@ proptest! {
fn transform_invariant_holds(o1 in operation_strategy(), o2 in operation_strategy()) {
let (o1p, o2p) = Operation::transform(o1.clone(), o2.clone());
let mut db1 = DB::new();
let mut db1 = newdb();
let mut db2 = newdb();
// Ensure that any expected tasks already exist
if let Operation::Update{ ref uuid, .. } = o1 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db2.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Update{ ref uuid, .. } = o2 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db2.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Delete{ ref uuid } = o1 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db2.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Delete{ ref uuid } = o2 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db2.apply(Operation::Create{uuid: uuid.clone()});
}
let mut db2 = db1.clone();
// if applying the initial operations fail, that indicates the operation was invalid
// in the base state, so consider the case successful.
if let Err(_) = db1.apply(o1) {

View file

@ -1,15 +1,19 @@
use chrono::Utc;
use taskwarrior_rust::{Operation, Server, DB};
use taskwarrior_rust::{taskstorage, Operation, Server, DB};
use uuid::Uuid;
fn newdb() -> DB {
DB::new(Box::new(taskstorage::InMemoryStorage::new()))
}
#[test]
fn test_sync() {
let mut server = Server::new();
let mut db1 = DB::new();
let mut db1 = newdb();
db1.sync("me", &mut server);
let mut db2 = DB::new();
let mut db2 = newdb();
db2.sync("me", &mut server);
// make some changes in parallel to db1 and db2..
@ -66,10 +70,10 @@ fn test_sync() {
fn test_sync_create_delete() {
let mut server = Server::new();
let mut db1 = DB::new();
let mut db1 = newdb();
db1.sync("me", &mut server);
let mut db2 = DB::new();
let mut db2 = newdb();
db2.sync("me", &mut server);
// create and update a task..

View file

@ -1,8 +1,12 @@
use chrono::Utc;
use proptest::prelude::*;
use taskwarrior_rust::{Operation, Server, DB};
use taskwarrior_rust::{taskstorage, Operation, Server, DB};
use uuid::Uuid;
fn newdb() -> DB {
DB::new(Box::new(taskstorage::InMemoryStorage::new()))
}
#[derive(Debug)]
enum Action {
Op(Operation),
@ -43,7 +47,7 @@ proptest! {
// another. So, the generated sequences focus on a single task UUID.
fn transform_sequences_of_operations(action_sequence in action_sequence_strategy()) {
let mut server = Server::new();
let mut dbs = [DB::new(), DB::new(), DB::new()];
let mut dbs = [newdb(), newdb(), newdb()];
for (action, db) in action_sequence {
println!("{:?} on db {}", action, db);