pull in changes from master, .gitignore test dbs

This commit is contained in:
Chris Guida
2021-03-26 10:02:38 -06:00
6 changed files with 92 additions and 46 deletions

View File

@@ -19,6 +19,7 @@ serde_json = "1.0.61"
serde_cbor = { path = "../cbor" }
thiserror = "1.0.23"
tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "io-util", "macros"] }
patch-db-macro = { path = "../patch-db-macro" }
[dev-dependencies]
proptest = "1.0.0"

View File

@@ -1002,13 +1002,14 @@ impl<T: Serialize + for<'de> Deserialize<'de>> DerefMut for ModelDataMut<T> {
}
}
#[derive(Debug)]
pub struct Model<T: Serialize + for<'de> Deserialize<'de>> {
ptr: JsonPointer,
phantom: PhantomData<T>,
}
impl<T> Model<T>
where
T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
T: Serialize + for<'de> Deserialize<'de>,
{
pub fn new(ptr: JsonPointer) -> Self {
Self {
@@ -1045,3 +1046,18 @@ where
}
}
}
impl<T> std::clone::Clone for Model<T>
where
T: Serialize + for<'de> Deserialize<'de>,
{
fn clone(&self) -> Self {
Model {
ptr: self.ptr.clone(),
phantom: PhantomData,
}
}
}
pub trait HasModel {
type Model;
}

View File

@@ -1,6 +1,8 @@
use super::*;
use crate as patch_db;
use proptest::prelude::*;
use tokio::{fs, runtime::Builder};
use patch_db_macro::HasModel;
async fn init_db(db_name: String) -> PatchDb {
cleanup_db(&db_name).await;
@@ -35,26 +37,26 @@ async fn put_string_into_root(db: PatchDb, s: String) -> Arc<Revision> {
#[tokio::test]
async fn basic() {
let db = init_db("basic".to_string()).await;
let db = init_db("basic.test.db".to_string()).await;
let ptr: JsonPointer = "/b/b".parse().unwrap();
let mut get_res: Value = db.get(&ptr).await.unwrap();
assert_eq!(get_res, 1);
db.put(&ptr, &"hello").await.unwrap();
get_res = db.get(&ptr).await.unwrap();
assert_eq!(get_res, "hello");
cleanup_db(&"basic".to_string()).await;
cleanup_db(&"basic.test.db".to_string()).await;
}
#[tokio::test]
async fn transaction() {
let db = init_db("transaction".to_string()).await;
let db = init_db("transaction.test.db".to_string()).await;
let mut tx = db.begin();
let ptr: JsonPointer = "/b/b".parse().unwrap();
tx.put(&ptr, &(2 as usize)).await.unwrap();
tx.put(&ptr, &(1 as usize)).await.unwrap();
let _res = tx.commit().await.unwrap();
println!("res = {:?}", _res);
cleanup_db(&"transaction".to_string()).await;
cleanup_db(&"transaction.test.db".to_string()).await;
}
proptest! {
@@ -67,32 +69,23 @@ proptest! {
.thread_stack_size(3 * 1024 * 1024)
.build()
.unwrap();
let db = runtime.block_on(init_db("doesnt_crash".to_string()));
let db = runtime.block_on(init_db("doesnt_crash.test.db".to_string()));
let put_future = put_string_into_root(db, s);
runtime.block_on(put_future);
runtime.block_on(cleanup_db(&"doesnt_crash".to_string()));
runtime.block_on(cleanup_db(&"doesnt_crash.test.db".to_string()));
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Deserialize, serde::Serialize, HasModel)]
pub struct Sample {
a: String,
#[model(name = ChildModel)]
b: Child,
}
pub struct SampleModel(Model<Sample>);
impl core::ops::Deref for SampleModel {
type Target = Model<Sample>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Child {
a: String,
b: usize,
}
pub struct ChildModel(Model<Child>);