11use futures:: TryStreamExt ;
2+ use tokio:: sync:: RwLockReadGuard ;
23
34use crate :: {
45 bson:: { doc, Bson } ,
56 error:: Result ,
6- options:: FindOptions ,
7- test:: TestClient ,
7+ options:: { ClientOptions , CursorType , FindOptions , ServerApi , ServerApiVersion } ,
8+ test:: { TestClient , DEFAULT_URI , LOCK } ,
9+ Client ,
810 Collection ,
911} ;
1012
@@ -1364,9 +1366,68 @@ async fn delete_examples(collection: &Collection) -> Result<()> {
13641366 Ok ( ( ) )
13651367}
13661368
1369+ #[ allow( unused_variables) ]
1370+ #[ cfg( not( feature = "sync" ) ) ]
1371+ async fn versioned_api_examples ( ) -> Result < ( ) > {
1372+ let setup_client = TestClient :: new ( ) . await ;
1373+ if setup_client. server_version_lt ( 4 , 9 ) {
1374+ println ! ( "skipping versioned API examples due to unsupported server version" ) ;
1375+ return Ok ( ( ) ) ;
1376+ }
1377+
1378+ let uri = DEFAULT_URI . clone ( ) ;
1379+ // Start 1. Declare an API version on a client
1380+ let mut options = ClientOptions :: parse ( & uri) . await ?;
1381+ let server_api = ServerApi :: builder ( )
1382+ . version ( ServerApiVersion :: Version1 )
1383+ . build ( ) ;
1384+ options. server_api = Some ( server_api) ;
1385+ let client = Client :: with_options ( options) ?;
1386+ let cursor = client
1387+ . database ( "versioned_api_example" )
1388+ . collection ( "example" )
1389+ . find ( None , None )
1390+ . await ?;
1391+ // End 1.
1392+
1393+ // Start 2. Strict option
1394+ let mut options = ClientOptions :: parse ( & uri) . await ?;
1395+ let server_api = ServerApi :: builder ( )
1396+ . version ( ServerApiVersion :: Version1 )
1397+ . strict ( true )
1398+ . build ( ) ;
1399+ options. server_api = Some ( server_api) ;
1400+ let client = Client :: with_options ( options) ?;
1401+
1402+ let find_options = FindOptions :: builder ( )
1403+ . cursor_type ( CursorType :: Tailable )
1404+ . build ( ) ;
1405+ let cursor = client
1406+ . database ( "versioned_api_example" )
1407+ . collection ( "example" )
1408+ . find ( None , find_options)
1409+ . await
1410+ . expect_err ( "should fail" ) ;
1411+ // End 2.
1412+
1413+ // Start 3. deprecationErrors option
1414+ let mut options = ClientOptions :: parse ( & uri) . await ?;
1415+ let server_api = ServerApi :: builder ( )
1416+ . version ( ServerApiVersion :: Version1 )
1417+ . deprecation_errors ( true )
1418+ . build ( ) ;
1419+ options. server_api = Some ( server_api) ;
1420+ let client = Client :: with_options ( options) ?;
1421+ // End 3.
1422+
1423+ Ok ( ( ) )
1424+ }
1425+
13671426#[ cfg_attr( feature = "tokio-runtime" , tokio:: test) ]
13681427#[ cfg_attr( feature = "async-std-runtime" , async_std:: test) ]
13691428async fn test ( ) {
1429+ let _guard: RwLockReadGuard < _ > = LOCK . run_concurrently ( ) . await ;
1430+
13701431 let client = TestClient :: new ( ) . await ;
13711432 let coll = client
13721433 . database ( "documentation_examples" )
@@ -1383,4 +1444,5 @@ async fn test() {
13831444 projection_examples ( & coll) . await . unwrap ( ) ;
13841445 update_examples ( & coll) . await . unwrap ( ) ;
13851446 delete_examples ( & coll) . await . unwrap ( ) ;
1447+ versioned_api_examples ( ) . await . unwrap ( ) ;
13861448}
0 commit comments