Skip to content

Commit

Permalink
Add more property tests.
Browse files Browse the repository at this point in the history
Use the test_strategy::proptest macro for nice async support.

Upstream proptest+tokio issue:
proptest-rs/proptest#179

While there is a workaround there, the `prop_assert!` macro didn't
seem to work properly. The `assert!` macro does work, but then we see
all the output when proptest tries to shrink a failing example.
  • Loading branch information
dcherian committed Aug 20, 2024
1 parent 131605a commit b7e80f4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ debug = true
[dev-dependencies]
pretty_assertions = "1.4.0"
proptest = "1.0.0"
test-strategy = "0.4.0"
tokio = { version = "1.39.2", features = ["rt-multi-thread", "macros"] }
43 changes: 43 additions & 0 deletions src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,27 @@ pub enum FlushError {
StorageError(#[from] StorageError),
}

#[cfg(test)]
mod strategies {
use std::path::PathBuf;
use std::sync::Arc;

use proptest::prelude::*;
use proptest::strategy::Strategy;

use crate::storage::InMemoryStorage;
use crate::{Dataset, Path};

pub(crate) fn node_path_strategy() -> impl Strategy<Value = Path> {
any::<PathBuf>()
}

pub(crate) fn dataset_strategy() -> impl Strategy<Value = Dataset> {
let storage = InMemoryStorage::new();
let dataset = Dataset::new(Arc::new(storage), None);
prop_oneof![Just(dataset)]
}
}
#[cfg(test)]
mod tests {
use std::{error::Error, num::NonZeroU64, path::PathBuf};
Expand All @@ -734,6 +755,28 @@ mod tests {
use super::*;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use proptest::prelude::{prop_assert, prop_assert_eq};
use strategies::*;
use test_strategy::proptest;

#[proptest(async = "tokio")]
async fn test_add_delete_group(
#[strategy(node_path_strategy())] path: Path,
#[strategy(dataset_strategy())] mut dataset: Dataset,
) {
prop_assert!(dataset.get_node(&path).await.is_err());

dataset.add_group(path.clone()).await.unwrap();
let node = dataset.get_node(&path).await;
prop_assert!(node.is_ok());
prop_assert_eq!(node.unwrap(), dataset.get_node(&path).await.unwrap());

// adding an existing group fails
prop_assert!(dataset.add_group(path.clone()).await.is_err());

dataset.delete_group(path.clone()).await.unwrap();
prop_assert!(dataset.get_node(&path).await.is_err());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_dataset_with_updates() -> Result<(), Box<dyn Error>> {
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,14 @@ pub trait Storage {
async fn write_chunk(&self, id: ObjectId, bytes: Bytes) -> Result<(), StorageError>;
}

#[derive(Clone)]
impl fmt::Debug for dyn Storage + Send + Sync {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// FIXME
write!(f, "Storage")
}
}

#[derive(Clone, Debug)]
pub struct Dataset {
storage: Arc<dyn Storage + Send + Sync>,
structure_id: Option<ObjectId>,
Expand Down

0 comments on commit b7e80f4

Please sign in to comment.