Files
mistralai-client-rs/tests/v1_client_new_test.rs
Ivan Gabriele 33876183e4 feat!: wrap Client::new() return in a Result
BREAKING CHANGE: `Client::new()` now returns a `Result`.
2024-03-04 04:43:22 +01:00

76 lines
2.5 KiB
Rust

use jrest::expect;
use mistralai_client::v1::{client::Client, error::ClientError};
#[test]
fn test_client_new_with_none_params() {
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
std::env::remove_var("MISTRAL_API_KEY");
std::env::set_var("MISTRAL_API_KEY", "test_api_key_from_env");
let client = Client::new(None, None, None, None).unwrap();
expect!(client.api_key).to_be("test_api_key_from_env".to_string());
expect!(client.endpoint).to_be("https://api.mistral.ai/v1".to_string());
expect!(client.max_retries).to_be(5);
expect!(client.timeout).to_be(120);
match maybe_original_mistral_api_key {
Some(original_mistral_api_key) => {
std::env::set_var("MISTRAL_API_KEY", original_mistral_api_key)
}
None => std::env::remove_var("MISTRAL_API_KEY"),
}
}
#[test]
fn test_client_new_with_all_params() {
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
std::env::remove_var("MISTRAL_API_KEY");
std::env::set_var("MISTRAL_API_KEY", "test_api_key_from_env");
let api_key = Some("test_api_key_from_param".to_string());
let endpoint = Some("https://example.org".to_string());
let max_retries = Some(10);
let timeout = Some(20);
let client = Client::new(
api_key.clone(),
endpoint.clone(),
max_retries.clone(),
timeout.clone(),
)
.unwrap();
expect!(client.api_key).to_be(api_key.unwrap());
expect!(client.endpoint).to_be(endpoint.unwrap());
expect!(client.max_retries).to_be(max_retries.unwrap());
expect!(client.timeout).to_be(timeout.unwrap());
match maybe_original_mistral_api_key {
Some(original_mistral_api_key) => {
std::env::set_var("MISTRAL_API_KEY", original_mistral_api_key)
}
None => std::env::remove_var("MISTRAL_API_KEY"),
}
}
#[test]
fn test_client_new_with_missing_api_key() {
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
std::env::remove_var("MISTRAL_API_KEY");
let call = || Client::new(None, None, None, None);
match call() {
Ok(_) => panic!("Expected `ClientError::ApiKeyError` but got Ok.`"),
Err(error) => assert_eq!(error, ClientError::ApiKeyError),
}
match maybe_original_mistral_api_key {
Some(original_mistral_api_key) => {
std::env::set_var("MISTRAL_API_KEY", original_mistral_api_key)
}
None => std::env::remove_var("MISTRAL_API_KEY"),
}
}