Skip to content

Commit e9d0689

Browse files
committed
Update the skd to meilisearch v0.25.2
1 parent 9187baa commit e9d0689

File tree

17 files changed

+1566
-1165
lines changed

17 files changed

+1566
-1165
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
- name: Build
2525
run: cargo build --verbose
2626
- name: Meilisearch (latest version) setup with Docker
27-
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics=true --master-key=masterKey
27+
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics --master-key=masterKey
2828
- name: Run tests
29-
run: cargo test --verbose -- --test-threads=1
29+
run: cargo test --verbose
3030

3131
linter:
3232
name: clippy-check

CONTRIBUTING.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ First of all, thank you for contributing to Meilisearch! The goal of this docume
2929

3030
### Tests <!-- omit in toc -->
3131

32-
All the tests are documentation tests.<br>
33-
Since they are all making operations on the Meilisearch server, running all the tests simultaneously would cause panics.
34-
35-
To run the tests one by one, run:
32+
To run the tests, run:
3633

3734
```bash
3835
# Tests
3936
curl -L https://install.meilisearch.com | sh # download Meilisearch
40-
./meilisearch --master-key=masterKey --no-analytics=true # run Meilisearch
41-
cargo test -- --test-threads=1
37+
./meilisearch --master-key=masterKey --no-analytics # run Meilisearch
38+
cargo test
4239
```
4340

41+
There is two kind of tests, documentation tests and unit tests.
42+
If you need to write or read the unit tests you should consider reading this
43+
[readme](meilisearch-test-macro/README.md) about our custom testing macro.
44+
4445
Also, the WASM example compilation should be checked:
4546

4647
```bash
@@ -65,7 +66,7 @@ rustup update
6566
rustup component add clippy
6667
```
6768

68-
⚠️ Also, if you have installed `clippy` a long time ago, you might need to update it:
69+
⚠️ Also, if you have installed `clippy` a long time ago, you might need to update it:
6970

7071
```bash
7172
rustup update

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "meilisearch-sdk"
3-
version = "0.13.0"
3+
version = "0.14.0"
44
authors = ["Mubelotix <[email protected]>"]
55
edition = "2018"
66
description = "Rust wrapper for the Meilisearch API. Meilisearch is a powerful, fast, open-source, easy to use and deploy search engine."
@@ -9,6 +9,7 @@ readme = "README.md"
99
repository = "https://github.com/meilisearch/meilisearch-sdk"
1010

1111
[dependencies]
12+
async-trait = "0.1.51"
1213
serde_json = "1.0"
1314
log = "0.4"
1415
serde = { version = "1.0", features = ["derive"] }
@@ -32,6 +33,7 @@ sync = []
3233
env_logger = "0.9"
3334
futures-await-test = "0.3"
3435
futures = "0.3"
36+
meilisearch-test-macro = { path = "meilisearch-test-macro" }
3537

3638
# The following dependencies are required for examples
3739
wasm-bindgen = "0.2"

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,27 @@ fn main() { block_on(async move {
117117

118118
// Add some movies in the index. If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
119119
movies.add_documents(&[
120-
Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()]},
121-
Movie{id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()]},
122-
Movie{id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()]},
123-
Movie{id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()]},
124-
Movie{id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()]},
125-
Movie{id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()]},
120+
Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()] },
121+
Movie { id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()] },
122+
Movie { id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()] },
123+
Movie { id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()] },
124+
Movie { id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()] },
125+
Movie { id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()] },
126126
], Some("id")).await.unwrap();
127127
})}
128128
```
129129

130130
#### Basic Search <!-- omit in TOC -->
131131

132132
```rust
133+
#
133134
// Meilisearch is typo-tolerant:
134-
println!("{:?}", client.index("movies").search().with_query("caorl").execute::<Movie>().await.unwrap().hits);
135+
println!("{:?}", movies.search().with_query("caorl").execute::<Movie>().await.unwrap().hits);
135136
```
136137

137138
Output:
138139
```
139-
[Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"]}]
140+
[Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"] }]
140141
```
141142

142143
Json output:
@@ -157,7 +158,15 @@ Json output:
157158
#### Custom Search <!-- omit in toc -->
158159

159160
```rust
160-
println!("{:?}", client.index("movies").search().with_query("phil").with_attributes_to_highlight(Selectors::Some(&["*"])).execute::<Movie>().await.unwrap().hits);
161+
#
162+
let search_result = movies
163+
.search()
164+
.with_query("phil")
165+
.with_attributes_to_highlight(Selectors::Some(&["*"]))
166+
.execute::<Movie>()
167+
.await
168+
.unwrap();
169+
println!("{:?}", search_result.hits);
161170
```
162171

163172
Json output:
@@ -189,9 +198,9 @@ index setting.
189198
```rust
190199
let filterable_attributes = [
191200
"id",
192-
"genres"
201+
"genres",
193202
];
194-
client.index("movies").set_filterable_attributes(&filterable_attributes).await.unwrap();
203+
movies.set_filterable_attributes(&filterable_attributes).await.unwrap();
195204
```
196205

197206
You only need to perform this operation once.
@@ -204,8 +213,15 @@ status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-st
204213
Then, you can perform the search:
205214

206215
```rust
207-
println!("{:?}", client.index("movies").search().with_query("wonder").with_filter("id > 1 AND genres = Action")
208-
.execute::<Movie>().await.unwrap().hits);
216+
#
217+
let search_result = movies
218+
.search()
219+
.with_query("wonder")
220+
.with_filter("id > 1 AND genres = Action")
221+
.execute::<Movie>()
222+
.await
223+
.unwrap();
224+
println!("{:?}", search_result.hits);
209225
```
210226

211227
Json output:

meilisearch-test-macro/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "meilisearch-test-macro"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
proc-macro2 = "1.0.0"
13+
quote = "1.0.0"
14+
syn = { version = "1.0.0", features = ["clone-impls", "full", "parsing", "printing", "proc-macro"], default-features = false }

meilisearch-test-macro/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Meilisearch test macro
2+
3+
This crate defines the `meilisearch_test` macro.
4+
5+
Since the code is a little bit harsh to read, here is a complete explanation of how to use it.
6+
The macro aims to ease the writing of tests by:
7+
1. Reducing the amount of code you need to write and maintain for each test.
8+
2. Ensuring All your indexes as a unique name so they can all run in parallel.
9+
3. Ensuring you never forget to delete your index if you need one.
10+
11+
12+
Before explaining its usage, we're going to see a simple test *before* this macro:
13+
```rust
14+
#[async_test]
15+
async fn test_get_all_tasks() -> Result<(), Error> {
16+
let client = Client::new("http://localhost:7700", "masterKey");
17+
18+
let index = client
19+
.create_index("test_get_all_tasks", None)
20+
.await?
21+
.wait_for_completion(&client, None, None)
22+
.await?
23+
.try_make_index(&client)
24+
.unwrap();
25+
26+
let tasks = index.get_all_tasks().await?;
27+
// The only task is the creation of the index
28+
assert_eq!(status.len(), 1);
29+
30+
index.delete()
31+
.await?
32+
.wait_for_completion(&client, None, None)
33+
.await?;
34+
Ok(())
35+
}
36+
```
37+
38+
I have multiple problems with this test:
39+
- `let client = Client::new("http://localhost:7700", "masterKey");`: This line is always the same in every test.
40+
And if you make a typo on the http addr or the master key, you'll have an error.
41+
- `let index = client.create_index("test_get_all_tasks", None)...`: Each test needs to have an unique name.
42+
This means we currently need to write the name of the test everywhere; it's not practical.
43+
- There are 11 lines dedicated to the creation and deletion of the index; this is once again something that'll never change
44+
whatever the test is. But, if you ever forget to delete the index at the end, you'll get in some trouble to re-run
45+
the tests.
46+
47+
-------
48+
49+
With this macro, all these problems are solved. See a rewrite of this test:
50+
```rust
51+
#[meilisearch_test]
52+
async fn test_get_all_tasks(index: Index, client: Client) -> Result<(), Error> {
53+
let tasks = index.get_all_tasks().await?;
54+
// The only task is the creation of the index
55+
assert_eq!(status.len(), 1);
56+
}
57+
```
58+
59+
So now you're probably seeing what happened. By using an index and a client in the parameter of
60+
the test, the macro automatically did the same thing we've seen before.
61+
There are a few rules, though:
62+
1. The macro only handles three types of arguments:
63+
- `String`: It returns the name of the test.
64+
- `Client`: It creates a client like that: `Client::new("http://localhost:7700", "masterKey")`.
65+
- `Index`: It creates and deletes an index, as we've seen before.
66+
2. You only get what you asked for. That means if you don't ask for an index, no index will be created in meilisearch.
67+
So, if you are testing the creation of indexes, you can ask for a `Client` and a `String` and then create it yourself.
68+
The index won't be present in meilisearch.
69+
3. You can put your parameters in the order you want it won't change anything.
70+
4. Everything you use **must** be in scope directly. If you're using an `Index`, you must write `Index` in the parameters,
71+
not `meilisearch_rust::Index` or `crate::Index`.
72+
5. And I think that's all, use and abuse it :tada:

0 commit comments

Comments
 (0)