batch_mode_batch_client/
retrieve_batch_by_id.rs

1// ---------------- [ File: batch-mode-batch-client/src/retrieve_batch_by_id.rs ]
2crate::ix!();
3
4#[async_trait]
5impl<E> RetrieveBatchById for OpenAIClientHandle<E>
6where
7    E: Debug + Send + Sync + From<OpenAIClientError>, // so we can do `.map_err(E::from)?`
8{
9    type Error = E;
10
11    async fn retrieve_batch(&self, batch_id: &str) -> Result<Batch, Self::Error> {
12        info!("retrieving batch {} from online", batch_id);
13
14        // The underlying call returns `Result<Batch, OpenAIApiError>` 
15        // or `Result<Batch, OpenAIClientError>`? Let’s assume it’s an OpenAI error:
16        let batch = self.batches().retrieve(batch_id)
17            .await
18            .map_err(|openai_err| E::from(OpenAIClientError::OpenAIError(openai_err)))?;
19
20        Ok(batch)
21    }
22}
23
24#[cfg(test)]
25mod retrieve_batch_by_id_tests {
26    use super::*;
27    use futures::executor::block_on;
28    use tempfile::tempdir;
29    use tracing::{debug, error, info, trace, warn};
30    use std::sync::Arc;
31
32    /// Exhaustive test suite for the `RetrieveBatchById` implementation on `OpenAIClientHandle`.
33    /// We use the mock client to simulate various scenarios (success, error, invalid input, etc).
34    #[traced_test]
35    async fn test_retrieve_batch_by_id_success() {
36        info!("Beginning test_retrieve_batch_by_id_success");
37        trace!("Constructing mock client...");
38        let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
39            .build()
40            .unwrap();
41        debug!("Mock client built: {:?}", mock_client);
42
43        let batch_id = "valid_batch_id";
44
45        trace!("Calling retrieve_batch on mock_client with batch_id={}", batch_id);
46        let result = mock_client.retrieve_batch(batch_id).await;
47        debug!("Result from retrieve_batch: {:?}", result);
48
49        // Expect an Ok result for a valid batch
50        assert!(
51            result.is_ok(),
52            "Expected retrieve_batch to succeed with a valid batch_id"
53        );
54        let batch = result.unwrap();
55        pretty_assert_eq!(
56            batch.id, batch_id,
57            "Retrieved batch should match the requested batch_id"
58        );
59        info!("test_retrieve_batch_by_id_success passed.");
60    }
61
62    #[traced_test]
63    async fn test_retrieve_batch_by_id_empty_input() {
64        info!("Beginning test_retrieve_batch_by_id_empty_input");
65        trace!("Constructing mock client...");
66        let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
67            .build()
68            .unwrap();
69        debug!("Mock client built: {:?}", mock_client);
70
71        let batch_id = "";
72
73        trace!("Calling retrieve_batch with an empty batch_id");
74        let result = mock_client.retrieve_batch(batch_id).await;
75        debug!("Result from retrieve_batch: {:?}", result);
76
77        // We now short-circuit in retrieve_batch: if batch_id.is_empty(), return error
78        assert!(
79            result.is_err(),
80            "Expected retrieve_batch to produce an error for empty batch_id"
81        );
82        info!("test_retrieve_batch_by_id_empty_input passed.");
83    }
84
85    #[traced_test]
86    async fn test_retrieve_batch_by_id_openai_api_error() {
87        info!("Beginning test_retrieve_batch_by_id_openai_api_error");
88        trace!("Constructing mock client that simulates an OpenAI error...");
89        let mock_client = {
90            let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
91            builder.build().unwrap()
92        };
93        debug!("Mock client built: {:?}", mock_client);
94
95        let batch_id = "trigger_api_error";
96
97        trace!("Calling retrieve_batch expecting an API error scenario...");
98        let result = mock_client.retrieve_batch(batch_id).await;
99        debug!("Result from retrieve_batch: {:?}", result);
100
101        // Because we “trigger_api_error,” the mock forcibly returns an OpenAIError.
102        assert!(
103            result.is_err(),
104            "Expected retrieve_batch to return an error due to OpenAI API error"
105        );
106        info!("test_retrieve_batch_by_id_openai_api_error passed.");
107    }
108
109    #[traced_test]
110    async fn test_retrieve_batch_by_id_other_error() {
111        info!("Beginning test_retrieve_batch_by_id_other_error");
112        trace!("Constructing mock client that simulates a different kind of error...");
113        let mock_client = {
114            let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
115            builder.build().unwrap()
116        };
117        debug!("Mock client built: {:?}", mock_client);
118
119        let batch_id = "trigger_other_error";
120
121        trace!("Calling retrieve_batch expecting a non-OpenAI error scenario...");
122        let result = mock_client.retrieve_batch(batch_id).await;
123        debug!("Result from retrieve_batch: {:?}", result);
124
125        // Because we “trigger_other_error,” the mock forcibly returns a std::io::Error
126        assert!(
127            result.is_err(),
128            "Expected retrieve_batch to return an error from a non-OpenAI scenario"
129        );
130        info!("test_retrieve_batch_by_id_other_error passed.");
131    }
132}