@@ -30,17 +30,29 @@ pub struct Batch {
3030 ///
3131 /// Introduced in Meilisearch v1.15.
3232 #[ serde( skip_serializing_if = "Option::is_none" ) ]
33- pub batch_strategy : Option < String > ,
33+ pub batch_strategy : Option < BatchStrategy > ,
34+ }
35+
36+ /// Reason why the autobatcher stopped batching tasks.
37+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize ) ]
38+ #[ serde( rename_all = "snake_case" ) ]
39+ #[ non_exhaustive]
40+ pub enum BatchStrategy {
41+ /// The batch reached its configured size threshold.
42+ SizeLimitReached ,
43+ /// The batch reached its configured time window threshold.
44+ TimeLimitReached ,
45+ /// Unknown strategy (forward-compatibility).
46+ #[ serde( other) ]
47+ Unknown ,
3448}
3549
3650#[ derive( Debug , Clone , Deserialize ) ]
3751#[ serde( rename_all = "camelCase" ) ]
3852pub struct BatchesResults {
3953 pub results : Vec < Batch > ,
40- #[ serde( skip_serializing_if = "Option::is_none" ) ]
41- pub total : Option < u64 > ,
42- #[ serde( skip_serializing_if = "Option::is_none" ) ]
43- pub limit : Option < u32 > ,
54+ pub total : u32 ,
55+ pub limit : u32 ,
4456 #[ serde( skip_serializing_if = "Option::is_none" ) ]
4557 pub from : Option < u32 > ,
4658 #[ serde( skip_serializing_if = "Option::is_none" ) ]
@@ -91,6 +103,7 @@ impl<'a, Http: HttpClient> BatchesQuery<'a, Http> {
91103
92104#[ cfg( test) ]
93105mod tests {
106+ use crate :: batches:: BatchStrategy ;
94107 use crate :: client:: Client ;
95108
96109 #[ tokio:: test]
@@ -130,7 +143,7 @@ mod tests {
130143 assert_eq ! ( batches. results. len( ) , 1 ) ;
131144 let b = & batches. results [ 0 ] ;
132145 assert_eq ! ( b. uid, 42 ) ;
133- assert_eq ! ( b. batch_strategy. as_deref ( ) , Some ( "time_limit_reached" ) ) ;
146+ assert_eq ! ( b. batch_strategy, Some ( BatchStrategy :: TimeLimitReached ) ) ;
134147 }
135148
136149 #[ tokio:: test]
@@ -156,6 +169,31 @@ mod tests {
156169 let client = Client :: new ( base, None :: < String > ) . unwrap ( ) ;
157170 let batch = client. get_batch ( 99 ) . await . expect ( "get batch failed" ) ;
158171 assert_eq ! ( batch. uid, 99 ) ;
159- assert_eq ! ( batch. batch_strategy. as_deref( ) , Some ( "size_limit_reached" ) ) ;
172+ assert_eq ! ( batch. batch_strategy, Some ( BatchStrategy :: SizeLimitReached ) ) ;
173+ }
174+
175+ #[ tokio:: test]
176+ async fn test_query_serialization_for_batches ( ) {
177+ use mockito:: Matcher ;
178+ let mut s = mockito:: Server :: new_async ( ) . await ;
179+ let base = s. url ( ) ;
180+
181+ let _m = s
182+ . mock ( "GET" , "/batches" )
183+ . match_query ( Matcher :: AllOf ( vec ! [
184+ Matcher :: UrlEncoded ( "limit" . into( ) , "2" . into( ) ) ,
185+ Matcher :: UrlEncoded ( "from" . into( ) , "40" . into( ) ) ,
186+ ] ) )
187+ . with_status ( 200 )
188+ . with_header ( "content-type" , "application/json" )
189+ . with_body ( r#"{"results":[],"limit":2,"total":0}"# )
190+ . create_async ( )
191+ . await ;
192+
193+ let client = Client :: new ( base, None :: < String > ) . unwrap ( ) ;
194+ let mut q = crate :: batches:: BatchesQuery :: new ( & client) ;
195+ let _ = q. with_limit ( 2 ) . with_from ( 40 ) ;
196+ let res = client. get_batches_with ( & q) . await . expect ( "request failed" ) ;
197+ assert_eq ! ( res. limit, 2 ) ;
160198 }
161199}
0 commit comments