From 4b36fa073990a90b6599405a8917cfa0f60cfd00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 16 May 2019 12:00:20 +0200 Subject: [PATCH] test: Add tests about additions and deletions of documents --- meilidb-data/Cargo.toml | 3 ++ meilidb-data/tests/updates.rs | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 meilidb-data/tests/updates.rs diff --git a/meilidb-data/Cargo.toml b/meilidb-data/Cargo.toml index eb0d92f55..adf618657 100644 --- a/meilidb-data/Cargo.toml +++ b/meilidb-data/Cargo.toml @@ -27,3 +27,6 @@ rev = "40b3d48" [dependencies.fst] git = "https://github.com/Kerollmops/fst.git" branch = "arc-byte-slice" + +[dev-dependencies] +tempfile = "3.0.7" diff --git a/meilidb-data/tests/updates.rs b/meilidb-data/tests/updates.rs new file mode 100644 index 000000000..393b2ecba --- /dev/null +++ b/meilidb-data/tests/updates.rs @@ -0,0 +1,67 @@ +use serde_json::json; +use meilidb_data::{Database, Schema}; +use meilidb_data::schema::{SchemaBuilder, STORED, INDEXED}; + +fn simple_schema() -> Schema { + let mut builder = SchemaBuilder::with_identifier("objectId"); + builder.new_attribute("objectId", STORED | INDEXED); + builder.new_attribute("title", STORED | INDEXED); + builder.build() +} + +#[test] +fn insert_delete_document() { + let tmp_dir = tempfile::tempdir().unwrap(); + let database = Database::start_default(&tmp_dir).unwrap(); + + let schema = simple_schema(); + let index = database.create_index("hello", schema).unwrap(); + + let doc1 = json!({ "objectId": 123, "title": "hello" }); + + let mut addition = index.documents_addition(); + addition.update_document(&doc1).unwrap(); + addition.finalize().unwrap(); + + let docs = index.query_builder().query("hello", 0..10).unwrap(); + assert_eq!(docs.len(), 1); + assert_eq!(index.document(None, docs[0].id).unwrap().as_ref(), Some(&doc1)); + + let mut deletion = index.documents_deletion(); + deletion.delete_document(&doc1).unwrap(); + deletion.finalize().unwrap(); + + let docs = index.query_builder().query("hello", 0..10).unwrap(); + assert_eq!(docs.len(), 0); +} + +#[test] +fn replace_document() { + let tmp_dir = tempfile::tempdir().unwrap(); + let database = Database::start_default(&tmp_dir).unwrap(); + + let schema = simple_schema(); + let index = database.create_index("hello", schema).unwrap(); + + let doc1 = json!({ "objectId": 123, "title": "hello" }); + let doc2 = json!({ "objectId": 123, "title": "coucou" }); + + let mut addition = index.documents_addition(); + addition.update_document(&doc1).unwrap(); + addition.finalize().unwrap(); + + let docs = index.query_builder().query("hello", 0..10).unwrap(); + assert_eq!(docs.len(), 1); + assert_eq!(index.document(None, docs[0].id).unwrap().as_ref(), Some(&doc1)); + + let mut deletion = index.documents_addition(); + deletion.update_document(&doc2).unwrap(); + deletion.finalize().unwrap(); + + let docs = index.query_builder().query("hello", 0..10).unwrap(); + assert_eq!(docs.len(), 0); + + let docs = index.query_builder().query("coucou", 0..10).unwrap(); + assert_eq!(docs.len(), 1); + assert_eq!(index.document(None, docs[0].id).unwrap().as_ref(), Some(&doc2)); +}