Skip to content

Commit

Permalink
lmdb: fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 1, 2024
1 parent 76931bc commit 8f49dc7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
### Fixed

* lmdb: add missing commit method call in `Store::delete` ([Yuki Kishimoto])
* lmdb: fix unit tests ([Yuki Kishimoto])

### Removed

Expand Down
1 change: 1 addition & 0 deletions contrib/scripts/check-crates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ buildargs=(
"-p nostr --no-default-features --features alloc"
"-p nostr --no-default-features --features alloc,all-nips"
"-p nostr-database"
"-p nostr-lmdb"
"-p nostr-relay-builder"
"-p nostr-sdk"
"-p nostr-sdk --no-default-features"
Expand Down
24 changes: 13 additions & 11 deletions crates/nostr-lmdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ mod tests {
(keys, event)
}

async fn add_event_with_keys(&self, builder: EventBuilder, keys: &Keys) -> Event {
async fn add_event_with_keys(&self, builder: EventBuilder, keys: &Keys) -> (Event, bool) {
let event = builder.to_event(&keys).unwrap();
self.db.save_event(&event).await.unwrap();
event
let stored = self.db.save_event(&event).await.unwrap();
(event, stored)
}

async fn count_all(&self) -> usize {
Expand Down Expand Up @@ -332,15 +332,16 @@ mod tests {
assert_eq!(db.count_all().await, added_events + 1);

// Replace previous event
let new_expected_event = db
let (new_expected_event, stored) = db
.add_event_with_keys(
EventBuilder::metadata(&metadata).custom_created_at(now),
&keys,
)
.await;
assert!(stored);

// Test event by ID (MUST return error)
assert!(db.event_by_id(&expected_event.id).await.is_err());
// Test event by ID (MUST be None because replaced)
assert!(db.event_by_id(&expected_event.id).await.unwrap().is_none());

// Test event by ID
let event = db
Expand Down Expand Up @@ -395,7 +396,7 @@ mod tests {
assert_eq!(db.count_all().await, added_events + 1);

// Replace previous event
let new_expected_event = db
let (new_expected_event, stored) = db
.add_event_with_keys(
EventBuilder::new(
Kind::ParameterizedReplaceable(33_333),
Expand All @@ -406,9 +407,10 @@ mod tests {
&keys,
)
.await;
assert!(stored);

// Test event by ID (MUST return error)
assert!(db.event_by_id(&expected_event.id).await.is_err());
// Test event by ID (MUST be None` because replaced)
assert!(db.event_by_id(&expected_event.id).await.unwrap().is_none());

// Test event by ID
let event = db
Expand Down Expand Up @@ -462,7 +464,7 @@ mod tests {
// Event 10 deleted by event 12
// Event 9 replaced by event 10
Event::from_json(EVENTS[8]).unwrap(),
Event::from_json(EVENTS[7]).unwrap(),
// Event 7 is an invalid deletion
Event::from_json(EVENTS[6]).unwrap(),
Event::from_json(EVENTS[5]).unwrap(),
Event::from_json(EVENTS[4]).unwrap(),
Expand All @@ -475,6 +477,6 @@ mod tests {
db.query(vec![Filter::new()]).await.unwrap(),
expected_output
);
assert_eq!(db.count_all().await, 9);
assert_eq!(db.count_all().await, 8);
}
}
3 changes: 1 addition & 2 deletions crates/nostr-lmdb/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ impl Store {
// Handle deletion events
if let Kind::EventDeletion = event.kind {
let invalid: bool = Self::handle_deletion_event(&db, &mut txn, &event)?;

if invalid {
txn.abort();
return Ok(false);
}
}
Expand Down

0 comments on commit 8f49dc7

Please sign in to comment.