Skip to content

Commit 42f2674

Browse files
committed
CDRIVER-1031 mongoc_database_get_collection inherits config
Copy write concern, read concern, and read preference from database, not from the client.
1 parent 420df13 commit 42f2674

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
mongo-c-driver 1.3.2
2+
====================
3+
4+
* mongoc_database_get_collection now copies the database's read preferences,
5+
read concern, and write concern, instead of copying the client's.
6+
17
mongo-c-driver 1.3.1
28
====================
39

src/mongoc/mongoc-database.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,9 @@ mongoc_database_get_collection (mongoc_database_t *database,
11231123
BSON_ASSERT (database);
11241124
BSON_ASSERT (collection);
11251125

1126-
return mongoc_client_get_collection (database->client, database->name,
1127-
collection);
1126+
return _mongoc_collection_new (database->client, database->name, collection,
1127+
database->read_prefs, database->read_concern,
1128+
database->write_concern);
11281129
}
11291130

11301131

tests/TestSuite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ extern "C" {
6969
#define ASSERT_CMPUINT(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "u")
7070
#define ASSERT_CMPLONG(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "ld")
7171
#define ASSERT_CMPULONG(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "lu")
72+
#define ASSERT_CMPINT32(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, PRId32)
7273
#define ASSERT_CMPINT64(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, PRId64)
7374
#define ASSERT_CMPUINT64(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, PRIu64)
7475
#define ASSERT_CMPSIZE_T(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "zd")

tests/test-mongoc-database.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <mongoc.h>
2+
#include <mongoc-collection-private.h>
3+
#include <mongoc-write-concern-private.h>
4+
#include <mongoc-read-concern-private.h>
25

36
#include "TestSuite.h"
47
#include "test-libmongoc.h"
@@ -303,6 +306,46 @@ test_get_collection_info (void)
303306
mongoc_client_destroy (client);
304307
}
305308

309+
static void
310+
test_get_collection (void)
311+
{
312+
mongoc_client_t *client;
313+
mongoc_database_t *database;
314+
mongoc_write_concern_t *wc;
315+
mongoc_read_concern_t *rc;
316+
mongoc_read_prefs_t *read_prefs;
317+
mongoc_collection_t *collection;
318+
319+
client = test_framework_client_new ();
320+
assert (client);
321+
322+
database = mongoc_client_get_database (client, "test");
323+
324+
wc = mongoc_write_concern_new ();
325+
mongoc_write_concern_set_w (wc, 2);
326+
mongoc_database_set_write_concern (database, wc);
327+
328+
rc = mongoc_read_concern_new ();
329+
mongoc_read_concern_set_level (rc, "majority");
330+
mongoc_database_set_read_concern (database, rc);
331+
332+
read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
333+
mongoc_database_set_read_prefs (database, read_prefs);
334+
335+
collection = mongoc_database_get_collection (database, "test");
336+
337+
ASSERT_CMPINT32 (collection->write_concern->w, ==, 2);
338+
ASSERT_CMPSTR (collection->read_concern->level, "majority");
339+
ASSERT_CMPINT (collection->read_prefs->mode, ==, MONGOC_READ_SECONDARY);
340+
341+
mongoc_collection_destroy (collection);
342+
mongoc_read_prefs_destroy (read_prefs);
343+
mongoc_read_concern_destroy (rc);
344+
mongoc_write_concern_destroy (wc);
345+
mongoc_database_destroy (database);
346+
mongoc_client_destroy (client);
347+
}
348+
306349
static void
307350
test_get_collection_names (void)
308351
{
@@ -481,6 +524,8 @@ test_database_install (TestSuite *suite)
481524
TestSuite_Add (suite, "/Database/create_collection", test_create_collection);
482525
TestSuite_Add (suite, "/Database/get_collection_info",
483526
test_get_collection_info);
527+
TestSuite_Add (suite, "/Database/get_collection",
528+
test_get_collection);
484529
TestSuite_Add (suite, "/Database/get_collection_names",
485530
test_get_collection_names);
486531
TestSuite_Add (suite, "/Database/get_collection_names_error",

0 commit comments

Comments
 (0)