fix tests

Co-authored-by: J H <Blu-J@users.noreply.github.com>
This commit is contained in:
Aiden McClelland
2023-02-24 15:41:43 -07:00
parent 2fb274ada3
commit bf927fcabd
5 changed files with 15 additions and 41 deletions

View File

@@ -239,23 +239,23 @@ fn impl_fns(children: &[ChildInfo]) -> Fns {
if optional {
impl_fns.extend(quote_spanned! { name.span() =>
#vis fn #name (&self) -> patch_db::OptionModel<#model_ty> {
<patch_db::OptionModel::<#model_ty> as patch_db::Model>::new((&**self) #accessor .clone())
<patch_db::OptionModel::<#model_ty> as patch_db::Model>::new((**self) #accessor .clone())
}
});
impl_mut_fns.extend(quote_spanned! { name.span() =>
#vis fn #name (&self) -> patch_db::OptionModelMut<'a, #model_mut_ty> {
<patch_db::OptionModelMut::<'a, #model_mut_ty> as patch_db::ModelMut<'a>>::new(&mut (&mut **self) #accessor)
#vis fn #name (&'a mut self) -> patch_db::OptionModelMut<'a, #model_mut_ty> {
<patch_db::OptionModelMut::<'a, #model_mut_ty> as patch_db::ModelMut<'a>>::new(&mut (**self) #accessor)
}
});
} else {
impl_fns.extend(quote_spanned! { name.span() =>
#vis fn #name (&self) -> #model_ty {
<#model_ty as patch_db::Model>::new((&**self) #accessor .clone())
<#model_ty as patch_db::Model>::new((**self) #accessor .clone())
}
});
impl_mut_fns.extend(quote_spanned! { name.span() =>
#vis fn #name (&mut self) -> #model_mut_ty {
<#model_mut_ty as patch_db::ModelMut<'a>>::new(&mut (&mut **self) #accessor)
#vis fn #name (&'a mut self) -> #model_mut_ty {
<#model_mut_ty as patch_db::ModelMut<'a>>::new(&mut (**self) #accessor)
}
});
}

View File

@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use fd_lock_rs::FdLock;
use imbl_value::Value;
use imbl_value::{InternedString, Value};
use json_patch::PatchError;
use json_ptr::{JsonPointer, SegList};
use lazy_static::lazy_static;
@@ -159,7 +159,7 @@ impl Store {
pub(crate) fn keys<S: AsRef<str>, V: SegList>(
&self,
ptr: &JsonPointer<S, V>,
) -> BTreeSet<Arc<String>> {
) -> BTreeSet<InternedString> {
match ptr.get(&self.persistent).unwrap_or(&Value::Null) {
Value::Object(o) => o.keys().cloned().collect(),
_ => BTreeSet::new(),
@@ -326,7 +326,7 @@ impl PatchDb {
pub async fn keys<S: AsRef<str>, V: SegList>(
&self,
ptr: &JsonPointer<S, V>,
) -> BTreeSet<Arc<String>> {
) -> BTreeSet<InternedString> {
self.store.read().await.keys(ptr)
}
pub async fn get_value<S: AsRef<str>, V: SegList>(&self, ptr: &JsonPointer<S, V>) -> Value {

View File

@@ -1,14 +1,14 @@
use std::future::Future;
use std::sync::Arc;
use imbl_value::Value;
use json_ptr::JsonPointer;
use patch_db::{HasModel, PatchDb, Revision};
use proptest::prelude::*;
use serde_json::Value;
use tokio::fs;
use tokio::runtime::Builder;
use crate::{self as patch_db, DbHandle, LockType};
use crate::{self as patch_db};
async fn init_db(db_name: String) -> PatchDb {
cleanup_db(&db_name).await;
@@ -45,23 +45,10 @@ async fn basic() {
let db = init_db("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);
assert_eq!(get_res.as_u64(), Some(1));
db.put(&ptr, "hello").await.unwrap();
get_res = db.get(&ptr).await.unwrap();
assert_eq!(get_res, "hello");
cleanup_db("test.db").await;
}
#[tokio::test]
async fn transaction() {
let db = init_db("test.db".to_string()).await;
let mut handle = db.handle();
let mut tx = handle.begin().await.unwrap();
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);
assert_eq!(get_res.as_str(), Some("hello"));
cleanup_db("test.db").await;
}
@@ -100,16 +87,3 @@ pub struct Child {
#[derive(Debug, serde::Deserialize, serde::Serialize, HasModel)]
pub struct NewType(Option<Box<Sample>>);
#[tokio::test]
async fn locks_dropped_from_enforcer_on_tx_save() {
let db = init_db("test.db".to_string()).await;
let mut handle = db.handle();
let mut tx = handle.begin().await.unwrap();
let ptr_a: JsonPointer = "/a".parse().unwrap();
let ptr_b: JsonPointer = "/b".parse().unwrap();
tx.lock(ptr_b.into(), LockType::Write).await.unwrap();
tx.save().await.unwrap();
handle.lock(ptr_a.into(), LockType::Write).await.unwrap();
cleanup_db("test.db").await;
}