Skip to content

Commit 59be73d

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

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

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

Lines changed: 76 additions & 5 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,12 +192,11 @@ 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(), 1);
196196
match search_result {
197-
// If there are > 1 entry (which there shouldn't be), still return true
198-
Ok(results) => results.len() != 0,
197+
Ok(results) => !results.is_empty(),
199198
Err(err) => {
200-
warn!("Failed to check if event has been indexed, assuming it has: {err:?}");
199+
warn!("Failed to check if event has been indexed, assuming it has: {err}");
201200
true
202201
}
203202
}
@@ -301,4 +300,76 @@ mod tests {
301300

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

0 commit comments

Comments
 (0)