Skip to content

Commit 010e2a7

Browse files
author
Shrey Patel
committed
test(search): Add tests for RoomIndex::contains and indexing idempotency
1 parent de1ad73 commit 010e2a7

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

crates/matrix-sdk-search/src/index.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl RoomIndex {
137137
self.writer.add_document(document)?;
138138
}
139139
}
140-
};
140+
}
141141
Ok(())
142142
}
143143

@@ -192,10 +192,10 @@ impl RoomIndex {
192192
}
193193

194194
fn contains(&self, event_id: &EventId) -> bool {
195-
let search_result = self.search(format!("event_id:\"{}\"", event_id).as_str(), 5);
195+
let search_result = self.search(format!("event_id:\"{event_id}\"").as_str(), 5);
196196
match search_result {
197197
// If there are > 1 entry (which there shouldn't be), still return true
198-
Ok(results) => results.len() != 0,
198+
Ok(results) => !results.is_empty(),
199199
Err(err) => {
200200
warn!("Failed to check if event has been indexed, assuming it has: {err:?}");
201201
true
@@ -301,4 +301,76 @@ mod tests {
301301

302302
Ok(())
303303
}
304+
305+
#[test]
306+
fn test_index_contains_false() -> Result<(), Box<dyn Error>> {
307+
let room_id = room_id!("!room_id:localhost");
308+
let mut index =
309+
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");
310+
311+
let event_id = event_id!("$event_id:localhost");
312+
313+
index.commit_and_reload()?;
314+
315+
assert!(!index.contains(event_id), "Index should not contain event");
316+
317+
Ok(())
318+
}
319+
320+
#[test]
321+
fn test_index_contains_true() -> Result<(), Box<dyn Error>> {
322+
let room_id = room_id!("!room_id:localhost");
323+
let mut index =
324+
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");
325+
326+
let event_id = event_id!("$event_id:localhost");
327+
let event = EventFactory::new()
328+
.text_msg("This is a sentence")
329+
.event_id(event_id)
330+
.room(room_id)
331+
.sender(user_id!("@user_id:localhost"))
332+
.into_any_sync_message_like_event();
333+
334+
index.handle_event(event)?;
335+
336+
index.commit_and_reload()?;
337+
338+
assert!(index.contains(event_id), "Index should contain event");
339+
340+
Ok(())
341+
}
342+
343+
#[test]
344+
fn test_indexing_idempotency() -> Result<(), Box<dyn Error>> {
345+
let room_id = room_id!("!room_id:localhost");
346+
let mut index =
347+
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");
348+
349+
let event_id = event_id!("$event_id:localhost");
350+
let event = EventFactory::new()
351+
.text_msg("This is a sentence")
352+
.event_id(event_id)
353+
.room(room_id)
354+
.sender(user_id!("@user_id:localhost"))
355+
.into_any_sync_message_like_event();
356+
357+
index.handle_event(event.clone())?;
358+
359+
index.commit_and_reload()?;
360+
361+
assert!(index.contains(event_id), "Index should contain event");
362+
363+
// indexing again should do nothing
364+
index.handle_event(event)?;
365+
366+
index.commit_and_reload()?;
367+
368+
assert!(index.contains(event_id), "Index should still contain event");
369+
370+
let result = index.search("sentence", 10).expect("search failed with: {result:?}");
371+
372+
assert_eq!(result.len(), 1, "Index should have ignored second indexing");
373+
374+
Ok(())
375+
}
304376
}

0 commit comments

Comments
 (0)