Skip to content

Commit 8b6ab76

Browse files
alcaeuskevinAlbs
andauthored
CDRIVER-3991 Add versioned API connection examples (#805)
* CDRIVER-3991 Add versioned API connection examples * Use mongoc_client_new in public examples Co-authored-by: Kevin Albertson <[email protected]> * Test versioned API samples on 4.9+ only * Free client and server_api object in tests Co-authored-by: Kevin Albertson <[email protected]>
1 parent dad3355 commit 8b6ab76

File tree

1 file changed

+146
-2
lines changed

1 file changed

+146
-2
lines changed

src/libmongoc/tests/test-mongoc-sample-commands.c

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,11 +3464,11 @@ with_transaction_example (bson_error_t *error)
34643464
* members in the URI string; e.g.
34653465
* uri_repl = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:" \
34663466
* "27017/?replicaSet=myRepl";
3467-
* client = test_framework_client_new (uri_repl);
3467+
* client = mongoc_client_new (uri_repl);
34683468
* For a sharded cluster, connect to the mongos instances; e.g.
34693469
* uri_sharded =
34703470
* "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
3471-
* client = test_framework_client_new (uri_sharded);
3471+
* client = mongoc_client_new (uri_sharded);
34723472
*/
34733473

34743474
client = get_client ();
@@ -3572,6 +3572,146 @@ callback (mongoc_client_session_t *session,
35723572
}
35733573
/* End Transactions withTxn API Example 1 */
35743574

3575+
static void
3576+
_test_sample_versioned_api_example_1 (void)
3577+
{
3578+
/* Start Versioned API Example 1 */
3579+
mongoc_client_t *client = NULL;
3580+
mongoc_server_api_t *server_api = NULL;
3581+
mongoc_server_api_version_t server_api_version;
3582+
bson_error_t error;
3583+
3584+
/* For a replica set, include the replica set name and a seedlist of the
3585+
* members in the URI string; e.g.
3586+
* uri_repl = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:" \
3587+
* "27017/?replicaSet=myRepl";
3588+
* client = mongoc_client_new (uri_repl);
3589+
* For a sharded cluster, connect to the mongos instances; e.g.
3590+
* uri_sharded =
3591+
* "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
3592+
* client = mongoc_client_new (uri_sharded);
3593+
*/
3594+
3595+
client = get_client ();
3596+
3597+
mongoc_server_api_version_from_string ("1", &server_api_version);
3598+
server_api = mongoc_server_api_new (server_api_version);
3599+
3600+
mongoc_client_set_server_api (client, server_api, &error);
3601+
/* End Versioned API Example 1 */
3602+
3603+
mongoc_client_destroy (client);
3604+
mongoc_server_api_destroy (server_api);
3605+
}
3606+
3607+
static void
3608+
_test_sample_versioned_api_example_2 (void)
3609+
{
3610+
/* Start Versioned API Example 2 */
3611+
mongoc_client_t *client = NULL;
3612+
mongoc_server_api_t *server_api = NULL;
3613+
mongoc_server_api_version_t server_api_version;
3614+
bson_error_t error;
3615+
3616+
/* For a replica set, include the replica set name and a seedlist of the
3617+
* members in the URI string; e.g.
3618+
* uri_repl = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:" \
3619+
* "27017/?replicaSet=myRepl";
3620+
* client = mongoc_client_new (uri_repl);
3621+
* For a sharded cluster, connect to the mongos instances; e.g.
3622+
* uri_sharded =
3623+
* "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
3624+
* client = mongoc_client_new (uri_sharded);
3625+
*/
3626+
3627+
client = get_client ();
3628+
3629+
mongoc_server_api_version_from_string ("1", &server_api_version);
3630+
server_api = mongoc_server_api_new (server_api_version);
3631+
mongoc_server_api_strict (server_api, true);
3632+
3633+
mongoc_client_set_server_api (client, server_api, &error);
3634+
/* End Versioned API Example 2 */
3635+
3636+
mongoc_client_destroy (client);
3637+
mongoc_server_api_destroy (server_api);
3638+
}
3639+
3640+
static void
3641+
_test_sample_versioned_api_example_3 (void)
3642+
{
3643+
/* Start Versioned API Example 3 */
3644+
mongoc_client_t *client = NULL;
3645+
mongoc_server_api_t *server_api = NULL;
3646+
mongoc_server_api_version_t server_api_version;
3647+
bson_error_t error;
3648+
3649+
/* For a replica set, include the replica set name and a seedlist of the
3650+
* members in the URI string; e.g.
3651+
* uri_repl = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:" \
3652+
* "27017/?replicaSet=myRepl";
3653+
* client = mongoc_client_new (uri_repl);
3654+
* For a sharded cluster, connect to the mongos instances; e.g.
3655+
* uri_sharded =
3656+
* "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
3657+
* client = mongoc_client_new (uri_sharded);
3658+
*/
3659+
3660+
client = get_client ();
3661+
3662+
mongoc_server_api_version_from_string ("1", &server_api_version);
3663+
server_api = mongoc_server_api_new (server_api_version);
3664+
mongoc_server_api_strict (server_api, false);
3665+
3666+
mongoc_client_set_server_api (client, server_api, &error);
3667+
/* End Versioned API Example 3 */
3668+
3669+
mongoc_client_destroy (client);
3670+
mongoc_server_api_destroy (server_api);
3671+
}
3672+
3673+
static void
3674+
_test_sample_versioned_api_example_4 (void)
3675+
{
3676+
/* Start Versioned API Example 4 */
3677+
mongoc_client_t *client = NULL;
3678+
mongoc_server_api_t *server_api = NULL;
3679+
mongoc_server_api_version_t server_api_version;
3680+
bson_error_t error;
3681+
3682+
/* For a replica set, include the replica set name and a seedlist of the
3683+
* members in the URI string; e.g.
3684+
* uri_repl = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:" \
3685+
* "27017/?replicaSet=myRepl";
3686+
* client = mongoc_client_new (uri_repl);
3687+
* For a sharded cluster, connect to the mongos instances; e.g.
3688+
* uri_sharded =
3689+
* "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
3690+
* client = mongoc_client_new (uri_sharded);
3691+
*/
3692+
3693+
client = get_client ();
3694+
3695+
mongoc_server_api_version_from_string ("1", &server_api_version);
3696+
server_api = mongoc_server_api_new (server_api_version);
3697+
mongoc_server_api_deprecation_errors (server_api, true);
3698+
3699+
mongoc_client_set_server_api (client, server_api, &error);
3700+
/* End Versioned API Example 4 */
3701+
3702+
mongoc_client_destroy (client);
3703+
mongoc_server_api_destroy (server_api);
3704+
}
3705+
3706+
static void
3707+
test_sample_versioned_api (void)
3708+
{
3709+
_test_sample_versioned_api_example_1 ();
3710+
_test_sample_versioned_api_example_2 ();
3711+
_test_sample_versioned_api_example_3 ();
3712+
_test_sample_versioned_api_example_4 ();
3713+
}
3714+
35753715
static void
35763716
test_sample_commands (void)
35773717
{
@@ -3649,6 +3789,10 @@ test_sample_commands (void)
36493789
test_sample_txn_commands (client);
36503790
}
36513791

3792+
if (test_framework_max_wire_version_at_least (WIRE_VERSION_4_9)) {
3793+
test_sample_versioned_api ();
3794+
}
3795+
36523796
mongoc_collection_drop (collection, NULL);
36533797

36543798
mongoc_collection_destroy (collection);

0 commit comments

Comments
 (0)