add a proptest

This commit is contained in:
Chris Guida
2021-03-26 09:12:39 -06:00
parent 16fd2b55e8
commit 2ea757027e

View File

@@ -1,9 +1,10 @@
use super::*; use super::*;
use proptest::prelude::*; use proptest::prelude::*;
use tokio::runtime::Builder; use tokio::{fs, runtime::Builder};
async fn init_test_env() -> PatchDb { async fn init_db(db_name: String) -> PatchDb {
let db = PatchDb::open("test.db").await.unwrap(); cleanup_db(&db_name).await;
let db = PatchDb::open(db_name).await.unwrap();
db.put( db.put(
&JsonPointer::<&'static str>::default(), &JsonPointer::<&'static str>::default(),
&Sample { &Sample {
@@ -18,6 +19,10 @@ async fn init_test_env() -> PatchDb {
db db
} }
async fn cleanup_db(db_name: &String) {
fs::remove_file(db_name).await.ok();
}
async fn put_string_into_root(db: PatchDb, s: String) -> Arc<Revision> { async fn put_string_into_root(db: PatchDb, s: String) -> Arc<Revision> {
println!("trying string: {}", s); println!("trying string: {}", s);
db.put( db.put(
@@ -30,25 +35,26 @@ async fn put_string_into_root(db: PatchDb, s: String) -> Arc<Revision> {
#[tokio::test] #[tokio::test]
async fn basic() { async fn basic() {
let db = init_test_env().await; let db = init_db("basic".to_string()).await;
let ptr: JsonPointer = "/b/b".parse().unwrap(); let ptr: JsonPointer = "/b/b".parse().unwrap();
let mut get_res: Value = db.get(&ptr).await.unwrap(); let mut get_res: Value = db.get(&ptr).await.unwrap();
assert_eq!(get_res, 1); assert_eq!(get_res, 1);
db.put(&ptr, &"hello").await.unwrap(); db.put(&ptr, &"hello").await.unwrap();
get_res = db.get(&ptr).await.unwrap(); get_res = db.get(&ptr).await.unwrap();
assert_eq!(get_res, "hello"); assert_eq!(get_res, "hello");
cleanup_db(&"basic".to_string()).await;
} }
#[tokio::test] #[tokio::test]
async fn transaction() { async fn transaction() {
let db = init_test_env().await; let db = init_db("transaction".to_string()).await;
let mut tx = db.begin(); let mut tx = db.begin();
let ptr: JsonPointer = "/b/b".parse().unwrap(); let ptr: JsonPointer = "/b/b".parse().unwrap();
tx.put(&ptr, &(2 as usize)).await.unwrap(); tx.put(&ptr, &(2 as usize)).await.unwrap();
tx.put(&ptr, &(1 as usize)).await.unwrap(); tx.put(&ptr, &(1 as usize)).await.unwrap();
let _res = tx.commit().await.unwrap(); let _res = tx.commit().await.unwrap();
println!("res = {:?}", _res); println!("res = {:?}", _res);
cleanup_db(&"transaction".to_string()).await;
} }
proptest! { proptest! {
@@ -61,9 +67,11 @@ proptest! {
.thread_stack_size(3 * 1024 * 1024) .thread_stack_size(3 * 1024 * 1024)
.build() .build()
.unwrap(); .unwrap();
let db = runtime.block_on(init_test_env()); let db = runtime.block_on(init_db("doesnt_crash".to_string()));
let put_future = put_string_into_root(db, s); let put_future = put_string_into_root(db, s);
runtime.block_on(put_future); runtime.block_on(put_future);
runtime.block_on(cleanup_db(&"doesnt_crash".to_string()));
} }
} }