Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Sep 27, 2024
1 parent ed6a0a0 commit 2452190
Showing 1 changed file with 123 additions and 4 deletions.
127 changes: 123 additions & 4 deletions grovedb/src/operations/insert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ impl GroveDb {
/// Insert if not exists
/// Insert if not exists
///
/// Inserts an element at the specified path and key if it does not already exist.
/// Inserts an element at the specified path and key if it does not already
/// exist.
///
/// # Arguments
///
Expand All @@ -500,7 +501,8 @@ impl GroveDb {
///
/// # Returns
///
/// Returns a `CostResult<bool, Error>` indicating whether the element was inserted (`true`) or already existed (`false`).
/// Returns a `CostResult<bool, Error>` indicating whether the element was
/// inserted (`true`) or already existed (`false`).
pub fn insert_if_not_exists<'b, B, P>(
&self,
path: P,
Expand Down Expand Up @@ -553,7 +555,9 @@ impl GroveDb {
///
/// # Returns
///
/// Returns a `CostResult<Option<Element>, Error>`, where `Ok(Some(element))` is the existing element if it was found, or `Ok(None)` if the new element was inserted.
/// Returns a `CostResult<Option<Element>, Error>`, where
/// `Ok(Some(element))` is the existing element if it was found, or
/// `Ok(None)` if the new element was inserted.
pub fn insert_if_not_exists_return_existing_element<'b, B, P>(
&self,
path: P,
Expand All @@ -574,7 +578,6 @@ impl GroveDb {
.insert
.insert_if_not_exists_return_existing_element
);
);

let mut cost = OperationCost::default();
let subtree_path: SubtreePath<_> = path.into();
Expand Down Expand Up @@ -858,6 +861,122 @@ mod tests {
assert!(matches!(result, Err(Error::InvalidParentLayerPath(_))));
}

#[test]
fn test_insert_if_not_exists_return_existing_element() {
let grove_version = GroveVersion::latest();
let db = make_test_grovedb(grove_version);

let element_key = b"key1";
let new_element = Element::new_item(b"new_value".to_vec());

// Insert a new element and check if it returns None
let result = db
.insert_if_not_exists_return_existing_element(
[TEST_LEAF].as_ref(),
element_key,
new_element.clone(),
None,
grove_version,
)
.unwrap()
.expect("Expected insertion of new element");

assert_eq!(result, None);

// Try inserting the same element again and expect it to return the existing
// element
let result = db
.insert_if_not_exists_return_existing_element(
[TEST_LEAF].as_ref(),
element_key,
Element::new_item(b"another_value".to_vec()),
None,
grove_version,
)
.unwrap()
.expect("Expected to return existing element");

assert_eq!(result, Some(new_element.clone()));

// Check if the existing element is still the original one and not replaced
let fetched_element = db
.get([TEST_LEAF].as_ref(), element_key, None, grove_version)
.unwrap()
.expect("Expected to retrieve the existing element");

assert_eq!(fetched_element, new_element);
}

#[test]
fn test_insert_if_not_exists_return_existing_element_with_transaction() {
let grove_version = GroveVersion::latest();
let db = make_test_grovedb(grove_version);

let element_key = b"key2";
let new_element = Element::new_item(b"transaction_value".to_vec());
let transaction = db.start_transaction();

// Insert a new element within a transaction and check if it returns None
let result = db
.insert_if_not_exists_return_existing_element(
[TEST_LEAF].as_ref(),
element_key,
new_element.clone(),
Some(&transaction),
grove_version,
)
.unwrap()
.expect("Expected insertion of new element in transaction");

assert_eq!(result, None);

// Try inserting the same element again within the transaction
// and expect it to return the existing element
let result = db
.insert_if_not_exists_return_existing_element(
[TEST_LEAF].as_ref(),
element_key,
Element::new_item(b"another_transaction_value".to_vec()),
Some(&transaction),
grove_version,
)
.unwrap()
.expect("Expected to return existing element in transaction");

assert_eq!(result, Some(new_element.clone()));

// Commit the transaction
db.commit_transaction(transaction).unwrap().unwrap();

// Check if the element is still the original one and not replaced
let fetched_element = db
.get([TEST_LEAF].as_ref(), element_key, None, grove_version)
.unwrap()
.expect("Expected to retrieve the existing element after transaction commit");

assert_eq!(fetched_element, new_element);
}

#[test]
fn test_insert_if_not_exists_return_existing_element_invalid_path() {
let grove_version = GroveVersion::latest();
let db = make_test_grovedb(grove_version);

// Try inserting to an invalid path and expect an error
let result = db.insert_if_not_exists_return_existing_element(
[b"invalid_path"].as_ref(),
b"key",
Element::new_item(b"value".to_vec()),
None,
grove_version,
);

assert!(matches!(
result.unwrap(),
Err(Error::InvalidParentLayerPath(_))
));
}

#[test]
fn test_one_insert_item_cost() {
let grove_version = GroveVersion::latest();
Expand Down

0 comments on commit 2452190

Please sign in to comment.