Skip to content

Commit

Permalink
Update ObjectId generation to be deterministic using generates bytes …
Browse files Browse the repository at this point in the history
…and bson::oid::ObjectId::from_bytes. Add test for determinism for bson_oid feature
  • Loading branch information
Jordan Van Allen committed Apr 16, 2024
1 parent dfc89f5 commit 31d17d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fake/src/impls/bson_oid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use bson::oid::ObjectId;
use crate::{Dummy, Faker};

impl Dummy<Faker> for ObjectId {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, _rng: &mut R) -> Self {
ObjectId::new()
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
let mut bytes = [0u8; 12];
rng.fill(&mut bytes);
ObjectId::from_bytes(bytes)
}
}
10 changes: 10 additions & 0 deletions fake/tests/determinism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,13 @@ mod serde_json {
use rand::SeedableRng as _;
check_determinism! { one fake_serde_json, serde_json::Value, Faker }
}

// BSON ObjectId
#[cfg(feature = "bson_oid")]
mod bson_oid {
use bson::oid::ObjectId;
use fake::{Fake, Faker};
use rand::SeedableRng as _;

check_determinism! { one fake_bson_oid, ObjectId, Faker }
}

0 comments on commit 31d17d3

Please sign in to comment.