Skip to content

Rename MEILISEARCH_HOST to MEILISEARCH_URL #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct Movie {

fn main() { block_on(async move {
// Create a client (without sending any request so that can't fail)
let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);

// An index is where the documents are stored.
let movies = client.index("movies");
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
stdin_open: true
working_dir: /home/package
environment:
- MEILISEARCH_HOST=http://meilisearch:7700
- MEILISEARCH_URL=http://meilisearch:7700
- CARGO_HOME=/vendor/cargo
depends_on:
- meilisearch
Expand Down
26 changes: 16 additions & 10 deletions meilisearch-test-macro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ This crate defines the `meilisearch_test` macro.

Since the code is a little bit harsh to read, here is a complete explanation of how to use it.
The macro aims to ease the writing of tests by:

1. Reducing the amount of code you need to write and maintain for each test.
2. Ensuring All your indexes as a unique name so they can all run in parallel.
3. Ensuring you never forget to delete your index if you need one.

Before explaining its usage, we're going to see a simple test _before_ this macro:

Before explaining its usage, we're going to see a simple test *before* this macro:
```rust
#[async_test]
async fn test_get_tasks() -> Result<(), Error> {
let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);

let index = client
.create_index("test_get_tasks", None)
Expand All @@ -36,17 +37,19 @@ async fn test_get_tasks() -> Result<(), Error> {
```

I have multiple problems with this test:
- `let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);`: This line is always the same in every test.

- `let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);`: This line is always the same in every test.
And if you make a typo on the http addr or the master key, you'll have an error.
- `let index = client.create_index("test_get_tasks", None)...`: Each test needs to have an unique name.
This means we currently need to write the name of the test everywhere; it's not practical.
- There are 11 lines dedicated to the creation and deletion of the index; this is once again something that'll never change
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
the tests.

-------
---

With this macro, all these problems are solved. See a rewrite of this test:

```rust
#[meilisearch_test]
async fn test_get_tasks(index: Index, client: Client) -> Result<(), Error> {
Expand All @@ -59,14 +62,17 @@ async fn test_get_tasks(index: Index, client: Client) -> Result<(), Error> {
So now you're probably seeing what happened. By using an index and a client in the parameter of
the test, the macro automatically did the same thing we've seen before.
There are a few rules, though:

1. The macro only handles three types of arguments:
- `String`: It returns the name of the test.
- `Client`: It creates a client like that: `Client::new("http://localhost:7700", "masterKey")`.
- `Index`: It creates and deletes an index, as we've seen before.

- `String`: It returns the name of the test.
- `Client`: It creates a client like that: `Client::new("http://localhost:7700", "masterKey")`.
- `Index`: It creates and deletes an index, as we've seen before.

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.
So, if you are testing the creation of indexes, you can ask for a `Client` and a `String` and then create it yourself.
The index won't be present in meilisearch.
So, if you are testing the creation of indexes, you can ask for a `Client` and a `String` and then create it yourself.
The index won't be present in meilisearch.
3. You can put your parameters in the order you want it won't change anything.
4. Everything you use **must** be in scope directly. If you're using an `Index`, you must write `Index` in the parameters,
not `meilisearch_rust::Index` or `crate::Index`.
not `meilisearch_rust::Index` or `crate::Index`.
5. And I think that's all, use and abuse it 🎉
4 changes: 2 additions & 2 deletions meilisearch-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ pub fn meilisearch_test(params: TokenStream, input: TokenStream) -> TokenStream
// First we need to check if a client will be used and create it if it’s the case
if use_client {
outer_block.push(parse_quote!(
let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
));
outer_block.push(parse_quote!(
let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
));
outer_block.push(parse_quote!(
let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
));
}

Expand Down
Loading