Skip to content

Commit 765d457

Browse files
author
Christian Hergert
committed
tests: try to use the test database to save creating files.
1 parent 2706e72 commit 765d457

File tree

6 files changed

+118
-40
lines changed

6 files changed

+118
-40
lines changed

tests/test-libmongoc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ log_handler (mongoc_log_level_t log_level,
6363
char MONGOC_TEST_HOST [1024];
6464
char MONGOC_TEST_UNIQUE [32];
6565

66+
char *
67+
gen_collection_name (const char *str)
68+
{
69+
return bson_strdup_printf ("%s_%u_%u",
70+
str,
71+
(unsigned)time(NULL),
72+
(unsigned)gettestpid());
73+
74+
}
75+
6676
static void
6777
set_mongoc_test_host(void)
6878
{

tests/test-libmongoc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define TEST_LIBMONGOC_H
2020

2121
extern char MONGOC_TEST_HOST [1024];
22-
extern char MONGOC_TEST_UNIQUE [32];
22+
23+
char *gen_collection_name (const char *prefix);
2324

2425
#endif

tests/test-mongoc-client.c

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "test-libmongoc.h"
1111

1212
static char *gTestUri;
13-
static char *gTestUriWithPassword;
1413
static char *gTestUriWithBadPassword;
1514

1615

@@ -38,6 +37,39 @@ usleep (int64_t usec)
3837
#endif
3938

4039

40+
static mongoc_collection_t *
41+
get_test_collection (mongoc_client_t *client,
42+
const char *name)
43+
{
44+
mongoc_collection_t *ret;
45+
char *str;
46+
47+
str = gen_collection_name (name);
48+
ret = mongoc_client_get_collection (client, "test", str);
49+
bson_free (str);
50+
51+
return ret;
52+
}
53+
54+
55+
static char *
56+
gen_test_user (void)
57+
{
58+
return bson_strdup_printf ("testuser_%u_%u",
59+
(unsigned)time(NULL),
60+
(unsigned)gettestpid());
61+
}
62+
63+
64+
static char *
65+
gen_good_uri (const char *username)
66+
{
67+
return bson_strdup_printf("mongodb://%s:testpass@%s:27017/test",
68+
username,
69+
MONGOC_TEST_HOST);
70+
}
71+
72+
4173
static void
4274
test_mongoc_client_authenticate (void)
4375
{
@@ -47,16 +79,21 @@ test_mongoc_client_authenticate (void)
4779
mongoc_cursor_t *cursor;
4880
const bson_t *doc;
4981
bson_error_t error;
82+
char *username;
83+
char *uri;
5084
bool r;
5185
bson_t q;
5286

87+
username = gen_test_user ();
88+
uri = gen_good_uri (username);
89+
5390
/*
5491
* Add a user to the test database.
5592
*/
5693
client = mongoc_client_new(gTestUri);
5794
database = mongoc_client_get_database(client, "test");
58-
mongoc_database_remove_user (database, "testuser", &error);
59-
r = mongoc_database_add_user(database, "testuser", "testpass", NULL, NULL, &error);
95+
mongoc_database_remove_user (database, username, &error);
96+
r = mongoc_database_add_user(database, username, "testpass", NULL, NULL, &error);
6097
ASSERT_CMPINT(r, ==, 1);
6198
mongoc_database_destroy(database);
6299
mongoc_client_destroy(client);
@@ -65,7 +102,7 @@ test_mongoc_client_authenticate (void)
65102
* Try authenticating with that user.
66103
*/
67104
bson_init(&q);
68-
client = mongoc_client_new(gTestUriWithPassword);
105+
client = mongoc_client_new(uri);
69106
collection = mongoc_client_get_collection(client, "test", "test");
70107
cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0,
71108
&q, NULL, NULL);
@@ -80,6 +117,9 @@ test_mongoc_client_authenticate (void)
80117
mongoc_cursor_destroy(cursor);
81118
mongoc_collection_destroy(collection);
82119
mongoc_client_destroy(client);
120+
121+
bson_free (username);
122+
bson_free (uri);
83123
}
84124

85125

@@ -342,6 +382,7 @@ test_mongoc_client_command (void)
342382
static void
343383
test_exhaust_cursor (void)
344384
{
385+
mongoc_write_concern_t *wr;
345386
mongoc_client_t *client;
346387
mongoc_collection_t *collection;
347388
mongoc_cursor_t *cursor;
@@ -356,19 +397,18 @@ test_exhaust_cursor (void)
356397
bool r;
357398
bson_error_t error;
358399
bson_oid_t oid;
359-
char dbname [32];
360400

361401
client = mongoc_client_new (gTestUri);
362402
assert (client);
363403

364-
bson_snprintf (dbname, sizeof dbname, "tests%u_%d",
365-
(unsigned)time (NULL), gettestpid ());
366-
367-
collection = mongoc_client_get_collection(client, dbname, "test_exhaust_cursor");
368-
assert(collection);
404+
collection = get_test_collection (client, "test_exhaust_cursor");
405+
assert (collection);
369406

370407
mongoc_collection_drop(collection, &error);
371408

409+
wr = mongoc_write_concern_new ();
410+
mongoc_write_concern_set_journal (wr, true);
411+
372412
/* bulk insert some records to work on */
373413
{
374414
bson_init(&q);
@@ -382,7 +422,7 @@ test_exhaust_cursor (void)
382422
}
383423

384424
r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE,
385-
(const bson_t **)bptr, 10, NULL, &error);
425+
(const bson_t **)bptr, 10, wr, &error);
386426

387427
if (!r) {
388428
MONGOC_WARNING("Insert bulk failure: %s\n", error.message);
@@ -449,7 +489,7 @@ test_exhaust_cursor (void)
449489
/* make sure writes fail as well */
450490
{
451491
r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE,
452-
(const bson_t **)bptr, 10, NULL, &error);
492+
(const bson_t **)bptr, 10, wr, &error);
453493
assert (!r);
454494
assert (error.domain == MONGOC_ERROR_CLIENT);
455495
assert (error.code == MONGOC_ERROR_CLIENT_IN_EXHAUST);
@@ -496,6 +536,7 @@ test_exhaust_cursor (void)
496536
r = mongoc_collection_drop (collection, &error);
497537
assert (r);
498538

539+
mongoc_write_concern_destroy (wr);
499540
mongoc_cursor_destroy (cursor2);
500541
mongoc_collection_destroy(collection);
501542
mongoc_client_destroy (client);
@@ -506,7 +547,6 @@ static void
506547
cleanup_globals (void)
507548
{
508549
bson_free(gTestUri);
509-
bson_free(gTestUriWithPassword);
510550
bson_free(gTestUriWithBadPassword);
511551
}
512552

@@ -517,7 +557,6 @@ test_client_install (TestSuite *suite)
517557
bool local;
518558

519559
gTestUri = bson_strdup_printf("mongodb://%s:27017/", MONGOC_TEST_HOST);
520-
gTestUriWithPassword = bson_strdup_printf("mongodb://testuser:testpass@%s:27017/test", MONGOC_TEST_HOST);
521560
gTestUriWithBadPassword = bson_strdup_printf("mongodb://baduser:badpass@%s:27017/test", MONGOC_TEST_HOST);
522561

523562
local = !getenv ("MONGOC_DISABLE_MOCK_SERVER");

tests/test-mongoc-collection.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ static char *gTestUri;
1212
static mongoc_database_t *
1313
get_test_database (mongoc_client_t *client)
1414
{
15-
return mongoc_client_get_database (client, MONGOC_TEST_UNIQUE);
15+
return mongoc_client_get_database (client, "test");
16+
}
17+
18+
19+
static mongoc_collection_t *
20+
get_test_collection (mongoc_client_t *client,
21+
const char *prefix)
22+
{
23+
mongoc_collection_t *ret;
24+
char *str;
25+
26+
str = gen_collection_name (prefix);
27+
ret = mongoc_client_get_collection (client, "test", str);
28+
bson_free (str);
29+
30+
return ret;
1631
}
1732

1833

@@ -36,7 +51,7 @@ test_insert (void)
3651
database = get_test_database (client);
3752
ASSERT (database);
3853

39-
collection = mongoc_database_get_collection (database, "test_insert");
54+
collection = get_test_collection (client, "test_insert");
4055
ASSERT (collection);
4156

4257
mongoc_collection_drop(collection, &error);
@@ -99,7 +114,7 @@ test_insert_bulk (void)
99114
database = get_test_database (client);
100115
ASSERT (database);
101116

102-
collection = mongoc_database_get_collection(database, "test_insert_bulk");
117+
collection = get_test_collection (client, "test_insert_bulk");
103118
ASSERT (collection);
104119

105120
mongoc_collection_drop(collection, &error);
@@ -189,10 +204,10 @@ test_save (void)
189204
database = get_test_database (client);
190205
ASSERT (database);
191206

192-
collection = mongoc_database_get_collection (database, "test_save");
207+
collection = get_test_collection (client, "test_save");
193208
ASSERT (collection);
194209

195-
mongoc_collection_drop(collection, &error);
210+
mongoc_collection_drop (collection, &error);
196211

197212
context = bson_context_new(BSON_CONTEXT_NONE);
198213
ASSERT (context);
@@ -241,7 +256,7 @@ test_regex (void)
241256
database = get_test_database (client);
242257
ASSERT (database);
243258

244-
collection = mongoc_database_get_collection (database, "test_regex");
259+
collection = get_test_collection (client, "test_regex");
245260
ASSERT (collection);
246261

247262
wr = mongoc_write_concern_new ();
@@ -299,7 +314,7 @@ test_update (void)
299314
database = get_test_database (client);
300315
ASSERT (database);
301316

302-
collection = mongoc_database_get_collection (database, "test_update");
317+
collection = get_test_collection (client, "test_update");
303318
ASSERT (collection);
304319

305320
context = bson_context_new(BSON_CONTEXT_NONE);
@@ -389,7 +404,7 @@ test_delete (void)
389404
database = get_test_database (client);
390405
ASSERT (database);
391406

392-
collection = mongoc_database_get_collection (database, "test_delete");
407+
collection = get_test_collection (client, "test_delete");
393408
ASSERT (collection);
394409

395410
context = bson_context_new(BSON_CONTEXT_NONE);
@@ -447,7 +462,7 @@ test_index (void)
447462
database = get_test_database (client);
448463
ASSERT (database);
449464

450-
collection = mongoc_database_get_collection (database, "test_index");
465+
collection = get_test_collection (client, "test_index");
451466
ASSERT (collection);
452467

453468
bson_init(&keys);
@@ -518,7 +533,7 @@ test_drop (void)
518533
database = get_test_database (client);
519534
ASSERT (database);
520535

521-
collection = mongoc_database_get_collection (database, "test_drop");
536+
collection = get_test_collection (client, "test_drop");
522537
ASSERT (collection);
523538

524539
doc = BCON_NEW("hello", "world");
@@ -568,7 +583,7 @@ test_aggregate (void)
568583
database = get_test_database (client);
569584
ASSERT (database);
570585

571-
collection = mongoc_database_get_collection (database, "test_aggregate");
586+
collection = get_test_collection (client, "test_aggregate");
572587
ASSERT (collection);
573588

574589
mongoc_collection_drop(collection, &error);

tests/test-mongoc-database.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,14 @@ test_create_collection (void)
133133
mongoc_collection_t *collection;
134134
mongoc_client_t *client;
135135
bson_error_t error = { 0 };
136-
unsigned t;
137-
unsigned p;
138136
bson_t options;
139-
char dbname [32];
137+
char *name;
140138
bool r;
141139

142140
client = mongoc_client_new (gTestUri);
143141
assert (client);
144142

145-
t = time (NULL);
146-
147-
p = gettestpid ();
148-
bson_snprintf (dbname, sizeof dbname, "test%u_%u", t, p);
149-
dbname [sizeof dbname - 1] = '\0';
150-
151-
database = mongoc_client_get_database (client, dbname);
143+
database = mongoc_client_get_database (client, "test");
152144
assert (database);
153145

154146
bson_init (&options);
@@ -157,8 +149,10 @@ test_create_collection (void)
157149
BSON_APPEND_BOOL (&options, "capped", true);
158150
BSON_APPEND_BOOL (&options, "autoIndexId", true);
159151

160-
collection = mongoc_database_create_collection (database, "createCollectionTest", &options, &error);
152+
name = gen_collection_name ("create_collection");
153+
collection = mongoc_database_create_collection (database, name, &options, &error);
161154
assert (collection);
155+
bson_free (name);
162156

163157
r = mongoc_collection_drop (collection, &error);
164158
assert (r);

0 commit comments

Comments
 (0)