crate::ix!();
#[async_trait]
impl<E> RetrieveBatchById for OpenAIClientHandle<E>
where
E: Debug + Send + Sync + From<OpenAIClientError>, {
type Error = E;
async fn retrieve_batch(&self, batch_id: &str) -> Result<Batch, Self::Error> {
info!("retrieving batch {} from online", batch_id);
let batch = self.batches().retrieve(batch_id)
.await
.map_err(|openai_err| E::from(OpenAIClientError::OpenAIError(openai_err)))?;
Ok(batch)
}
}
#[cfg(test)]
mod retrieve_batch_by_id_tests {
use super::*;
use futures::executor::block_on;
use tempfile::tempdir;
use tracing::{debug, error, info, trace, warn};
use std::sync::Arc;
#[traced_test]
async fn test_retrieve_batch_by_id_success() {
info!("Beginning test_retrieve_batch_by_id_success");
trace!("Constructing mock client...");
let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
.build()
.unwrap();
debug!("Mock client built: {:?}", mock_client);
let batch_id = "valid_batch_id";
trace!("Calling retrieve_batch on mock_client with batch_id={}", batch_id);
let result = mock_client.retrieve_batch(batch_id).await;
debug!("Result from retrieve_batch: {:?}", result);
assert!(
result.is_ok(),
"Expected retrieve_batch to succeed with a valid batch_id"
);
let batch = result.unwrap();
pretty_assert_eq!(
batch.id, batch_id,
"Retrieved batch should match the requested batch_id"
);
info!("test_retrieve_batch_by_id_success passed.");
}
#[traced_test]
async fn test_retrieve_batch_by_id_empty_input() {
info!("Beginning test_retrieve_batch_by_id_empty_input");
trace!("Constructing mock client...");
let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
.build()
.unwrap();
debug!("Mock client built: {:?}", mock_client);
let batch_id = "";
trace!("Calling retrieve_batch with an empty batch_id");
let result = mock_client.retrieve_batch(batch_id).await;
debug!("Result from retrieve_batch: {:?}", result);
assert!(
result.is_err(),
"Expected retrieve_batch to produce an error for empty batch_id"
);
info!("test_retrieve_batch_by_id_empty_input passed.");
}
#[traced_test]
async fn test_retrieve_batch_by_id_openai_api_error() {
info!("Beginning test_retrieve_batch_by_id_openai_api_error");
trace!("Constructing mock client that simulates an OpenAI error...");
let mock_client = {
let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
builder.build().unwrap()
};
debug!("Mock client built: {:?}", mock_client);
let batch_id = "trigger_api_error";
trace!("Calling retrieve_batch expecting an API error scenario...");
let result = mock_client.retrieve_batch(batch_id).await;
debug!("Result from retrieve_batch: {:?}", result);
assert!(
result.is_err(),
"Expected retrieve_batch to return an error due to OpenAI API error"
);
info!("test_retrieve_batch_by_id_openai_api_error passed.");
}
#[traced_test]
async fn test_retrieve_batch_by_id_other_error() {
info!("Beginning test_retrieve_batch_by_id_other_error");
trace!("Constructing mock client that simulates a different kind of error...");
let mock_client = {
let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
builder.build().unwrap()
};
debug!("Mock client built: {:?}", mock_client);
let batch_id = "trigger_other_error";
trace!("Calling retrieve_batch expecting a non-OpenAI error scenario...");
let result = mock_client.retrieve_batch(batch_id).await;
debug!("Result from retrieve_batch: {:?}", result);
assert!(
result.is_err(),
"Expected retrieve_batch to return an error from a non-OpenAI scenario"
);
info!("test_retrieve_batch_by_id_other_error passed.");
}
}