@@ -3703,13 +3703,146 @@ _test_sample_versioned_api_example_4 (void)
3703
3703
mongoc_server_api_destroy (server_api );
3704
3704
}
3705
3705
3706
+ static int64_t iso_to_unix (const char * iso_str ) {
3707
+ /* TODO (CDRIVER-2945) there is no convenient helper for converting ISO8601
3708
+ * strings to Unix timestamps. This is not shown in the example. */
3709
+ return 1628330345 ;
3710
+ }
3711
+
3712
+ static void _test_sample_versioned_api_example_5_6_7_8 (void ) {
3713
+ #define N_DOCS 8
3714
+ mongoc_client_t * client ;
3715
+ mongoc_server_api_t * server_api ;
3716
+ mongoc_server_api_version_t server_api_version ;
3717
+ bool ok ;
3718
+ bson_error_t error ;
3719
+ mongoc_database_t * db ;
3720
+ mongoc_collection_t * sales ;
3721
+ bson_t * docs [N_DOCS ];
3722
+ int i ;
3723
+ bson_t reply ;
3724
+ bson_t * cmd ;
3725
+ int64_t count ;
3726
+ bson_t * filter ;
3727
+
3728
+ client = get_client ();
3729
+ mongoc_client_set_error_api (client , MONGOC_ERROR_API_VERSION_2 );
3730
+ mongoc_server_api_version_from_string ("1" , & server_api_version );
3731
+ server_api = mongoc_server_api_new (server_api_version );
3732
+ mongoc_server_api_strict (server_api , true);
3733
+ ok = mongoc_client_set_server_api (client , server_api , & error );
3734
+ ASSERT_OR_PRINT (ok , error );
3735
+ db = mongoc_client_get_database (client , "db" );
3736
+ sales = mongoc_database_get_collection (db , "sales" );
3737
+ /* Drop db.sales in case the collection exists. */
3738
+ ok = mongoc_collection_drop (sales , & error );
3739
+ if (!ok && NULL == strstr (error .message , "ns not found" )) {
3740
+ /* Ignore an "ns not found" error on dropping the collection in case the
3741
+ * namespace does not exist. */
3742
+ ASSERT_OR_PRINT (ok , error );
3743
+ }
3744
+
3745
+ /* Start Versioned API Example 5 */
3746
+ docs [0 ] = BCON_NEW ("_id" , BCON_INT32 (1 ),
3747
+ "item" , "abc" ,
3748
+ "price" , BCON_INT32 (10 ),
3749
+ "quantity" , BCON_INT32 (2 ),
3750
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-01-01T08:00:00Z" )));
3751
+ docs [1 ] = BCON_NEW ("_id" , BCON_INT32 (2 ),
3752
+ "item" , "jkl" ,
3753
+ "price" , BCON_INT32 (20 ),
3754
+ "quantity" , BCON_INT32 (1 ),
3755
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-03T09:00:00Z" )));
3756
+ docs [2 ] = BCON_NEW ("_id" , BCON_INT32 (3 ),
3757
+ "item" , "xyz" ,
3758
+ "price" , BCON_INT32 (5 ),
3759
+ "quantity" , BCON_INT32 (5 ),
3760
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-03T09:05:00Z" )));
3761
+ docs [3 ] = BCON_NEW ("_id" , BCON_INT32 (4 ),
3762
+ "item" , "abc" ,
3763
+ "price" , BCON_INT32 (10 ),
3764
+ "quantity" , BCON_INT32 (10 ),
3765
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-15T08:00:00Z" )));
3766
+ docs [4 ] = BCON_NEW ("_id" , BCON_INT32 (5 ),
3767
+ "item" , "xyz" ,
3768
+ "price" , BCON_INT32 (5 ),
3769
+ "quantity" ,BCON_INT32 (10 ),
3770
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-15T09:05:00Z" )));
3771
+ docs [5 ] = BCON_NEW ("_id" , BCON_INT32 (6 ),
3772
+ "item" , "xyz" ,
3773
+ "price" , BCON_INT32 (5 ),
3774
+ "quantity" ,BCON_INT32 (5 ),
3775
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-15T12:05:10Z" )));
3776
+ docs [6 ] = BCON_NEW ("_id" , BCON_INT32 (7 ),
3777
+ "item" , "xyz" ,
3778
+ "price" , BCON_INT32 (5 ),
3779
+ "quantity" ,BCON_INT32 (10 ),
3780
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-02-15T14:12:12Z" )));
3781
+ docs [7 ] = BCON_NEW ("_id" , BCON_INT32 (8 ),
3782
+ "item" , "abc" ,
3783
+ "price" , BCON_INT32 (10 ),
3784
+ "quantity" ,BCON_INT32 (5 ),
3785
+ "date" , BCON_DATE_TIME (iso_to_unix ("2021-03-16T20:20:13Z" )));
3786
+ ok = mongoc_collection_insert_many (
3787
+ sales , (const bson_t * * ) docs , N_DOCS , NULL /* opts */ , & reply , & error );
3788
+ /* End Versioned API Example 5 */
3789
+ ASSERT_OR_PRINT (ok , error );
3790
+ bson_destroy (& reply );
3791
+
3792
+ cmd = BCON_NEW ("count" , "sales" );
3793
+ ok = mongoc_database_command_simple (
3794
+ db , cmd , NULL /* read_prefs */ , & reply , & error );
3795
+ ASSERT_ERROR_CONTAINS (error ,
3796
+ MONGOC_ERROR_SERVER ,
3797
+ 323 ,
3798
+ "Provided apiStrict:true, but the command count is not in API Version 1" );
3799
+ ASSERT (!ok );
3800
+ bson_destroy (& reply );
3801
+ #if 0
3802
+ /* This block not evaluated, but is inserted into documentation to represent the above reply.
3803
+ * Don't delete me! */
3804
+ /* Start Versioned API Example 6 */
3805
+ char * str = bson_as_json (& reply , NULL /* length */ );
3806
+ printf ("%s" , str );
3807
+ /* Prints the server reply:
3808
+ * { "ok" : 0, "errmsg" : "Provided apiStrict:true, but the command count is not in API Version 1", "code" : 323, "codeName" : "APIStrictError" } */
3809
+ bson_free (str );
3810
+ /* End Versioned API Example 6 */
3811
+ #endif
3812
+
3813
+ /* Start Versioned API Example 7 */
3814
+ filter = bson_new ();
3815
+ count = mongoc_collection_count_documents (
3816
+ sales , filter , NULL /* opts */ , NULL /* read_prefs */ , & reply , & error );
3817
+ /* End Versioned API Example 7 */
3818
+ if (N_DOCS != count ) {
3819
+ test_error ("expected %d documents, got %" PRId64 , N_DOCS , count );
3820
+ }
3821
+ bson_destroy (& reply );
3822
+
3823
+ /* Start Versioned API Example 8 */
3824
+ BSON_ASSERT (count == N_DOCS );
3825
+ /* End Versioned API Example 8 */
3826
+
3827
+ bson_destroy (filter );
3828
+ bson_destroy (cmd );
3829
+ for (i = 0 ; i < N_DOCS ; i ++ ) {
3830
+ bson_destroy (docs [i ]);
3831
+ }
3832
+ mongoc_collection_destroy (sales );
3833
+ mongoc_database_destroy (db );
3834
+ mongoc_server_api_destroy (server_api );
3835
+ mongoc_client_destroy (client );
3836
+ }
3837
+
3706
3838
static void
3707
3839
test_sample_versioned_api (void )
3708
3840
{
3709
3841
_test_sample_versioned_api_example_1 ();
3710
3842
_test_sample_versioned_api_example_2 ();
3711
3843
_test_sample_versioned_api_example_3 ();
3712
3844
_test_sample_versioned_api_example_4 ();
3845
+ _test_sample_versioned_api_example_5_6_7_8 ();
3713
3846
}
3714
3847
3715
3848
static void
0 commit comments